Problem:
Smart Calculator

Your task is to write a program which reads an expression and evaluates it.

The expression consists of numerical values, operators and parentheses, and the ends with '='.

The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.

Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.

You may assume that there is no division by zero.

All calculation is performed as integers, and after the decimal point should be truncated

Length of the expression will not exceed 100.

-1 × 109 ≤ intermediate results of computation ≤ 109

Input

The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.

Output

For each datasets, prints the result of calculation.

Sample Input

2
4-2*3=
4*(8+4+3)=

Output for the Sample Input

-2
60

Solution in Python (judge verdict: Wrong Answer)

def f(s):
  while 1:
    b=s.find(")")
    if b<0: return g(s)
    a=s[:b].rfind("(")
    s=s.replace(s[a:b+1],f(s[a+1:b]))

def g(s):
  a=""
  f=1
  x=[]
  def F(a,b):
    x.append(a)
    x.append(b)
    return 1,""
  for c in s:
    if c=="=": continue
    elif c in "*/": f,a=F(a,c)
    elif c in "+-":
      if f==1: f,a=0,c
      else: f,a=F(a,c)
    else: f,a=0,a+c
  x.append(a)

  s=x[:]
  x=[]
  f=""
  for e in s:
    if e in "+-": x.append(e)
    elif e in "*/": f=e
    else:
      a=int(e)
      if f=="*": a*=x.pop()
      elif f=="/": a=int(1.0*x.pop()/a)
      x.append(a)
      f=""

  a,f=0,1
  for e in x:
    if e=="+": f=1
    elif e=="-": f=-1
    else: a+=f*e
  return str(a)