#include #include #include #include using namespace std; int nsd(int a, int b) { if (b==0) { return a; } else { return nsd(b, a % b); } } void floyd(int g[200][200][2], int c) { for (int k = 0; k < c; k++) { for (int i = 0; i < c; i++) { for (int j = 0; j < c; j++) { if (g[i][k][1] > 0 && g[k][j][1] > 0) { int c0 = g[i][k][0] * g[k][j][0]; int c1 = g[i][k][1] * g[k][j][1]; int sd = nsd(c0, c1); if (sd == 0) continue; c0 /= sd; c1 /= sd; if (c1 == 0) continue; double c = c0 / c1; //cout << i << "-" << j << "-" << k << endl; double d = 1000000; if (g[i][j][1] > 0) { d = g[i][j][0] / g[i][j][1]; } if (c < d) { g[i][j][0] = c0; g[i][j][1] = c1; } } } } } } /*bool hledej(int g[200][200][2], int n, int start) { bool v[200]; for (int i = 0; i < n; i++) { v[i] = false; } queue fifo; fifo.push(start); while (!fifo.empty()) { int s = fifo.pop(); v[s] = true; for (int k = 0; k < n; i++) { if (g[s][k][0] > 0) { if (!v[k]) { v[k] = true; fifo.push(k); } } } } return false; }*/ int main() { do { int c, r; cin >> c; if (c==0) { break; } map currs; for (int i = 0; i < c; i++) { string curr; cin >> curr; currs[curr] = i; //cout << curr << "=" << i << endl; } int g[200][200][2]; for (int i = 0; i < c; i++) { for (int j = 0; j < c; j++) { if (i == j) { g[i][j][0] = 1; g[i][j][1] = 1; } else { g[i][j][0] = 0; g[i][j][1] = 0; } } } cin >> r; for (int i = 0; i < r; i++) { string cfrom, cto; char c; int ufrom, uto; cin >> cfrom >> cto >> ufrom >> c >> uto; int nfrom = currs[cfrom], nto = currs[cto]; g[nfrom][nto][0] = ufrom; g[nfrom][nto][1] = uto; } /*for (int i = 0; i < c; i++) { for (int j = 0; j < c; j++) { cout << g[i][j][0] << ":" << g[i][j][1] << " "; } cout << endl; }*/ floyd(g, c); /*for (int i = 0; i < c; i++) { for (int j = 0; j < c; j++) { cout << g[i][j][0] << ":" << g[i][j][1] << " "; } cout << endl; }*/ bool arb = false; for (int i = 0; i < c; i++) { if (g[i][i][1] > 0) { double c = g[i][i][0] / g[i][i][1]; if (c != 1) { cout << "Arbitrage" << endl; arb = true; break; } } } if (!arb) { cout << "Ok" << endl; } } while(true); return 0; }