#include #include #include #include #include #include #include #define SIZE(x) ((int) (x).size()) #define For(i, n) for (int i = 0; i < (int) (n); ++i) #define VELA 1000000000 using namespace std; typedef long long ll; char inst[100047][5]; int par[100047]; stack S; inline int mabs(int x){ return (x<0)?-x:x; } inline ll mabs(ll x){ return (x<0)?-x:x; } int sprav(int x){ if (inst[x][0]=='N') { S.push(par[x]); return 0; } if (inst[x][0]=='P'){ if (S.empty()) return 2; else S.pop(); return 0; } if (inst[x][0]=='I'){ if (S.empty()) return 2; int a = S.top(); S.pop(); S.push(-a); return 0; } if (inst[x][0]=='D' && inst[x][1] == 'U'){ if (S.empty()) return 2; int a = S.top(); S.push(a); return 0; } if (inst[x][0]=='S' && inst[x][1] == 'W'){ if (S.empty()) return 2; int a = S.top(); S.pop(); if (S.empty()) return 2; int b = S.top(); S.pop(); S.push(a); S.push(b); return 0; } if (inst[x][0]=='A' && inst[x][1] == 'D'){ if (S.empty()) return 2; int a = S.top(); S.pop(); if (S.empty()) return 2; int b = S.top(); S.pop(); if (mabs(a+b)>VELA) return 1; S.push(a+b); return 0; } if (inst[x][0]=='S' && inst[x][1] == 'U'){ if (S.empty()) return 2; int a = S.top(); S.pop(); if (S.empty()) return 2; int b = S.top(); S.pop(); if (mabs(b-a)>VELA) return 1; S.push(b-a); return 0; } if (inst[x][0]=='M' && inst[x][1] == 'U'){ if (S.empty()) return 2; ll a = S.top(); S.pop(); if (S.empty()) return 2; ll b = S.top(); S.pop(); if (mabs(a*b)>VELA) return 1; S.push(a*b); return 0; } if (inst[x][0]=='D' && inst[x][1] == 'I'){ if (S.empty()) return 2; int a = S.top(); S.pop(); if (S.empty()) return 2; int b = S.top(); S.pop(); if (a==0) return 1; int c = mabs(b)/mabs(a); if (a<0) c*=-1; if (b<0) c*=-1; S.push(c); return 0; } if (inst[x][0]=='M' && inst[x][1] == 'O'){ if (S.empty()) return 2; int a = S.top(); S.pop(); if (S.empty()) return 2; int b = S.top(); S.pop(); if (a==0) return 1; int c; if (b<0) { c = -mabs(b)%mabs(a); }else{ c = mabs(b)%mabs(a); } S.push(c); return 0; } return 0; } int N,M; int main() { while(1){ int cis = 0; while(1){ scanf("%s", inst[cis]); if (inst[cis][0]=='Q') return 0; if (inst[cis][0]=='E') break; if (inst[cis][0]=='N') scanf("%d",par+cis); cis++; } scanf("%d",&N); bool total = 0; For(i,N){ scanf("%d",&M); if (total){ printf("ERROR\n"); continue ; } int b = 0; while(!S.empty()) S.pop(); S.push(M); For(x, cis){ b = sprav(x); // printf("%d %d\n",x,b); if (b) break; } if ((b==0) && (S.size()!=1)) b = 2; if (b==1) { printf("ERROR\n"); } if (b==2) { printf("ERROR\n"); total = 1; } if (b==0){ printf("%d\n",S.top()); } // printf("%d\n", b); } printf("\n"); } }