Under construction

Still being built. Things may change

Simple Arithmetic

Interview

Time limit1sMemory limit128 MB

Summary
Evaluate single-digit expressions with usual precedence and floor division, printing WRONG INPUT for bad forms or division by zero.
Level

Easy3 of 10

Topics
Stack, Simulation, Implementation
Solved
No attempts yet

Problem

Geunwoo is ten years old and is learning the four basic arithmetic operations. Tired of grinding through pointless calculations by hand, he decided to write a program that reads an expression and prints its answer.

An expression is built only from single digits 0 to 9 and the operators +, -, *, /. Multiplication and division are computed before addition and subtraction, and operators of the same precedence are computed from left to right.

Geunwoo has not learned real numbers yet, so every division rounds down. Rounding goes toward negative infinity, so the answer to 4/3 is 1 and the answer to -7/2 is -4.

He is young, so he sometimes mistypes an expression. Only the following form counts as a valid expression. One minus sign - may appear at the very front, then comes one digit, and after that a pair of one operator and one digit repeats zero or more times. A leading - is the sign of the first number.

An expression outside this form cannot be computed. An expression that contains a division by zero anywhere cannot be computed either.

Input

The first line contains the number of test cases T. Each of the next T lines contains one expression with no spaces. An expression is built only from the digits 0 to 9 and the operators +, -, *, /, and its length is between 1 and 50.

Output

For each test case, print the value of the expression on its own line. Print WRONG INPUT when the expression cannot be computed. Both the intermediate values and the final result stay within the range of a 64-bit signed integer.

Examples3

  1. Example 1

    Input
    5
    -1
    1+2*3
    2*/3
    9/4+4*5
    /9
    
    Expected output
    -1
    7
    WRONG INPUT
    22
    WRONG INPUT
    
  2. Example 2

    Input
    5
    4/3
    1+2*3-4/5
    9*9
    0
    -0
    
    Expected output
    1
    7
    81
    0
    0
    
  3. Example 3

    Input
    5
    -7/2
    0-7/2
    -9/2/2
    1-9/2
    -1*3/2
    
    Expected output
    -4
    -3
    -3
    -3
    -2