#include <stdio.h>
#include <stack>
using namespace std;
stack<char>sta;
int main()
{
int T;
scanf("%d",&T);
getchar();
while(T--)
{
char t,ch;
sta.push('(');
while(true)
{
scanf("%c",&t);
ch=t;
if(ch=='\n')
{
ch=')';
}
if(ch>='0'&&ch<='9')
{
printf("%c",ch);
}
else if(ch=='(')
{
sta.push(ch);
}
else if(ch==')')
{
while(sta.top()!='(')
{
printf("%c",sta.top());
sta.pop();
}
sta.pop();
}
else if(ch=='+' || ch=='-')
{
while(sta.top()!='(')
{
printf("%c",sta.top());
sta.pop();
}
sta.push(ch);
}
else
{
if(sta.top()=='*' || sta.top()=='/')
{
printf("%c",sta.top());
sta.pop();
}
sta.push(ch);
}
if(t=='\n')
break;
}
printf("\n");
}
return 0;
}