#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;
}
