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
    })
  }
}
