#include using namespace std; typedef long long LL; enum CMD { MOD, DIV, MUL, SUB, ADD, SWP, DUP, INV, POP, NUM }; stack S; vector commands; bool inerror; LL get() { if (S.empty()) { inerror = true; return 0; } LL n = S.top(); S.pop(); return n; } void put(LL n) { S.push(n); } void check() { if (S.empty()) return; if (abs(S.top()) > 1000000000) inerror = true; } void run_case() { inerror = false; int n; S = stack(); scanf("%d", &n); S.push(n); for (int i = 0; !inerror && i < (int)commands.size(); ++i) { LL a, b, s; switch (commands[i]) { case MOD: b = get(); a = get(); s = (a >= 0) ? 1 : -1; if (b == 0) inerror = true; else put((abs(a) % abs(b)) * s); break; case DIV: b = get(); a = get(); s = (a*b >= 0) ? 1 : -1; if (b == 0) inerror = true; else put((abs(a) / abs(b)) * s); break; case MUL: put(get() * get()); break; case SUB: b = get(); a = get(); put(a - b); break; case ADD: put(get() + get()); break; case SWP: b = get(); a = get(); put(a); put(b); break; case DUP: a = get(); put(a); put(a); break; case INV: put(-get()); break; case POP: get(); break; default: put(commands[i] - 10); break; } check(); } if (S.size() != 1) inerror = true; } int main() { char cmd[64]; while (true) { scanf("%s", cmd); commands.clear(); if (strcmp(cmd, "QUIT") == 0) break; do { string c = cmd; if (c == "MOD") commands.push_back(MOD); else if (c == "DIV") commands.push_back(DIV); else if (c == "MUL") commands.push_back(MUL); else if (c == "SUB") commands.push_back(SUB); else if (c == "ADD") commands.push_back(ADD); else if (c == "SWP") commands.push_back(SWP); else if (c == "DUP") commands.push_back(DUP); else if (c == "INV") commands.push_back(INV); else if (c == "POP") commands.push_back(POP); else { int n; scanf("%d", &n); commands.push_back(10 + n); } scanf("%s", cmd); } while (strcmp(cmd, "END") != 0); int N; scanf("%d", &N); while (N --> 0) { run_case(); if (inerror) puts("ERROR"); else printf("%lld\n", S.top()); } puts(""); } return 0; }