#include #include #include #include #include #include #include #include #include #include #include using namespace std; // using namespace __gnu_cxx; typedef long long ll; typedef double db; typedef vector vi; typedef vector vs; typedef pair pii; #define INF (1<<30) #define PB push_back #define FI first #define SE second #define REP(i,n) for(int (i)=0;(i)<(n);++(i)) #define FUP(i,a,b) for(int (i)=(a);(i)<=(b);++(i)) #define FDN(i,a,b) for(int (i)=(a);(i)>=(b);--(i)) const int MAX_STACK = 1013; const int MAX_INSTR = 100123; const int MAX_QUESTS = 10123; const int MAXLIMIT = 1000000000; int instr[MAX_INSTR]; int stack[MAX_QUESTS][MAX_STACK]; int ssize[MAX_QUESTS]; bool nieerror[MAX_QUESTS]; char st[23]; int doit() { int n = 0; bool czytajProg = true; bool sign; while (czytajProg) { scanf("%s", st); string s = st; if (s == "NUM") { scanf("%d", &instr[n]); n++; } else if (s == "POP") { instr[n] = -1; n++; } else if (s == "INV") { instr[n] = -2;n++; } else if (s == "DUP") { instr[n] = -3;n++; } else if (s == "SWP") { instr[n] = -4;n++; } else if (s == "ADD") { instr[n] = -5;n++; } else if (s == "SUB") { instr[n] = -6;n++; } else if (s == "MUL") { instr[n] = -7;n++; } else if (s == "DIV") { instr[n] = -8;n++; } else if (s == "MOD") { instr[n] = -9;n++; } else if (s == "END") { czytajProg = false; } else if (s == "QUIT") { return 0; } } int m; scanf("%d", &m); REP(i, m) { scanf("%d", &stack[i][0]); ssize[i] = 1; nieerror[i] = true; } REP(i, n) { // i -ta instrukcja REP(j, m) if (nieerror[j]) { int todo = instr[i]; if (todo >= 0) { stack[j][ssize[j]] = todo; ssize[j]++; } else if (todo == -1) { if (ssize[j] == 0) { nieerror[j] = false; } else { ssize[j] --; } } else if (todo == -2) { if (ssize[j] == 0) { nieerror[j] = false; } else { stack[j][ssize[j] - 1] = -stack[j][ssize[j] - 1]; } } else if (todo == -3) { if (ssize[j] == 0) { nieerror[j] = false; } else { stack[j][ssize[j]] = stack[j][ssize[j] - 1]; ssize[j]++; } } else if (todo == -4) { if (ssize[j] < 2) { nieerror[j] = false; } else { swap(stack[j][ssize[j] - 1], stack[j][ssize[j] - 2]); } } else if (todo == -5) { if (ssize[j] < 2) { nieerror[j] = false; } else { long long x1 = stack[j][ssize[j] - 2]; int x2 = stack[j][ssize[j] - 1]; ssize[j] --; x1 += x2; if (x1 > MAXLIMIT || x1 < -MAXLIMIT) { nieerror[j] = false; } else { stack[j][ssize[j] - 1] = x1; } } } else if (todo == -6) { if (ssize[j] < 2) { nieerror[j] = false; } else { long long x1 = stack[j][ssize[j] - 2]; int x2 = stack[j][ssize[j] - 1]; ssize[j]--; x1 -= x2; if (x1 < -MAXLIMIT || x1 > MAXLIMIT) { nieerror[j] = false; } else { stack[j][ssize[j] - 1] = x1; } } } else if (todo == -7) { if (ssize[j] < 2) { nieerror[j] = false; } else { long long x = stack[j][ssize[j] - 2]; x *= stack[j][ssize[j] - 1]; if (x < -MAXLIMIT || x > MAXLIMIT) { nieerror[j] = false; } else { stack[j][ssize[j] - 2] = x; ssize[j]--; } } } else if (todo == -8) { if (ssize[j] < 2) { nieerror[j] = false; } else { sign = false; int x1 = stack[j][ssize[j] - 2]; int x2 = stack[j][ssize[j] - 1]; if (x1 < 0) { sign = !sign; x1 *= -1; } if (x2 < 0) { sign = !sign; x2 *= -1; } if (x2 == 0) { nieerror[j] = false; } else { stack[j][ssize[j] - 2] = x1 / x2; if (sign) { stack[j][ssize[j] - 2] *= -1; } ssize[j] --; } } } else if (todo == -9) { if (ssize[j] < 2) { nieerror[j] = false; } else { sign = false; int x1 = stack[j][ssize[j] - 2]; int x2 = stack[j][ssize[j] - 1]; if (x1 < 0) { sign = true; x1 *= -1; } if (x2 < 0) { x2 *= -1; } if (x2 == 0) { nieerror[j] = false; } else { stack[j][ssize[j] - 2] = x1 % x2; if (sign) { stack[j][ssize[j] - 2] *= -1; } ssize[j] --; } } } } } REP(i, m) if (nieerror[i] && ssize[i] == 1) { printf("%d\n", stack[i][0]); } else { printf("ERROR\n"); } printf("\n"); return 1; } int main(){ while (doit()) {} return 0; }