#include <stdlib.h>
#include <stdio.h>
int postf(char*);
int priority(char);

int main()
{
    int a,b;
    char in[100];
    scanf("%d",&a);
    for(b=1;b<=a;b++){

    scanf("%s",in);

    postf(in);
    }
}
int postf(char* fix) {
    int i = 0, top = 0;
    char stack[100]={'\0'};
    char op;

    while(1) {

        op=fix[i];

     switch(op) {
            case '\0':
            printf(" ");
                while(top > 0) {
                    printf("%c", stack[top]);
                    top--;
                }

                printf("\n");
                return 0;

            case '(':
                if(top<(sizeof(stack) / sizeof(char))) {
                    top++;
                    stack[top]=op;
                }
                break;
            case '+':
            case '-':
            case '*':
            case '/':
                printf(" ");
                while(priority(stack[top]) >= priority(op)) {
                    printf("%c ", stack[top]);
                    top--;
                }

                if(top < (sizeof(stack) / sizeof(char))) {
                    top++;
                    stack[top] = op;
                }
                break;

            case ')':
                while(stack[top] != '(') {
                    printf("%c",stack[top]);
                    top--;
                }
                top--;
                break;
            default:
                printf("%c",op);


                break;

}

        i++;


    }


}

int priority(char op) {
    int a;

    switch(op) {

        case '+':
        case '-':
            a = 1;
            break;

        case '*':
        case '/':
            a = 2;
            break;

        default:
            a = 0;
            break;
    }

    return a;
}
