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

object Main extends App{
  import scala.io.StdIn.{readLine}
  type Rest = List[Char]
  type CharMatch = Option[(Char, Rest)]
  type StringMatch = Option[(List[Char], Rest)]
  type CalculateResult = Option[(Int, Rest)]
  type Operator = Option[(Int ⇒ Int ⇒ Int, Rest)]
  object Digit{
    def unapply(char: List[Char]):CharMatch = {
      char match{
        case h::t if h.isDigit ⇒ Some((h, t))
        case _ ⇒ None
      }
    }
  }
  object Number{
    def unapply(string:List[Char]): StringMatch = {
      string match{
        case Digit(digit, Number(succ, rest)) ⇒ Some((digit :: succ, rest))
        case Digit(digit, rest) ⇒ Some((digit::Nil, rest))
        case _ ⇒ None
      }
    }
  }
  object Parenthesis{
    def unapply(string:List[Char]): CalculateResult = {
      string match{
        case '('::Expression(expression, ')'::rest) ⇒ Some((expression, rest))
        case _ ⇒ None
      }
    }
  }
  object Term{
    def unapply(string:List[Char]):CalculateResult ={
      string match{
        case Number(number, rest) ⇒ Some((number.mkString.toInt, rest))
        case Parenthesis(result, rest) ⇒ Some((result, rest))
        case _ ⇒ None
      }
    }
  }
  object PriorityOperator{
    def unapply(string:List[Char]):Operator = {
      string match{
        case '*'::rest ⇒ Some((a ⇒ b ⇒ a * b, rest))
        case '/'::rest ⇒ Some((a ⇒ b ⇒ a / b, rest))
        case _ ⇒ None
      }
    }
  }
  object NoPriorityOperator{
    def unapply(string:List[Char]):Operator = {
      string match{
        case '+'::rest ⇒ Some((a ⇒ b ⇒ a + b, rest))
        case '-'::rest ⇒ Some((a ⇒ b ⇒ a - b, rest))
        case _ ⇒ None
      }
    }
  }
  object PriorityTerm{
    def unapply(string:List[Char]):CalculateResult = {
      string match{
        case Term(left, PriorityOperator(op, Term(right, rest))) ⇒ Some((op(left)(right), rest))
        case Term(term, rest) ⇒ Some(term, rest)
        case _ ⇒ None
      }
    }
  }
  object Expression{
    def unapply(string:List[Char]): CalculateResult = {
      string match {
        case PriorityTerm(left, NoPriorityOperator(op, Expression(right, rest))) ⇒ Some((op(left)(right), rest))
        case PriorityTerm(result, rest) ⇒ Some((result, rest))
        case _ ⇒ Some(0, '='::Nil)
      }
    }
  }
  for (_ ← 0 until readLine().toInt){
    println (readLine().trim.toList match{
      case Expression(result, '='::Nil) ⇒ result
      case _ ⇒ 0
    })
  }
}