void pop(){return;}
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define elif else if
#define endl "\n"
#define maxx 1000000000
using namespace std;
inline ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void write(ll x)
{
if(x<0) putchar('-'),x=-x;
if(x>=10) write(x/10);
putchar(x%10+'0');
}
stack<ll> a;
ll n,m,step[2020],num[2020],sum;
bool add_fx()
{
string s;
cin >> s;
if(s=="END") return 0;
if(s=="NUM"){
step[++sum]=1;
num[sum]=read();
}
if(s=="POP") step[++sum]=2;
if(s=="INV") step[++sum]=3;
if(s=="DUP") step[++sum]=4;
if(s=="SWP") step[++sum]=5;
if(s=="ADD") step[++sum]=6;
if(s=="SUB") step[++sum]=7;
if(s=="MUL") step[++sum]=8;
if(s=="DIV") step[++sum]=9;
if(s=="MOD") step[++sum]=10;
return 1;
}
void work()
{
for(ll i=1;i<=sum;i++)
{
if(step[i]==1) a.push(num[i]);
if(step[i]==2){
if(a.empty()){
cout << "ERROR\n";
return;
}
a.pop();
}
if(step[i]==3){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt=a.top();
a.pop();
a.push(cnt*(-1));
}
if(step[i]==4){
if(a.empty()){
cout << "ERROR\n";
return;
}
a.push(a.top());
}
if(step[i]==5){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt1=a.top(),cnt2=0;
a.pop();
if(a.empty()){
cout << "ERROR\n";
return;
}
cnt2=a.top();
a.pop();
a.push(cnt1);
a.push(cnt2);
}
if(step[i]==6){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt1=a.top(),cnt2=0;
a.pop();
if(a.empty()){
cout << "ERROR\n";
return;
}
cnt2=a.top();
a.pop();
if(abs(cnt1+cnt2)>maxx){
cout << "ERROR\n";
return;
}
a.push(cnt1+cnt2);
}
if(step[i]==7){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt1=a.top(),cnt2=0;
a.pop();
if(a.empty()){
cout << "ERROR\n";
return;
}
cnt2=a.top();
a.pop();
if(abs(cnt2-cnt1)>maxx){
cout << "ERROR\n";
return;
}
a.push(cnt2-cnt1);
}
if(step[i]==8){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt1=a.top(),cnt2=0;
a.pop();
if(a.empty()){
cout << "ERROR\n";
return;
}
cnt2=a.top();
a.pop();
if(abs(cnt1*cnt2)>maxx){
cout << "ERROR\n";
return;
}
a.push(cnt1*cnt2);
}
if(step[i]==9){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt1=a.top(),cnt2=0;
a.pop();
if(a.empty()||cnt1==0){
cout << "ERROR\n";
return;
}
cnt2=a.top();
a.pop();
if(abs(cnt2/cnt1)>maxx){
cout << "ERROR\n";
return;
}
a.push(cnt2/cnt1);
}
if(step[i]==10){
if(a.empty()){
cout << "ERROR\n";
return;
}
ll cnt1=a.top(),cnt2=0;
a.pop();
if(a.empty()||cnt1==0){
cout << "ERROR\n";
return;
}
cnt2=a.top();
a.pop();
if(abs(cnt2%cnt1)>maxx){
cout << "ERROR\n";
return;
}
a.push(cnt2%cnt1);
}
}
if(a.empty()){
cout << "ERROR\n";
return;
}
cout << a.top() << endl;
a.pop();
return;
}
void solve()
{
cin >> m;
a.push(m);
work();
while(!a.empty())
a.pop();
}
signed main()
{
while(add_fx());
cin >> n;
while(n--)
solve();
return 0;
}