#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define FOR(i,m,n) for (int i = m; i < n; i++) using namespace std; struct Ins { int typ; int a; }; vector ins; bool solve() { ins.clear(); char s[10]; while (1) { scanf(" %s", s); if (strcmp(s, "QUIT")==0) return false; else if (strcmp(s, "NUM")==0) { int a; scanf("%d", &a); ins.push_back((Ins){1, a}); } else if (strcmp(s, "POP")==0) ins.push_back((Ins){2, 0}); else if (strcmp(s, "INV")==0) ins.push_back((Ins){3, 0}); else if (strcmp(s, "DUP")==0) ins.push_back((Ins){4, 0}); else if (strcmp(s, "SWP")==0) ins.push_back((Ins){5, 0}); else if (strcmp(s, "ADD")==0) ins.push_back((Ins){6, 0}); else if (strcmp(s, "SUB")==0) ins.push_back((Ins){7, 0}); else if (strcmp(s, "MUL")==0) ins.push_back((Ins){8, 0}); else if (strcmp(s, "DIV")==0) ins.push_back((Ins){9, 0}); else if (strcmp(s, "MOD")==0) ins.push_back((Ins){10, 0}); else if (strcmp(s, "END")==0) break; } int N; scanf("%d", &N); bool spatne = false; FOR (j, 0, N) { int v; scanf("%d", &v); stack st; st.push(v); int K=1; bool error = false; long long a, b; FOR (i, 0, ins.size()) { //printf("i%d\n", i); if (spatne) { printf("ERROR\n"); break; } switch(ins[i].typ) { case 1: st.push(ins[i].a); K++; break; case 2: if (st.empty()) { error = true; goto ven; } st.pop(); K--; break; case 3: if (st.empty()) { error = true; goto ven; } a = st.top(); st.pop(); st.push(-a); break; case 4: if (st.empty()) { error = true; goto ven; } st.push(st.top()); K++; break; case 5: if (K<2) { error = true; goto ven; } a = st.top(); st.pop(); b = st.top(); st.pop(); st.push(b); st.push(a); break; case 6: //printf("K%d\n", K); if (K<2) { error = true; goto ven; } a = st.top(); st.pop(); b = st.top(); st.pop(); st.push(a+b); K--; break; case 7: if (K<2) { error = true; goto ven; } a = st.top(); st.pop(); b = st.top(); st.pop(); st.push(b-a); K--; break; case 8: if (K<2) { error = true; goto ven; } a = st.top(); st.pop(); b = st.top(); st.pop(); st.push(a*b); K--; break; case 9: if (K<2) { error = true; goto ven; } a = st.top(); st.pop(); b = st.top(); st.pop(); if (a==0) { error = true; goto ven; } st.push(b/a); K--; break; case 10: if (K<2) { error = true; goto ven; } a = st.top(); st.pop(); b = st.top(); st.pop(); if (a==0) { error = true; goto ven; } st.push(b%a); K--; break; } if (!st.empty() && (st.top()<-1000000000 || st.top()>1000000000)) { error = true; goto ven; } if (K>1000) { error = true; spatne = true; goto ven; } } if (spatne) continue; ven: if (error) { printf("ERROR\n"); } else if (K!=1) { spatne = true; printf("ERROR\n"); } else { printf("%lld\n", st.top()); } } printf("\n"); return true; } int main() { while (solve()); return 0; }