#include #include #include #include using namespace std; struct pol { string pl; long long l; }; long long abs(long long a) { if(a<0) return -a; return a; } bool range(long long a) { if(a>=-1000000000 && a<=1000000000) return 1; else return 0; } int main() { string a; cin >> a; while(a!="QUIT") { vector tab; while(a!="END") { pol tmp; tmp.pl=a; if(a=="NUM") { long long l; scanf("%lld", &l); tmp.l=l; } tab.push_back(tmp); cin >> a; } int n; scanf("%d", &n); bool czy=1; for(int x=1;x<=n;++x) { long long p; scanf("%lld", &p); if(!range(p)) { printf("ERROR\n"); continue; } stack stos; stos.push(p); if(!czy) { printf("ERROR\n"); continue; } bool kk=1; for(int i=0;i0 && temp2>0)) w=w; else if(temp1<0 || temp2<0) w=-w; stos.push(w); } else if(tab[i].pl=="MOD") { if(stos.size()<=1) { printf("ERROR\n"); czy=0; kk=0; break; } long long temp1, temp2, w; temp1=stos.top(); stos.pop(); temp2=stos.top(); stos.pop(); if(temp1==0 || !range(abs(temp2)%abs(temp1))) { printf("ERROR\n"); kk=0; break; } w=abs(temp2)%abs(temp1); if(temp2<0) w=-w; stos.push(temp2%temp1); } } if(stos.size()!=1 && kk) printf("ERROR\n"); else if(kk) printf("%lld\n", stos.top()); } printf("\n"); cin >> a; } return 0; }