#include #include #include #include #include #include #include using namespace std; int n; stack s; vector program; vector parametre; void checkni(size_t pocpar) { if (s.size() < pocpar) throw new exception; } int main() { while(47) { program.clear(); program.reserve(1047); parametre.clear(); parametre.reserve(1047); char buf[47]; scanf(" %s", buf); string t(buf); if (t == "QUIT") return 0; while (t != "END") { program.push_back(t); if (t == "NUM") { int a; scanf("%d", &a); parametre.push_back(a); } scanf(" %s", buf); t = buf; } scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); s.push(a); a = 0; try { for (size_t ip = 0; ip < program.size(); ++ip) { string ci = program[ip]; if (ci == "NUM") { s.push(parametre[a++]); } else if (ci == "POP") { checkni(1); s.pop(); } else if (ci == "INV") { checkni(1); int tmp = s.top(); s.pop(); s.push(-tmp); } else if (ci == "DUP") { checkni(1); s.push(s.top()); } else if (ci == "SWP") { checkni(2); int x = s.top(); s.pop(); int y = s.top(); s.pop(); s.push(x); s.push(y); } else if (ci == "ADD") { checkni(2); int x = s.top(); s.pop(); int y = s.top(); s.pop(); if (abs(x + y) > 1000000000) throw new exception; s.push(x + y); } else if (ci == "SUB") { checkni(2); int x = s.top(); s.pop(); int y = s.top(); s.pop(); if (abs(y - x) > 1000000000) throw new exception; s.push(y - x); } else if (ci == "MUL") { checkni(2); long long x = s.top(); s.pop(); long long y = s.top(); s.pop(); if (abs(x * y) > 1000000000) throw new exception; s.push(x * y); } else if (ci == "DIV") { checkni(2); int x = s.top(); s.pop(); int y = s.top(); s.pop(); if (!x) throw new exception; int res = abs(y) / abs(x); // maju rozne znamienko if (((x < 0) + (y < 0)) % 2) s.push(-res); else s.push(res); } else if (ci == "MOD") { checkni(2); int x = s.top(); s.pop(); int y = s.top(); s.pop(); if (!x) throw new exception; int res = abs(y) % abs(x); // maju rozne znamienko if (((x < 0) + (y < 0)) % 2) s.push(-res); else s.push(res); } } if (s.size() != 1) throw new exception; printf("%d\n", s.top()); } catch (exception *e) { printf("ERROR\n"); } while (!s.empty()) s.pop(); } putchar('\n'); } return 0; }