#include <bits/stdc++.h>
#define int long long
const int mx = 1000000000;
using namespace std;
stack<int> st;
int a[2023], b[2023], cnt;
void NUM(int i){
st.push(b[i]);
cnt++;
}
void POP(){
if(st.empty()){
cnt = 114514;
return;
}
st.pop();
cnt--;
}
void INV(){
if(st.empty()){
cnt = 114514;
return;
}
int n = st.top(), m = 0;
st.pop();
while(n){
m = n % 10 + m * 10;
n /= 10;
}
st.push(m);
}
void DUP(){
if(st.empty()){
cnt = 114514;
return;
}
st.push(st.top());
cnt++;
}
void SWP(){
if(st.size() < 2){
cnt = 114514;
return;
}
int n = st.top();
st.pop();
int m = st.top();
st.pop();
st.push(n);
st.push(m);
}
void ADD(){
if(st.size() < 2){
cnt = 114514;
return;
}
int n = st.top();
st.pop();
int m = st.top();
st.pop();
st.push(n + m);
cnt--;
}
void SUB(){
if(st.size() < 2){
cnt = 114514;
return;
}
int n = st.top();
st.pop();
int m = st.top();
st.pop();
st.push(m - n);
cnt--;
}
void MUL(){
if(st.size() < 2){
cnt = 114514;
return;
}
int n = st.top();
st.pop();
int m = st.top();
st.pop();
st.push(n * m);
cnt--;
}
void DIV(){
if(st.size() < 2){
cnt = 114514;
return;
}
int n = st.top();
if(n == 0){
cnt = 114514;
return;
}
st.pop();
int m = st.top();
st.pop();
st.push(m / n);
cnt--;
}
void MOD(){
if(st.size() < 2){
cnt = 114514;
return;
}
int n = st.top();
if(n == 0){
cnt = 114514;
return;
}
st.pop();
int m = st.top();
st.pop();
st.push(m % n);
cnt--;
}
signed main(){
string s;
int k = 0;
while(s != "END"){
cin >> s;
k++;
if(s == "NUM"){int n;scanf("%lld", &n);a[k] = 1; b[k] = n;}
else if(s == "POP")a[k] = 2;
else if(s == "INV")a[k] = 3;
else if(s == "DUP")a[k] = 4;
else if(s == "SWP")a[k] = 5;
else if(s == "ADD")a[k] = 6;
else if(s == "SUB")a[k] = 7;
else if(s == "MUL")a[k] = 8;
else if(s == "DIV")a[k] = 9;
else if(s == "MOD")a[k] = 10;
}
k--;
int n;
scanf("%lld", &n);
while(n--){
int x;
scanf("%lld", &x);
while(!st.empty()){
st.pop();
}
cnt = 1;
st.push(x);
for(int i = 1; i <= k && x != -114514; i++){
switch(a[i]){
case 1:NUM(i);break;
case 2:POP();break;
case 3:INV();break;
case 4:DUP();break;
case 5:SWP();break;
case 6:ADD();break;
case 7:SUB();break;
case 8:MUL();break;
case 9:DIV();break;
case 10:MOD();break;
default:x = -114514;break;
}
if(abs(st.top()) > mx || cnt < 0){
x = -114514;
}
}
if(cnt != 1){
x = -114514;
}
if(x == -114514){
printf("ERROR\n");
}else{
printf("%lld\n", st.top());
}
}
return 0;
}