Gooseberry Tart BASIC

No attempts yetTime limit1sMemory limit128 MB

Problem

Many people fondly remember early home computers and their built-in BASIC programming language. There has been a surge of nostalgic development as teams compete to recreate these iconic systems. You have been asked to help team "Gooseberry Tart" by building them a BASIC interpreter called GTB1. Because modern machines have plenty of memory and are expected to process video streams in real time, your interpreter must run fast. Your task is to implement an interpreter for the limited subset of the language specified below.

The GTB1 language

  • There is only one variable type: the 32-bit signed integer. Every <expression> is integer-valued. Boolean comparison syntax appears only inside the IF statement.
  • Variable names start with a letter (AZ, az) and may continue with letters or digits. A name can be any length, but only the first two characters matter, and names are case-insensitive. For example Fred, Fr, Freda, and fRE all refer to the same variable.
  • Reserved words are case-insensitive, and only their first two characters are significant. No variable name may share its first two characters with any reserved word. The reserved words are LET, GOTO, IF, FOR, TO, NEXT, OUT, and COMMENT.
  • Arithmetic expressions may use parentheses ( ) and the binary operators + - * / % (addition, subtraction, multiplication, integer division, and modulo) with the usual precedence.
  • A unary minus may appear only at the start of an expression or of a bracketed sub-expression; it has the same precedence as binary + and -.
  • A program is a sequence of instruction lines. Each line begins with a line number. Line numbers are strictly ascending and lie in the range $1$ to $10000$ inclusive. Each line holds exactly one statement: an assignment, a GOTO, an IF, a FOR, a NEXT, an OUT, or a comment.

Statement forms

  • Assignment: LET <variable> = <expression>
  • Goto: GOTO <line number>
  • If: IF <expression> <comparison> <expression> GOTO <line number>, where <comparison> is one of =, <, >, <=, >=, or <> (equal, less than, greater than, less than or equal, greater than or equal, not equal).
  • For: FOR <variable> = <start expression> TO <end expression>
  • Next: NEXT <variable>
  • Comment: COMMENT <any text>
  • Out: OUT <expression>

FOR / NEXT loops

A loop is a matching FOR and NEXT pair that use the same variable. It begins with the FOR statement, contains the loop body, and ends with the NEXT statement. When the FOR statement runs, the start expression is assigned to the variable and the body executes at least once. Each time the NEXT statement runs it adds $1$ to the variable; if the result is less than or equal to the end expression, the body executes again. The end expression is re-evaluated on every NEXT. Loops may be nested, and the nesting must be proper (an inner FOR/NEXT lies entirely between its outer FOR and NEXT). A GOTO may jump into or out of a FOR/NEXT structure.

Execution

Statements normally execute in the numerical order in which they are written, starting at the first line. GOTO and IF can change this order. A program finishes when execution runs off the end. Variables need not be declared: they come into existence when first assigned or referenced, and a variable referenced before it is assigned has the value $0$. The OUT statement prints one line containing the value of its expression.

Input

The input is a sequence of programs. Each program starts with a line containing an integer $N$, the number of lines in the program, with $1 \le N \le 1000$. It is followed by $N$ statement lines. No statement is longer than $80$ characters. Tokens may be separated by more than one space, and a line may have leading or trailing spaces; there are no tabs, and operators and parentheses need not be surrounded by spaces. Each statement line has a line number and a statement. The input ends with a program whose $N$ is $0$. Every program is syntactically correct and is guaranteed to terminate.

Output

For each program, print a line Programme <i> (where <i> is $1, 2, 3, \dots$), followed by every line produced by the program's OUT statements, in order.

The original test data may require executing up to $10^9$ instructions, so an efficient interpreter is expected.