#include <iostream>
#include<stack>
using namespace std;
bool isoperand(char j);
bool isoperator(char j);
int precedence(char j);
int main() {
int t=0;
cin>>t;
stack<char> mystack;
for(int i=0;i<t;i++){
string infix;
string postfix;
cin>>infix;
for(int j=0;j<infix.size();j++){
char currentelement=infix[j];
if(currentelement=='(')mystack.push('(');
if(isoperand(currentelement))postfix=postfix+currentelement;
if(isoperator(currentelement)){
while(!mystack.empty()&&(precedence(mystack.top())>=precedence(currentelement))&&mystack.top()!='('){
postfix=postfix+mystack.top();
mystack.pop();
}
mystack.push(currentelement);
}
if(currentelement==')'){
while(mystack.top()!='('){postfix+=mystack.top();
mystack.pop();
}
mystack.pop();
}
}
while(!mystack.empty()){
if(mystack.top()!='('&&mystack.top()!=')')
postfix+=mystack.top();
mystack.pop();
}
cout<<postfix<<endl;
}
// your code goes here
return 0;
}
bool isoperand(char j){
if(j>='a'&&j<='z')return true;
return false;
}
bool isoperator(char j){
return (j=='+'||j=='-'||j=='*'||j=='/'||j=='^');
}
int precedence(char j){
switch(j){
case '+':
return 1;
case '-':
return 2;
case '*':
return 3;
case '/':
return 4;
case '^':
return 5;
case ')':
return 6;
case '(':
return 6;
default:
return 0;
}
}