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

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main{

	Scanner sc=new Scanner(System.in);

	int INF=1<<28;
	double EPS=1e-9;

	String s;

	void run(){
		for(int n=sc.nextInt(); n>0; n--){
			s=sc.next();
			println(equation(0).val+"");
		}
	}

	R equation(int p){
		R r=factor(p);
		for(;;){
			char c=s.charAt(r.p);
			if(c=='+'||c=='-'){
				R q=factor(r.p+1);
				if(c=='+')
					r.val+=q.val;
				else
					r.val-=q.val;
				r.p=q.p;
			}else{
				break;
			}
		}
		return r;
	}

	R factor(int p){
		R r=term(p);
		for(;;){
			char c=s.charAt(r.p);
			if(c=='*'||c=='/'){
				R q=factor(r.p+1);
				if(c=='*')
					r.val*=q.val;
				else
					r.val/=q.val;
				r.p=q.p;
			}else{
				break;
			}
		}
		return r;
	}

	R term(int p){
		if(s.charAt(p)=='('){
			R r=equation(p+1);
			r.p++; // skip ')'
			return r;
		}else{
			boolean minus=false;
			if(s.charAt(p)=='-'){
				p++;
				minus=true;
			}
			long val=0;
			for(; Character.isDigit(s.charAt(p)); p++){
				val=val*10+(s.charAt(p)-'0');
			}
			if(minus){
				val=-val;
			}
			return new R(p, val);
		}
	}

	class R{
		int p;
		long val;

		R(int p, long val){
			this.p=p;
			this.val=val;
		}
	}

	void debug(Object... os){
		System.err.println(Arrays.deepToString(os));
	}

	void print(String s){
		System.out.print(s);
	}

	void println(String s){
		System.out.println(s);
	}

	public static void main(String[] args){
		// System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
		new Main().run();
	}
}