import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Stack; import java.util.StringTokenizer; /** * * @author cteam17 */ public class execute { StringTokenizer st = new StringTokenizer(""); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); /** * @param args the command line arguments */ public static void main(String[] args) throws Exception{ execute instance = new execute(); while (instance.run()){}; } String nextToken() throws Exception{ while (!st.hasMoreTokens()) st = new StringTokenizer(input.readLine()); return st.nextToken(); } int nextInt() throws Exception{ return Integer.parseInt(nextToken()); } List commands; Stack stack; boolean run() throws Exception{ commands = new ArrayList(); String command; do{ command = nextToken(); //System.out.println("nacitam " + command); if (command.equals("QUIT")){ return false; } if (command.equals("NUM")){ command += " " + nextToken(); } commands.add(command); }while(!command.equals("END")); int count = nextInt(); for (int i = 0; i < count; i++){ stack = new Stack(); stack.push(nextInt()); Integer result = execute(stack, commands); System.out.println(result == null ? "ERROR" : String.valueOf(result)); } System.out.println(); return true; } private Integer execute(Stack stack, List commands) { try{ for (String c : commands){ if (c.startsWith("NUM")){ Integer num = Integer.valueOf(c.substring(c.indexOf(" ") + 1)); stack.push(num); } else if (c.equals("POP")){ stack.pop(); } else if (c.equals("INV")){ Integer num = stack.pop(); stack.push(-num); } else if (c.equals("DUP")){ Integer num = stack.peek(); stack.push(num); } else if (c.equals("SWP")){ Integer num1 = stack.pop(); Integer num2 = stack.pop(); stack.push(num1); stack.push(num2); } else if (c.equals("ADD")){ Integer num1 = stack.pop(); Integer num2 = stack.pop(); if (Math.abs(num1 + num2) > 1000000000){ throw new Exception(); } stack.push(num1 + num2); } else if (c.equals("SUB")){ Integer num1 = stack.pop(); Integer num2 = stack.pop(); if (Math.abs(num2 - num1) > 1000000000){ throw new Exception(); } stack.push(num2 - num1); } else if (c.equals("MUL")){ Integer num1 = stack.pop(); Integer num2 = stack.pop(); if (Math.abs(num1 * num2) > 1000000000){ throw new Exception(); } stack.push(num1 * num2); } else if (c.equals("DIV")){ Integer num1 = stack.pop(); Integer num2 = stack.pop(); stack.push(num2 / num1); } else if (c.equals("MOD")){ Integer num1 = stack.pop(); Integer num2 = stack.pop(); if (num1 < 0){ stack.push(-(num2 % num1)); } else{ stack.push(num2 % num1); } } } if (stack.size() != 1){ return null; } return stack.peek(); } catch (Exception e){ return null; } } }