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: Accepted)

import java.util.*;

public class Main {
	String s = "";
	int pos = 0;
	
	int exp(){
		int p = term();
		while(true){
			if(pos==s.length()) break;
			int q = s.codePointAt(pos);
			pos++;
			if(q==43) p+=term();
			else if(q==45) p-=term();
			else break;
		}
		return p;
	}
	
	int term(){
		int p = fact();
		while(true){
			if(pos==s.length()) break;
			int q = s.codePointAt(pos);
			pos++;
			if(q==42) p*=fact();
			else if(q==47) p/=fact();
			else break;
		}
		pos--;
		return p;
	}
	
	int fact(){
		int q = s.codePointAt(pos);
		pos++;
		if(q==40) return exp();
		if(q==43) return fact();
		if(q==45) return -fact();

		pos--;
		int ini = pos;
		while(s.codePointAt(pos)>=48 && s.codePointAt(pos)<=57){
			pos++;
			if(pos==s.length()) break;
		}
		int p = Integer.valueOf(s.substring(ini,pos));
		return p;
	}
	
	void solve(){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for(int i=0;i<n;i++){
			s = sc.next();
			pos = 0;
			System.out.println(exp());
		}
	}

	
	public static void main(String[] args) {
		new Main().solve();
	}	
}