#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 1000

// Stack structure for operators
typedef struct {
    char data[MAX];
    int top;
} Stack;

void initStack(Stack* stack) {
    stack->top = -1;
}

void push(Stack* stack, char c) {
    if (stack->top < MAX - 1) {
        stack->data[++stack->top] = c;
    }
}

char pop(Stack* stack) {
    if (stack->top >= 0) {
        return stack->data[stack->top--];
    }
    return -1;
}

char peek(Stack* stack) {
    if (stack->top >= 0) {
        return stack->data[stack->top];
    }
    return -1;
}

int isOperator(char c) {
    return c == '+' || c == '-' || c == '*' || c == '/';
}

int precedence(char c) {
    switch (c) {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        default:
            return 0;
    }
}

void infixToPostfix(char* expression) {
    Stack stack;
    initStack(&stack);
    int len = strlen(expression);
    for (int i = 0; i < len; i++) {
        char c = expression[i];
        if (isspace(c)) continue;
        if (isdigit(c)) {
            printf("%c ", c);
            while (i + 1 < len && isdigit(expression[i + 1])) {
                printf("%c", expression[++i]);
            }
            printf(" ");
        } else if (c == '(') {
            push(&stack, c);
        } else if (c == ')') {
            while (peek(&stack) != '(' && stack.top >= 0) {
                printf("%c ", pop(&stack));
            }
            if (peek(&stack) == '(') pop(&stack);
        } else if (isOperator(c)) {
            while (stack.top >= 0 && precedence(peek(&stack)) >= precedence(c)) {
                printf("%c ", pop(&stack));
            }
            push(&stack, c);
        } else {
            printf("Invalid input\n");
            return;
        }
    }
    while (stack.top >= 0) {
        printf("%c ", pop(&stack));
    }
    printf("\n");
}

int main() {
    int n;
    scanf("%d", &n);
    getchar(); // Consume newline
    for (int i = 0; i < n; i++) {
        char expression[MAX];
        fgets(expression, MAX, stdin);
        expression[strcspn(expression, "\n")] = 0; // Remove newline
        infixToPostfix(expression);
    }
    return 0;
}