#include
using namespace std;
void push(char ch);
char pop();
int top=-1;
char infix[400];
char postfix[400],stack[400];
int p=-1;
int priority(char symbol);
int main()
{
int t;
scanf("%d",&t);
char *a=new char[t];
fflush(stdin);
for(int k=0;k<t;k++)
{
for(int i=0;i<400;i++)
{
scanf("%c",&infix[i]);
if(infix[i]=='\n')
{
infix[i]='\0';
break;
}
}
for(int i=0;infix[i]!='\0';i++)
{
char ch=infix[i];
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^'||ch=='('||ch==')')
{
if(ch=='(')
push(ch);
else if(ch==')')
{
char c;
while((c=pop())!='(')
{
p++;
postfix[p]=c;
}
}
else
{
char c;
while(priority(c=pop())>priority(ch))
{
p++;
postfix[p]=c;
}
push(c);
push(ch);
}
}
else if(ch>=97&&ch<=122)
{
p++;
postfix[p]=ch;
}
}
a[k]=new char[p+2];
for(int j=0;j<=p;j++)
{
a[k][j]=postfix[j];
if(p==j)
a[k][j+1]='\0';
}
top=-1;
p=-1;
}
printf("\n");
for(int k=0;k<t;k++)
{
for(int j=0;a[k][j]!='\0';j++)
printf("%c",a[k][j]);
printf("\n");
}
return 0;
}
void push(char ch)
{
top++;
stack[top]=ch;
}
char pop()
{
if(top==-1)
return -1;
char ch;
ch=stack[top];
top--;
return ch;
}
int priority(char ch)
{
if(ch=='+'||ch=='-')
return 1;
if(ch=='(')
return 0;
if(ch=='*'||ch=='/')
return 2;
if(ch=='^')
return 3;
}