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

<?php

fscanf(STDIN, '%d', $n);
for ($i = 0; $i < $n; $i++) {
    $str = str_replace('=', '', trim(fgets(STDIN)));
    //括弧は1つだけと想定
    if (preg_match('/\(.+\)/', $str, $matches)) {
        $str2 = substr($matches[0], 1, -1);
        $str = str_replace($matches[0], calc($str2), $str);
    }
    echo calc($str), PHP_EOL;
}

function calc($str)
{
    while (preg_match('|[*/]|', $str, $matches, PREG_OFFSET_CAPTURE)) {
        $ope = $matches[0][0];
        $pos = $matches[0][1];
        $a = '';
        $b = '';
        $i = $pos - 1;
        $j = $pos + 1;

        while (true) {
            $bool1 = @ctype_digit($str[$i]);
            $bool2 = @ctype_digit($str[$j]);
            if (!$bool1 && !$bool2) {
                break;
            }

            if ($bool1) {
                $a = $str[$i] . $a;
                $i--;
            }

            if ($bool2) {
                $b .= $str[$j];
                $j++;
            }
        }

        $result = $ope === '*' ? (int)($a * $b) : (int)($a / $b);
        $str = substr($str, 0, $i + 1) . $result . substr($str, $j);
    }

    $arr = array();
    $n = '';
    for ($i = 0, $len = strlen($str); $i < $len; $i++) {
        $ch = $str[$i];
        if (ctype_digit($ch)) {
            $n .= $ch;
        } else {
            $arr[] = (int)$n;
            $n = $ch;
        }
    }
    $arr[] = (int)$n;

    return array_sum($arr);
}