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: Accepted)

$<.gets.to_i.times do
  splited = $<.gets.chomp.scan(/([0-9\.]+|\+|\-|\*|\/|\(|\)|=)/)
  output = []
  stack = []
  a = nil
  until (token = splited.shift) == ["="]
    token = token.first
    case token
    when "(" then stack << token
    when ")" then
      output << a until (a = stack.pop) == "("
    when "*", "/"
      loop do
        a = stack.last
        break unless %w(* /).include?(a)
        output << stack.pop
      end
      stack << token
    when "+", "-"
      loop do
        a = stack.last
        break unless %w(+ - * /).include?(a)
        output << stack.pop
      end
      stack << token
    else
      output << token.to_r
    end
  end
  output << a while (a = stack.pop)
  
  stack = []
  while (x = output.shift)
    if %w(+ - * /).include?(x)
      a, b = stack.pop, stack.pop
      if x == "/" and ((a < 0) ^ (b < 0))
        stack << -(b.abs / a.abs)
      else
        stack << eval("#{b.to_i} #{x} #{a.to_i}").to_i
      end
    else
      stack << x
    end
  end
  puts stack.first.to_i
end