#include #include #include #include using namespace std; typedef long long LL; #define MX 100100 #define MXV 1000000000LL int N; char s[10]; char ins[MX][10]; int p[MX]; vector st; int poc; int ex() { //printf("riesim %d\n",st[0]); for(int i=0;iMXV) return -1; st.push_back((int)a); } else if (!strcmp(ins[i],"SUB")) { if (st.size()<2) return -1; LL a=st[st.size()-2]; LL b=st[st.size()-1]; st.pop_back(); st.pop_back(); a=a-b; if (abs(a)>MXV) return -1; st.push_back((int)a); } else if (!strcmp(ins[i],"MUL")) { if (st.size()<2) return -1; LL a=st[st.size()-2]; LL b=st[st.size()-1]; st.pop_back(); st.pop_back(); a=a*b; if (abs(a)>MXV) return -1; st.push_back((int)a); } else if (!strcmp(ins[i],"DIV")) { if (st.size()<2) return -1; LL a=st[st.size()-2]; LL b=st[st.size()-1]; st.pop_back(); st.pop_back(); LL zn=1; if (a<0) zn*=-1; if (b<0) zn*=-1; a=abs(a); b=abs(b); if (b==0) return -1; a=a/b; a*=zn; if (abs(a)>MXV) return -1; st.push_back((int)a); } else if (!strcmp(ins[i],"MOD")) { if (st.size()<2) return -1; LL a=st[st.size()-2]; LL b=st[st.size()-1]; st.pop_back(); st.pop_back(); LL zn=1; if (a<0) zn*=-1; a=abs(a); b=abs(b); if (b==0) return -1; a=a%b; a*=zn; if (abs(a)>MXV) return -1; st.push_back((int)a); } } if (st.size()!=1) return -1; return 0; } int main() { while(scanf("%s",ins[0]),strcmp(ins[0],"QUIT")) { if (strcmp(ins[0],"NUM")==0) { scanf("%d",&p[0]); } //puts(ins[0]); poc=1; while(strcmp(ins[poc-1],"END")) { scanf("%s",ins[poc]); if (!strcmp(ins[poc],"NUM")) scanf("%d",&p[poc]); //puts(ins[poc]); ++poc; } poc--; scanf("%d",&N); while(N--) { st.clear(); int x; scanf("%d",&x); st.push_back(x); if (ex()==-1) puts("ERROR"); else printf("%d\n",st[0]); } puts(""); } return 0; }