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.*;
import java.util.*;

public class Main{
	public static void main(String args[]){
		try{
			new Main();
		}catch(IOException err){
			err.printStackTrace();
		}
	}

	public Main() throws IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		List<Integer> ans = new ArrayList<Integer>();
		String line = in.readLine();

		int size = Integer.parseInt(line);

		for(int n=0; n<size; n++){
			line = in.readLine();
			int a = SC(line);
			ans.add(a);
		}

		for(int i=0; i<ans.size(); i++){
			System.out.println(ans.get(i));
		}
	}

	public int SC(String line){
		List<String> eq = new ArrayList<String>();
		int count = 0;
		while(true){
			String str = line.substring(count, count+1);
			if(str.equals("=")){
				break;
			}
			eq.add(str);
			count++;
		}

		count = 0;
		while(count < eq.size()-1){
			if(isNumber(eq.get(count)) && isNumber(eq.get(count+1))){
				String dst = eq.get(count) + eq.get(count+1);
				eq.add(count+2, dst);
				eq.remove(count);
				eq.remove(count);
			}
			else{
				count++;
			}
		}


		while(eq.size() != 1){
			for(int i=0; i<eq.size(); i++){
				System.out.print(eq.get(i));
			}
			System.out.println();

			//ツ環古環づ個計ツ算
			int bra_count = 0;
			int n = eq.indexOf("(");
			if(n != -1){
				for(int i=n+1; i<eq.size(); i++){
					if(eq.get(i).equals(")") && bra_count == 0){
						eq = RangeReplace(eq, n, i);
						break;
					}
					else if(eq.get(i).equals("(")){
						bra_count++;
					}
					else if(eq.get(i).equals(")")){
						bra_count--;
					}
				}

				continue;
			}

			//ツ掛ツつッツ算ツ環づィツ算
			n = eq.indexOf("*");
			if(n != -1){
				int a = Integer.parseInt(eq.get(n-1)) * Integer.parseInt(eq.get(n+1));
				eq.add(n+2, Integer.toString(a));
				eq.remove(n-1);
				eq.remove(n-1);
				eq.remove(n-1);
				continue;
			}

			n = eq.indexOf("/");
			if(n != -1){
				int a = Integer.parseInt(eq.get(n-1)) / Integer.parseInt(eq.get(n+1));
				eq.add(n+2, Integer.toString(a));
				eq.remove(n-1);
				eq.remove(n-1);
				eq.remove(n-1);
				continue;
			}

			//ツ堕ォツつオツ算ツ暗クツつォツ算
			n = eq.indexOf("+");
			if(n != -1){
				int a = Integer.parseInt(eq.get(n-1)) + Integer.parseInt(eq.get(n+1));
				eq.add(n+2, Integer.toString(a));
				eq.remove(n-1);
				eq.remove(n-1);
				eq.remove(n-1);
				continue;
			}

			n = eq.indexOf("-");
			if(n != -1){
				int a = Integer.parseInt(eq.get(n-1)) - Integer.parseInt(eq.get(n+1));
				eq.add(n+2, Integer.toString(a));
				eq.remove(n-1);
				eq.remove(n-1);
				eq.remove(n-1);
				continue;
			}
		}

		return Integer.parseInt(eq.get(0));
	}

	public List<String> RangeReplace(List<String> eq, int s, int e){
		String part = "";

		for(int i=s+1; i<e; i++){
			part += eq.get(i);
		}

		part = Integer.toString(SC(part+"="));

		eq.add(e+1, part);

		for(int i=s; i<=e; i++){
			eq.remove(s);
		}

		return eq;
	}

	public boolean isNumber(String s){
		try{
			int n = Integer.parseInt(s);
		}catch(NumberFormatException err){
			return false;
		}

		return true;
	}
}