#include<iostream>
#include<stdio.h>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;
stack<char>st;
queue<char>postfix;
char arr[10];
void Clear()
{
while(!st.empty())
st.pop();
}
int main()
{
int len,a,t,i;
string str1;
scanf("%d",&t);
getchar();
getchar();
for(a=1; a<=t; a++)
{
str1+="(";
while(gets(arr))
{
if(arr[0]=='\0')
{
break;
}
str1+=arr[0];
}
str1+=')';
len = str1.length();
for(i=0; i<len; i++)
{
if(str1[i]=='(')
{
st.push(str1[i]);
}
else if(str1[i]>='0'&&str1[i]<='9')
{
postfix.push(str1[i]);
}
else if(str1[i]=='*'|str1[i]=='/')
{
while(st.size() && (st.top()=='*'|st.top()=='/'))
{
postfix.push(st.top());
st.pop();
}
st.push(str1[i]);
}
else if(str1[i]=='+'|str1[i]=='-')
{
while(st.size() && (st.top()=='+'|st.top()=='-'|st.top()=='*'|st.top()=='/'))
{
postfix.push(st.top());
st.pop();
}
st.push(str1[i]);
}
else if(str1[i]==')')
{
while(st.size() && st.top()!='(')
{
postfix.push(st.top());
st.pop();
}
st.pop();
}
}
while(!postfix.empty())
{
printf("%c",postfix.front());
postfix.pop();
}
printf("\n");
if(a<t)
{
printf("\n");
}
Clear();
str1="";
}
return 0;
}