#include <iostream>
using namespace std;
int n;
string str;
int start,goal;
void kake();
void tasi();
void tume(int s,int g){
  string str2="";
  for(int i = 0 ; i < str.size() ; i++){
    if(str[i] != '#'&& (s!=i || g != i))str2+=str[i];
  }
  str= str2;
}
void cal(){
  kake();
  tasi();
}
void kake(){
  for(int i = start ; i <= goal ; i++ ){
    if(str[i] == '*'){
      int t = (str[i-1] - '0')*(str[i+1] - '0'); 

      str[i-1] = str[i+1] = '#'; 
      str[i] = char(t+'0');
      tume(i-1,i+1);
      i = start;
      goal = goal-2;

    }
    if(str[i] == '/'){
      int t = (str[i-1] - '0')/(str[i+1] - '0'); 
      str[i-1] = str[i+1] = '#'; 
      str[i] = char(t+'0');
      tume(i-1,i+1);
      i = start;
      goal = goal-2;


    }
  }
}
void tasi(){
  for(int i = start ; i <= goal ; i++ ){
    if(str[i] == '+'){
      int t = (str[i-1] - '0')+(str[i+1] - '0'); 
      str[i-1] = str[i+1] = '#'; 
      str[i] = char(t+'0');
      tume(i-1,i+1);
      i = start;
      goal = goal-2;

    }
    if(str[i] == '-'){
      int t = (str[i-1] - '0')-(str[i+1] - '0'); 
      str[i-1] = str[i+1] = '#'; 
      str[i] = char(t+'0');
      tume(i-1,i+1);
      i = start;
      goal = goal-2;
    }
  }
}

int main(){
  cin >> n;
  while(n--){
    str = "";
    cin >> str;

    if(str[0] == '#')break;

    for(int i = 0 ; i < str.size() ; i++){
      if(str[i] == '(')start = i;
      if(str[i] == ')'){
	goal = i;
	cal();
	str[start] = '#';
	str[goal] = '#';
	tume(start,goal);
	i = -1;
	continue;
      }
    }
    start = 0,goal = str.size()-1;
    cal();
    int ans = str[0] - '0';
    cout << ans << endl;
  }
}