import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class execute
{
	public static void main(String[] args) throws Exception
	{
	int max=1000000000;
	int min=-1000000000;
		StringTokenizer st;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String line;
		line = br.readLine();
		ArrayList<String> prog;
		Stack<Integer> zas;
		
		while(line.compareTo("QUIT")!= 0)
		{
			prog = new ArrayList<String>();
			while(line.compareTo("END")!= 0)
			{
				prog.add(line);
				line = br.readLine();
				//System.out.println(line);
			}
			
			int pocet = Integer.parseInt(br.readLine());
			for(int i = 0; i < pocet; i++)
			{
				zas=new Stack<Integer>();
				zas.push(Integer.parseInt(br.readLine()));
				try
				{
				for (int ii=0; ii<prog.size(); ii++)
				{
					String e = prog.get(ii);
					if (e.compareTo("POP")==0)
					{
						zas.pop();
					} else if (e.compareTo("INV")==0)
					{
						zas.push(-1*zas.pop());
					} else if (e.compareTo("DUP")==0)
					{
						zas.push(zas.peek());
					} else if (e.compareTo("SWP")==0)
					{
						int a=zas.pop();
						int b=zas.pop();
						zas.push(a);
						zas.push(b);
					} else if (e.compareTo("ADD")==0)
					{
						int a=zas.pop();
						int b=zas.pop();
						if (Math.abs(a+b)>max) throw new Exception();
						zas.push(a+b);
					} else if (e.compareTo("SUB")==0)
					{
						int a=zas.pop();
						int b=zas.pop();
						if (Math.abs(a-b)>max) throw new Exception();
						zas.push(b-a);
					} else if (e.compareTo("MUL")==0)
					{
						int a=zas.pop();
						int b=zas.pop();
						if (Math.abs(a*b)>max) throw new Exception();
						zas.push(a*b);
					} else if (e.compareTo("DIV")==0)
					{
						int a=zas.pop();
						int b=zas.pop();
						zas.push(b/a);
					} else if (e.compareTo("MOD")==0)
					{
						int a=zas.pop();
						int b=zas.pop();
						int c=b%a;
						if (c*b<0) c=c*-1;
						zas.push(c);
					} else
					{
						String[] pr=e.split(" ");
						if (Math.abs(Integer.parseInt(pr[1]))>max) throw new Exception();
						if (Integer.parseInt(pr[1])<0) throw new Exception();
						zas.push(Integer.parseInt(pr[1]));
					}
				}
				
				
				if (!zas.empty())
				{
					int c=zas.pop();
					
					if (!zas.empty())
						sb.append("ERROR\n");
					else
						sb.append(c).append("\n");
				}
				else
					sb.append("ERROR\n");
				} catch (Exception e)
				{
					sb.append("ERROR\n");
				}
				//System.out.println(sb.toString());
			}
			sb.append("\n");
			br.readLine();
			line=br.readLine();
			
		}
		
		System.out.println(sb.toString().trim());
		System.out.println();
		
	}
}