#include <iostream>
#include <string.h>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
const ll Max=1000000000;
vector<string> op;
vector<ll > num;
bool ok(ll x)
{
if(x>Max||x<-Max)return false;
return true;
}
ll solve(ll t )
{
if(!ok(t))return Max+1;
stack<ll> q;
q.push(t);
int j=0;
for(int i=0; i<op.size(); i++)
{
if(op[i]=="NUM")
{
if(ok(num[j]))
q.push(num[j]),j++;
else
return Max+1;
}
else if(op[i]=="POP")
{
if(!q.empty())
q.pop();
else
return Max+1;
}
else if(op[i]=="INV")
{
if(!q.empty())
{
ll tmp=q.top();
q.pop();
q.push(-tmp);
}
else
return Max+1;
}
else if(op[i]=="DUP")
{
if(!q.empty())
{
ll tmp=q.top();
q.push(tmp);
}
else
return Max+1;
}
else if(op[i]=="SWP")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
q.push(tmp);
q.push(tmp1);
}
else if(op[i]=="ADD")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(ok(tmp+tmp1))
q.push(tmp+tmp1);
else
return Max+1;
}
else if(op[i]=="SUB")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(ok(tmp1-tmp))
q.push(tmp1-tmp);
else
return Max+1;
}
else if(op[i]=="MUL")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(ok(tmp*tmp1))
q.push(tmp*tmp1);
else
return Max+1;
}
else if(op[i]=="DIV")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(tmp==0)return Max+1;
if(ok(tmp1/tmp))
q.push(tmp1/tmp);
else
return Max+1;
}
else if(op[i]=="MOD")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(tmp==0)return Max+1;
ll c=tmp1/tmp;
if(ok(tmp1-c*tmp))
q.push(tmp1-c*tmp);
else
return Max+1;
}
}
int sum=0;
ll ans;
while(!q.empty())
{
ans=q.top();
q.pop();
sum++;
}
if(sum==1&&ok(ans))return ans;
return Max+1;
}
int main()
{
op.clear();
num.clear();
ll t;
string a;
ll n;
while(cin>>a)
{
if(a=="QUIT")break;
if(a!="END")
{
op.push_back(a);
if(a=="NUM")
{
scanf("%lld",&n);
num.push_back(n);
}
}
else
{
scanf("%lld",&n);
for(ll i=0; i<n; i++)
{
scanf("%lld",&t);
ll ans=solve(t);
if(ans>Max)
puts("ERROR");
else
printf("%lld\n",ans);
}
puts("");
op.clear();
num.clear();
}
}
return 0;
}
#include <string.h>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <stack>
using namespace std;
typedef long long ll;
const ll Max=1000000000;
vector<string> op;
vector<ll > num;
bool ok(ll x)
{
if(x>Max||x<-Max)return false;
return true;
}
ll solve(ll t )
{
if(!ok(t))return Max+1;
stack<ll> q;
q.push(t);
int j=0;
for(int i=0; i<op.size(); i++)
{
if(op[i]=="NUM")
{
if(ok(num[j]))
q.push(num[j]),j++;
else
return Max+1;
}
else if(op[i]=="POP")
{
if(!q.empty())
q.pop();
else
return Max+1;
}
else if(op[i]=="INV")
{
if(!q.empty())
{
ll tmp=q.top();
q.pop();
q.push(-tmp);
}
else
return Max+1;
}
else if(op[i]=="DUP")
{
if(!q.empty())
{
ll tmp=q.top();
q.push(tmp);
}
else
return Max+1;
}
else if(op[i]=="SWP")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
q.push(tmp);
q.push(tmp1);
}
else if(op[i]=="ADD")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(ok(tmp+tmp1))
q.push(tmp+tmp1);
else
return Max+1;
}
else if(op[i]=="SUB")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(ok(tmp1-tmp))
q.push(tmp1-tmp);
else
return Max+1;
}
else if(op[i]=="MUL")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(ok(tmp*tmp1))
q.push(tmp*tmp1);
else
return Max+1;
}
else if(op[i]=="DIV")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(tmp==0)return Max+1;
if(ok(tmp1/tmp))
q.push(tmp1/tmp);
else
return Max+1;
}
else if(op[i]=="MOD")
{
ll tmp,tmp1;
if(!q.empty())
{
tmp=q.top();
q.pop();
}
else
return Max+1;
if(!q.empty())
{
tmp1=q.top();
q.pop();
}
else
return Max+1;
if(tmp==0)return Max+1;
ll c=tmp1/tmp;
if(ok(tmp1-c*tmp))
q.push(tmp1-c*tmp);
else
return Max+1;
}
}
int sum=0;
ll ans;
while(!q.empty())
{
ans=q.top();
q.pop();
sum++;
}
if(sum==1&&ok(ans))return ans;
return Max+1;
}
int main()
{
op.clear();
num.clear();
ll t;
string a;
ll n;
while(cin>>a)
{
if(a=="QUIT")break;
if(a!="END")
{
op.push_back(a);
if(a=="NUM")
{
scanf("%lld",&n);
num.push_back(n);
}
}
else
{
scanf("%lld",&n);
for(ll i=0; i<n; i++)
{
scanf("%lld",&t);
ll ans=solve(t);
if(ans>Max)
puts("ERROR");
else
printf("%lld\n",ans);
}
puts("");
op.clear();
num.clear();
}
}
return 0;
}