#include<stdio.h>
char buf[256];
int idx;
int stack[100];
int sp;
#define push(type,x) (stack[sp++]=(type<<8)|x)
#define pop() (stack[--sp]&0xff)
#define toptype() (stack[sp-1]>>8)

int gettoken(char a[]){
    while(buf[idx]==' ')idx++;
    if(buf[idx]=='\0') return 0;
    int i;
    for(i=0;;){
        switch(buf[idx]){
        case'\0':
        case' ':
            a[i]='\0';
            return 4;
        case'+': case'-':
            if(i==0){
                a[i++]=buf[idx++];
                a[i]='\0';
                return 2;
            }
            a[i]='\0';
            return 4;
        case'*': case'/': case'%':
            if(i==0){
                a[i++]=buf[idx++];
                a[i]='\0';
                return 1;
            }
            a[i]='\0';
            return 4;
        case'(': case')':
            if(i==0){
                a[i++]=buf[idx++];
                a[i]='\0';
                return 3;
            }
            a[i]='\0';
            return 4;
        default:
            a[i++]=buf[idx++];
            }
        }
    }

void popto(int type){
    int i;
    while(toptype()!=type){
        printf("%c ",pop());
    }
    pop();
}
int main(){
    int a,b;
    scanf("%d",&a);
    for(b=0;b<=a;b++){
    gets(buf);
    idx=0;
    int i,type;
    char a[64];
    sp=0;
    push(3,'(');
    for(i=1;(type=gettoken(a));i++){
        switch(type){
    case 1:
    case 2:
        while(toptype()<=type)
            printf("%c ",pop());
        push(type, a[0]);
        break;
    case 3:
        if(a[0]=='(')
            push(type,a[0]);
        else popto(type);
        break;
    case 4:

        printf("%s",a);
if(a[0]!='\0'){printf(" ");}
          break;


        }
    }
    popto(3);
    if(b>=1)printf("\n");}

    return 0;

}