#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 100

char* stack[MAX];
int top;


void push(char* str) {
    stack[++top] = str;
}


char* pop() {
    if (top < 0) return NULL;
    return stack[top--];
}


int isOperand(char ch) {
    return isalpha(ch);
}


int isOperator(char ch) {
    return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}


char* convert(char* expr) {
    top = -1; 
    int len = strlen(expr);

    for (int i = len - 1; i >= 0; i--) {
        char ch = expr[i];

        if (isOperand(ch)) {
            char* operand = (char*)malloc(2);
            operand[0] = ch;
            operand[1] = '\0';
            push(operand);
        } else if (isOperator(ch)) {
            if (top < 1) return strdup("Invalid input");
            char* op1 = pop();
            char* op2 = pop();

            int size = strlen(op1) + strlen(op2) + 4;
            char* newExpr = (char*)malloc(size);
            snprintf(newExpr, size, "(%s%c%s)", op1, ch, op2);
            push(newExpr);
        } else {
            return strdup("Invalid input");
        }
    }

    if (top != 0) return strdup("Invalid input");
    return stack[top];
}

int main() {
    int n;
    scanf("%d", &n);
    getchar(); 

    for (int i = 0; i < n; i++) {
        char expr[MAX];
        fgets(expr, MAX, stdin);
        expr[strcspn(expr, "\n")] = '\0'; 

        char* result = convert(expr);
        printf("%s\n", result);

        for (int j = 0; j <= top; j++) {
            free(stack[j]);
        }
    }

    return 0;
}