#include #include #include #include #include #include #include #include #include #include #include #define FORE(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define FC(aa, bb) FORE(bb, aa) #define debug(x) cerr << #x << " = " << x << endl; #define debugv(x) cerr << #x << " = " ; FORE(it, (x)) cerr << *it << ", "; cerr << endl; #define fup(i, a, b) for(int i = (a); i <= (b); ++i) #define fdo(i, a, b) for(int i = (a); i >= (b); --i) #define FOR fup #define FORD fdo #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() #define CLR(x) memset((x), 0, sizeof(x)) #define MP make_pair #define PB push_back #define siz(a) ((int)(a).size()) #define SZ siz #define inf 1000000000 #define FI first #define SE second #include using namespace std; typedef long long lli; typedef double ld; typedef lli LL; typedef ld LD; typedef vector VI; typedef pair PII; stack t; bool dupa = 0; int po() { int x = t.top(); t.pop(); return x; } bool check1() { if (t.empty()) { //cout << "CHUJ" << endl; return true; } return false; } bool check2() { if (siz(t) < 2) { //cout << "CHUJ" << endl; return true; } return false; } void check(lli a) { if (abs(a) <= 1000000000); else dupa = true; } void num(int x) { t.push(x); } void pop() { if (!check1()) t.pop(); } void inv() { if (!check1()) { int x = t.top(); t.pop(); t.push(-x); } } void dup() { if (!check1()) { int x = t.top(); t.push(x); } } void swp() { if (!check2()) { int x, y; x = po(); y = po(); t.push(x); t.push(y); } } void add() { if (!check2()) { int x, y; x = po(); y = po(); check(x + y); t.push(x + y); } } void sub() { if (!check2()) { int x, y; x = po(); y = po(); check(y - x); t.push(y - x); } } void mul() { if (!check2()) { int x, y; x = po(); y = po(); check((lli)x * (lli)y); int z = x * y; t.push(z); } } void div() { if (!check2()) { int x, y; x = po(); y = po(); if (x == 0) { // cout << "DIV 0 !!!!!!!!" << endl; dupa = true; return ; } int z = abs(y) / abs(x); if ((x < 0 && y >= 0) || (y < 0 && x >= 0)) { z = -z; } check(z); t.push(z); } } void mod() { if (!check2()) { int x, y; x = po(); y = po(); if (x == 0) { // cout << "DIV 0 !!!!!!!!" << endl; dupa = true; return ; } int z = abs(y) % abs(x); if (y < 0) { z = -z; } check(z); t.push(z); } } vector > code; void execute() { FORE(it, code) { if (dupa) break; int c, n; c = it -> first; n = it -> second; // cout << c << " " << n << endl; switch(c) { case 0: num(n); break; case 1: pop(); break; case 2: inv(); break; case 3: dup(); break; case 4: swp(); break; case 5: add(); break; case 6: sub(); break; case 7: mul(); break; case 8: div(); break; case 9: mod(); break; } } } void check_end() { if (t.size() != 1) { // cout << "CHUJ" << endl; dupa = true; } } char buff[20]; int main() { while (true) { code.clear(); while (true) { scanf("%s", buff); char c = buff[0]; if (c == 'Q') return 0; if (c == 'E') break; if (c == 'N') { int u; scanf("%d", &u); code.PB(MP(0, u)); } if (c == 'P') code.PB(MP(1, 0)); if (c == 'I') code.PB(MP(2, 0)); if (c == 'D' && buff[1] == 'U') code.PB(MP(3, 0)); if (c == 'S' && buff[1] == 'W') code.PB(MP(4, 0)); if (c == 'A') code.PB(MP(5, 0)); if (c == 'S' && buff[1] == 'U') code.PB(MP(6, 0)); if (c == 'M' && buff[1] == 'U') code.PB(MP(7, 0)); if (c == 'D' && buff[1] == 'I') code.PB(MP(8, 0)); if (c == 'M' && buff[1] == 'O') code.PB(MP(9, 0)); } int k; scanf("%d", &k); fup(i, 1, k) { dupa = false; while (!t.empty()) t.pop(); int v; scanf("%d", &v); t.push(v); execute(); check_end(); if (dupa) { printf("ERROR\n"); } else { printf("%d\n", t.top()); } } printf("\n"); } return 0; }