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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;

import static java.lang.Integer.parseInt;

/**
 * Smart Calculator
 */
public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		String[] words;

		//skip first line
		br.readLine();

		while ((line = br.readLine()) != null && !line.isEmpty()) {

			Deque<String> queue = new ArrayDeque<>();
			Deque<Character> stack = new ArrayDeque<>();

			char _c = 0;
			for (char c : line.toCharArray()) {
				switch (c) {
					case '+':
					case '-':
						while (!stack.isEmpty() && (stack.peek() == '*' || stack.peek() == '/')) {
							queue.offer(String.valueOf(stack.pop()));
						}
						stack.push(c);
						break;
					case '*':
					case '/':
					case '(':
						stack.push(c);
						break;
					case ')':
						while (stack.peek() != '(') {
							queue.offer(String.valueOf(stack.pop()));
						}
						stack.pop();
						break;
					case '=':
						while (!stack.isEmpty()) {
							queue.offer(String.valueOf(stack.pop()));
						}
						break;
					default:
						if ('0' <= _c && _c <= '9') {
							String s = queue.pollLast();
							queue.offer(s + c);
						} else {
							queue.offer(String.valueOf(c));
						}
				}
				_c = c;
			}

			//solve
			Deque<Integer> ans = new ArrayDeque<>();
			for (String s : queue) {
				if ("+-*/".indexOf(s) != -1) {
					int a, b;
					b = ans.pop();
					a = ans.pop();
					switch (s) {
						case "+":
							ans.push(a + b);
							break;
						case "-":
							ans.push(a - b);
							break;
						case "*":
							ans.push(a * b);
							break;
						case "/":
							ans.push(a / b);
							break;
					}
				} else {
					ans.push(parseInt(s));
				}
			}
			System.out.println(ans.peek());

		}//end while
	}//end main
}