// Problem: P2186 小 Z 的栈函数
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2186
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e7+5;
int c[N];
int num,cnt;
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
vector<string> v;
stack<int> st;
while(cin>>s){
if(s=="END") break;
v.push_back(s);
if(s=="NUM"){
int x;
cin>>x;
c[++num]=x;
}
}
// for(int i=1;i<=num;i++){
// cout<<c[i]<<' ';
// }
// cout<<"\n";
// for(auto i:v){
// cout<<i<<' ';
// }
// cout<<"\n";
int n;
cin>>n;
while(n--){
while(!st.empty()){
st.pop();
}
cnt=1;
bool flag=true;
int x;
cin>>x;
st.push(x);
if(abs(x)>1000000000){
flag=false;
continue;
}
for(auto i:v){
if(i=="NUM"){
int k=c[cnt];
cnt++;
st.push(k);
if(abs(k)>1000000000){
flag=false;
break;
}
// stack<int> ss;
// ss=st;
// while(!ss.empty()){
// cout<<ss.top()<<' ';
// ss.pop();
// }
}
else if(i=="POP"){
if(st.size()<1){
flag=false;
break;
}
st.pop();
}
else if(i=="INV"){
if(st.size()<1){
flag=false;
break;
}
int k=st.top();
st.pop();
k=0-k;
if(abs(k)>1000000000){
flag=false;
break;
}
st.push(k);
}
else if(i=="DUP"){
if(st.size()<1){
flag=false;
break;
}
int k=st.top();
if(abs(k)>1000000000){
flag=false;
break;
}
st.push(k);
}
else if(i=="SWP"){
if(st.size()<2){
flag=false;
break;
}
int k1=st.top();
st.pop();
if(abs(k1)>1000000000){
flag=false;
break;
}
int k2=st.top();
st.pop();
if(abs(k2)>1000000000){
flag=false;
break;
}
st.push(k2);
st.push(k1);
}
else if(i=="ADD"){
if(st.size()<2){
flag=false;
break;
}
// stack<int> ss;
// ss=st;
// while(!ss.empty()){
// cout<<ss.top()<<' ';
// ss.pop();
// }
int k1=st.top();
st.pop();
int k2=st.top();
st.pop();
int k3=k2+k1;
//cout<<k1<<' '<<k2<<"\n";
if(abs(k3)>1000000000){
flag=false;
break;
}
st.push(k3);
// stack<int> ss;
// ss=st;
// while(!ss.empty()){
// cout<<ss.top()<<' ';
// ss.pop();
// }
}
else if(i=="SUB"){
if(st.size()<2){
flag=false;
break;
}
int k1=st.top();
st.pop();
int k2=st.top();
st.pop();
int k3=k2-k1;
if(abs(k3)>1000000000){
flag=false;
break;
}
st.push(k3);
}
else if(i=="MUL"){
if(st.size()<2){
flag=false;
break;
}
int k1=st.top();
st.pop();
int k2=st.top();
st.pop();
int k3=k2*k1;
if(abs(k3)>1000000000){
flag=false;
break;
}
st.push(k3);
}
else if(i=="DIV"){
if(st.size()<2){
flag=false;
break;
}
int k1=st.top();
st.pop();
int k2=st.top();
st.pop();
if(k1==0) flag=false;
else{
int k3=k2/k1;
if(abs(k3)>1000000000){
flag=false;
break;
}
st.push(k3);
}
}
else if(i=="MOD"){
if(st.size()<2){
flag=false;
break;
}
int k1=st.top();
st.pop();
int k2=st.top();
st.pop();
if(k1==0) flag=false;
else{
int k3=k2%k1;
if(abs(k3)>1000000000){
flag=false;
break;
}
st.push(k3);
}
}
}
// stack<int> ss;
// ss=st;
// while(!ss.empty()){
// cout<<ss.top()<<' ';
// ss.pop();
// }
//cout<<"\n";
if(st.size()!=1||flag==false){
cout<<"ERROR\n";
}
else{
int res=st.top();
cout<<res<<"\n";
}
}
return 0;
}