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 Ruby (judge verdict: Wrong Answer)

TERM_N  = 0
TERM_OP_PM = 1
TERM_OP_MD = 2
TERM_LB = 3
TERM_RB = 4
TERM_END = 5

def lex_anlz(str)
  expr = []

  for t in str.scan(/\d+|[\+\-\*\/\(\)=]/)
    case t
      when '+', '-'
      expr.push [TERM_OP_PM, t]
      when '*', '/'
      expr.push [TERM_OP_MD, t]
      when '('
      expr.push [TERM_LB, t]
      when ')'
      expr.push [TERM_RB, t]
      when '='
      expr.push [TERM_END, t]
      else
      expr.push [TERM_N, t.to_i]
    end
  end

  return expr
end

def to_rpn(expr)
  rpn = []
  stack = []

  for t in expr
    case t[0]
      when TERM_N
      rpn.push t

      when TERM_OP_PM
      while ! stack.empty? &&
          ((st = stack[-1])[0] == TERM_OP_PM || st[0] == TERM_OP_MD)
        rpn.push stack.pop
      end
      stack.push t

      when TERM_OP_MD
      while ! stack.empty? && stack[-1][0] == TERM_OP_MD
        rpn.push stack.pop
      end
      stack.push t

      when TERM_LB
      stack.push t

      when TERM_RB
      while stack[-1][0] != TERM_LB
        rpn.push stack.pop
      end
      stack.pop

      when TERM_END
      while ! stack.empty?
        rpn.push stack.pop
      end
    end
  end

  return rpn
end

def calc_rpn(rpn)
  stack = []

  for t in rpn
    if t[0] == TERM_N
      stack.push t[1]
    else
      n2 = stack.pop
      n1 = stack.pop
      case t[1]
        when "+"
        stack.push(n1 + n2)
        when "-"
        stack.push(n1 - n2)
        when "*"
        stack.push(n1 * n2)
        when "/"
        stack.push(n1 / n2)
      end
    end
  end

  return stack[-1]
end

### main

n = gets.chomp.to_i

n.times.each do
  str = gets.chomp
  #p str
  expr = lex_anlz(str)
  #p expr
  rpn = to_rpn(expr)
  #p rpn
  num = calc_rpn(rpn)
  puts num
end