#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
size_t i;
int n, run=0;
bool first=true;
string str;
cin >> n;
getchar();
getline(cin, str);
while(run < n){
vector<string> eq;
stack<char> op;
if(!first) cout << endl;
first=false;
while(getline(cin, str) && str.size()!=0){
eq.push_back(str);
}
for(i=0 ; i<eq.size() ; ++i){
if(eq[i][0]=='('){
op.push(eq[i][0]);
}
else if(eq[i][0]==')'){
while(op.top()!='('){
cout << op.top();
op.pop();
}
op.pop();
if(!op.empty()){
if(op.top()=='*'||op.top()=='/'){
cout << op.top();
op.pop();
}
}
}
else if(eq[i][0]<='9' && eq[i][0]>='0'){
cout << eq[i];
}
else{
if(op.empty() || (!op.empty() && op.top()=='(')){
op.push(eq[i][0]);
}
else{
if(eq[i][0]=='+'||eq[i][0]=='-'){
if(op.top()=='*'||op.top()=='/'){
cout << op.top();
op.pop();
if(!op.empty()){
cout << op.top();
op.pop();
}
}
else{
cout << op.top();
op.pop();
}
op.push(eq[i][0]);
}
else if(eq[i][0]=='*'||eq[i][0]=='/'){
if(!op.empty() && (op.top()=='+'||op.top()=='-')){
op.push(eq[i][0]);
}
else{
cout << op.top();
op.pop();
op.push(eq[i][0]);
}
}
}
}
}
while(!op.empty()){
cout << op.top();
op.pop();
}
cout << endl;
run++;
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxmc3RyZWFtPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxzdGFjaz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCl7CgogICAgI2lmbmRlZiBPTkxJTkVfSlVER0UKICAgICAgICBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgICNlbmRpZgoKICAgIHNpemVfdCBpOwogICAgaW50IG4sIHJ1bj0wOwogICAgYm9vbCBmaXJzdD10cnVlOwogICAgc3RyaW5nIHN0cjsKICAgIGNpbiA+PiBuOwoKICAgIGdldGNoYXIoKTsKICAgIGdldGxpbmUoY2luLCBzdHIpOwoKICAgIHdoaWxlKHJ1biA8IG4pewogICAgICAgIHZlY3RvcjxzdHJpbmc+IGVxOwogICAgICAgIHN0YWNrPGNoYXI+IG9wOwoKICAgICAgICBpZighZmlyc3QpIGNvdXQgPDwgZW5kbDsKICAgICAgICBmaXJzdD1mYWxzZTsKCiAgICAgICAgd2hpbGUoZ2V0bGluZShjaW4sIHN0cikgJiYgc3RyLnNpemUoKSE9MCl7CiAgICAgICAgICAgIGVxLnB1c2hfYmFjayhzdHIpOwogICAgICAgIH0KCiAgICAgICAgZm9yKGk9MCA7IGk8ZXEuc2l6ZSgpIDsgKytpKXsKCiAgICAgICAgICAgIGlmKGVxW2ldWzBdPT0nKCcpewogICAgICAgICAgICAgICBvcC5wdXNoKGVxW2ldWzBdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKGVxW2ldWzBdPT0nKScpewogICAgICAgICAgICAgICAgd2hpbGUob3AudG9wKCkhPScoJyl7CiAgICAgICAgICAgICAgICAgICAgY291dCA8PCBvcC50b3AoKTsKICAgICAgICAgICAgICAgICAgICBvcC5wb3AoKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIG9wLnBvcCgpOwogICAgICAgICAgICAgICAgaWYoIW9wLmVtcHR5KCkpewogICAgICAgICAgICAgICAgICAgIGlmKG9wLnRvcCgpPT0nKid8fG9wLnRvcCgpPT0nLycpewogICAgICAgICAgICAgICAgICAgICAgICBjb3V0IDw8IG9wLnRvcCgpOwogICAgICAgICAgICAgICAgICAgICAgICBvcC5wb3AoKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZihlcVtpXVswXTw9JzknICYmIGVxW2ldWzBdPj0nMCcpewogICAgICAgICAgICAgICAgY291dCA8PCBlcVtpXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgaWYob3AuZW1wdHkoKSB8fCAoIW9wLmVtcHR5KCkgJiYgb3AudG9wKCk9PScoJykpewogICAgICAgICAgICAgICAgICAgb3AucHVzaChlcVtpXVswXSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgIGlmKGVxW2ldWzBdPT0nKyd8fGVxW2ldWzBdPT0nLScpewogICAgICAgICAgICAgICAgICAgICAgICBpZihvcC50b3AoKT09JyonfHxvcC50b3AoKT09Jy8nKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgb3AudG9wKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvcC5wb3AoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKCFvcC5lbXB0eSgpKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3V0IDw8IG9wLnRvcCgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9wLnBvcCgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3V0IDw8IG9wLnRvcCgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3AucG9wKCk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgb3AucHVzaChlcVtpXVswXSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYoZXFbaV1bMF09PScqJ3x8ZXFbaV1bMF09PScvJyl7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKCFvcC5lbXB0eSgpICYmIChvcC50b3AoKT09JysnfHxvcC50b3AoKT09Jy0nKSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBvcC5wdXNoKGVxW2ldWzBdKTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCBvcC50b3AoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9wLnBvcCgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgb3AucHVzaChlcVtpXVswXSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd2hpbGUoIW9wLmVtcHR5KCkpewogICAgICAgICAgICBjb3V0IDw8IG9wLnRvcCgpOwogICAgICAgICAgICBvcC5wb3AoKTsKICAgICAgICB9CiAgICAgICAgY291dCA8PCBlbmRsOwogICAgICAgIHJ1bisrOwogICAgfQogICAgcmV0dXJuIDA7Cn0K