
import java.math.*;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Stack;

public class execute {

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line=in.readLine();
        
        while(!"QUIT".equals(line)){
            
            
            
            ArrayList<command> program = new ArrayList<command>();
            
            while(!"END".equals(line)){
                
                command c = new command(line);
                program.add(c);                
              // System.out.println(c.cmd);
                line = in.readLine();     
                
            }
            
            
            int N = Integer.parseInt(in.readLine());
            int i = 0;
            while(i++ < N){
                
                Stack<Long> stack = new Stack<Long>();
                stack.push(Long.parseLong(in.readLine()));
                try{
                    for(command c : program){
                        if("NUM".equals(c.cmd)){
                            stack.push(c.value);
                        }
                        else if("POP".equals(c.cmd)){
                            stack.pop();
                        }
                        else if("INV".equals(c.cmd)){
                            long  a = stack.pop();
                            stack.push(a*(-1));
                        }
                        else if("DUP".equals(c.cmd)){
                            long  a = stack.pop();
                            stack.push(a);
                            stack.push(a);
                        }
                        else if("SWP".equals(c.cmd)){
                            long  a = stack.pop();
                            long b = stack.pop();
                            stack.push(a);
                            stack.push(b);
                        }
                        else if("ADD".equals(c.cmd)){
                            long  a = stack.pop();
                            long b = stack.pop();
                            stack.push(a+b);

                        }
                        else if("SUB".equals(c.cmd)){
                            long  a = stack.pop();
                            long b = stack.pop();
                            stack.push(b-a);
                        }
                        else if("MUL".equals(c.cmd)){
                            long  a = stack.pop();
                            long b = stack.pop();
                            stack.push(a*b);
                        }
                        else if("DIV".equals(c.cmd)){
                            long  a = stack.pop();
                            long b = stack.pop();
                            stack.push(b/a);
                        }
                        else if("MOD".equals(c.cmd)){
                            long  a = stack.pop();
                            long b = stack.pop();
                            stack.push(b%a);
                        }
                        long a = stack.pop();
                        if(Math.abs(a) > 1000000000){
                            throw new Exception();
                        }
                        stack.push(a);
                        
                    }
                    long result = stack.pop();
                    if(!stack.empty()){
                        throw new Exception();
                    }
                    System.out.println(result);
                }
                catch(Exception e){
                    System.out.println("ERROR");
                }
                
            }
            System.out.println();
            line = in.readLine();
            line = in.readLine();

            
        }
        
    }
    
    static class command{
        long value;
        String cmd;

        public command(String s) {
            if(s.contains("NUM")){
                value = Long.parseLong(s.split(" ")[1]);
                        s = s.split(" ")[0];
            }
                cmd = s;
        }
        
        
    }
    
    
}
