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)

def parse tokens
  case t = tokens.shift
  when /\d+/
    parse_with_left_expr t, tokens
  when '('
    depth = 1
    pos = tokens.index {|t|
      if t == '('
        depth += 1
        false
      elsif t == ')'
        (depth -= 1).zero?
      else
        false
      end
    }
    parse_with_left_expr(parse(tokens[0..pos - 1]), tokens[pos + 1..-1])
  else
    raise t.to_s
  end
end

def parse_with_left_expr left, tokens
    return [left] if tokens.empty?
    case op = tokens.shift
    when '+', '-'
      [left, parse(tokens), op]
    when '*', '/'
      case right = tokens.first
      when /\d+/
        tokens.shift
        parse_with_left_expr [left, right, op], tokens
      when '('
        [left, parse(tokens), op]
      else
        raise right.to_s
      end
    else
      raise op.to_s
    end
end

gets.to_i
  .times
  .lazy
  .map{ gets.chomp }
  .map {|s| s.scan(/([+\-*\/()]|\d+)/).flatten }
  .map(&method(:parse))
  .map(&:flatten)
  .map {|tokens|
    tokens.reduce([]) {|s, t|
      case t
      when /\d+/
        s << t.to_i
      else
        r = s.pop
        l = s.pop
        s << l.send(t, r)
        s
      end
    }.first
  }
  .each(&method(:puts))