#include <iostream>
#include <string.h>
#include <cstring>
#include <vector>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
#define ll long long
using namespace std;
int error;
string valu,res;
int isOprtr(char c)
{
if(c=='+' || c=='-')
return 1;
if(c=='*' || c=='/' ||c=='%')
return 2;
return 0;
}
//1*(2+3*23+12*34)+3 = 482
//1*(2+3*23+12*34)+3*(4+5*65-13+8-2) = 1445
//2+3*(2+2-1+3+(12+12*3)+3+4)
//1*2+3*23+12*34+3*4+5*65-13+8-2
//((20*((((((((100)))))))*100)))
int getvalu(string tmp)
{
int pos,ind;
ll num[300];
char op[300];
char newop[300];
ll v=0;
stack<ll> res;
queue<char> q;
ind = 0;
for(int i=tmp.size()-1;i>=0;i--)
{
if(isOprtr(tmp[i]))
{
v = 0;
while(isOprtr(q.front())==0 && !q.empty())
{
v*=10;
v+=(q.front()-'0');
q.pop();
}
op[ind]=tmp[i];
num[ind++]=v;
}
else
{
q.push(tmp[i]);
}
}
v = 0;
while(isOprtr(q.front())==0 && !q.empty())
{
v*=10;
v+=(q.front()-'0');
q.pop();
}
num[ind++]=v;
pos =0;
res.push(num[0]);
for(int i=0;i<ind-1;i++)
{
if(op[i]=='+')
{
res.push(num[i+1]);
newop[pos++]=op[i];
}
else if(op[i]=='-')
{
res.push(num[i+1]*(-1));
newop[pos++]='+';
}
else if(op[i]=='*')
{
v = res.top();
res.pop();
v = v*num[i+1];
res.push(v);
}
else if(op[i]=='/')
{
v = res.top();
res.pop();
v = v/num[i+1];
res.push(v);
}
else if(op[i]=='%')
{
v = res.top();
res.pop();
v = v%num[i+1];
res.push(v);
}
}
v = 0;
while(!res.empty())
{
v+=res.top();
res.pop();
}
valu = "";
while(v)
{
valu.append(1u,v%10+'0');
v/=10;
}
int last_position = valu.size()-1;
for(int i=0;i<valu.size()/2;i++)
{
char ch = valu[i];
valu[i] = valu[last_position-i];
valu[last_position-i] = ch;
}
return 0;
}
void parenthesis(string s,int parenthesis_Pair)
{
string pp="",tmp;
stack<char> stck;
for(int i=0;i<s.size();i++)
{
pp.append(1u,s[i]);
if(s[i]==')')
{
tmp = "";
while(stck.top()!='(' && !stck.empty())
{
tmp.append(1u,stck.top());
stck.pop();
}
error = getvalu(tmp);
if(error)
return;
else
pp.replace(pp.size()-1-(tmp.size()+1),(tmp.size()+3),valu);
s.replace(0,i+1,pp);
if(parenthesis_Pair)
parenthesis(s,parenthesis_Pair-1);
else
res = s;
return;
}
else
{
stck.push(s[i]);
}
}
res = pp;
}
void fincalc(string s)
{
ll v=0;
char op[300];
ll num[300];
int ind=0;
stack<ll> vlu;
for(int i=0;i<s.size();i++)
{
if(isOprtr(s[i]))
{
num[ind++]=v;
op[ind]=s[i];
v = 0;
}
else
{
v*=10;
v+=(s[i]-'0');
}
}
num[ind++]=v;
v = 0;
vlu.push(num[0]);
int oprtcnt = 0;
for(int i=1;i<ind;i++)
{
if(op[i]=='-')
{
vlu.push(num[i]*(-1)); oprtcnt+=1;
}
else if(op[i]=='+')
{
vlu.push(num[i]);oprtcnt+=1;
}
else if(op[i]=='*')
{
v = vlu.top();
vlu.pop();
vlu.push(v*num[i]);oprtcnt+=1;
}
else if(op[i]=='/')
{
v = vlu.top();
vlu.pop();
vlu.push(v/num[i]);oprtcnt+=1;
}
else if(op[i]=='%')
{
v = vlu.top();
vlu.pop();
vlu.push(v%num[i]);oprtcnt+=1;
}
}
v = 0;
while(!vlu.empty())
{
v+=vlu.top();
vlu.pop();
}
cout<<v<<"\n";
}
int main()
{
int t,num_cnt,op_cnt,cnt;
string ss,s;
cin>>t;
getchar();
while(t--)
{
getline(cin,ss);
s = "";
error = 0;
op_cnt = 0;
num_cnt = 0;
stack<char> parenth;
int ind = 0;
for(ind = 0;ind<ss.size();ind++)
{
if(isalpha(ss[ind]))
{
error = 1;
break;
}
else if(ss[ind]<='9' && ss[ind]>='0')
{
while(ss[ind]<='9' && ss[ind]>='0')
{
s.append(1u,ss[ind]);
ind+=1;
}
ind-=1;
num_cnt+=1;
}
else if(ss[ind]==' ')
{
while(ss[ind]==' ')
ind+=1;
ind-=1;
}
else if(ss[ind]=='(')
{
if(s.size())
{
if(s[s.size()-1]>='0' && s[s.size()-1]<='9')
{
error = 1;
break;
}
}
while(ss[ind]=='(')
{
s.append(1u,ss[ind]);
parenth.push('(');
ind+=1;
cnt+=1;
}
ind-=1;
}
else if(ss[ind]==')')
{
if(s.size())
{
if(isOprtr(s[s.size()-1]))
{
error = 1;
break;
}
}
s.append(1u,ss[ind]);
parenth.pop();
}
else if(isOprtr(ss[ind]))
{
if(s.size()==0)
{
error = 1;
break;
}
else
{
if(isOprtr(s[s.size()-1]))
{
error = 1;
break;
}
}
s.append(1u,ss[ind]);
op_cnt+=1;
}
}
if(parenth.size())
error = 1;
if(isOprtr(s[s.size()-1]))
error = 1;
if(num_cnt - op_cnt!=1)
error = 1;
if(error)
cout<<"ERROR\n";
else
{
parenthesis(s,cnt);
fincalc(res);
}
}
return 0;
}