#!/usr/bin/node
// Generated by CoffeeScript 1.3.3
// manually optimized. Base for CodeIQ341.
(function() {
  var a=[];
  var stdin = process.openStdin();
  stdin.setEncoding('utf8');

  var proc = function(s){
    var bidx=s.indexOf('('),count,eidx,m;
    while(bidx!=-1){
      eidx=bidx+1;
      for(count=1;count;eidx++){
        if(s[eidx]=='(')count++;
        if(s[eidx]==')')count--;
      }
      s=s.substring(0,bidx)+proc(s.substring(bidx+1,eidx-1))+s.substring(eidx);
      bidx=s.indexOf('(');
    }
    while(m = /^(.*?)(-?\d+)([*/])(-?\d+)(.*)$/.exec(s)){
      if (m[3] == '*') {
        s = m[1] + (parseInt(m[2]) * parseInt(m[4])) + m[5];
      } else {
        s = m[1] + (parseInt(m[2]) / parseInt(m[4]) ^ 0) + m[5];
      }
    }
    while(m = /^(.*?)(-?\d+)([+Z])(-?\d+)(.*)$/.exec(s)){
      if (m[3] == '+') {
        s = m[1] + (parseInt(m[2]) + parseInt(m[4])) + m[5];
      } else {
        s = m[1] + (parseInt(m[2]) - parseInt(m[4])) + m[5];
      }
    }
    return s;
  };

  stdin.on('data', function(input) {
    var i=0,len,ref,x=[];
    ref=input.split("\n");
    for(len=ref.length;i<len;i++)x.push(ref[i]);
    if(x.length>2||!isNaN(x[1]))a=x;
    else a.push(x[0]);
  });

  stdin.on('end', function(z) {
    var i,n,s;
    n = parseInt(a[0]);
    for(i=1;i<=n;i++){
      s=a[i].substring(0,a[i].indexOf('=')).replace(/-/g,'Z');
      console.log(proc(s)));
    }
  });

}).call(this);