#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_SIZE 100
typedef struct {
    char data[MAX_SIZE][MAX_SIZE];
    int top;
} Stack;
void initialize(Stack *s) {
    s->top = -1;
}
int isEmpty(Stack *s) {
    return s->top == -1;
}
int isFull(Stack *s) {
    return s->top == MAX_SIZE - 1;
}
void push(Stack *s, const char *item) {
    if (isFull(s)) {
        printf("Stack Overflow\n");
        return;
    }
    s->top++;
    strcpy(s->data[s->top], item);
}
char* pop(Stack *s) {
    if (isEmpty(s)) {
        printf("Stack Underflow\n");
        return NULL;
    }
    return s->data[s->top--];
}
int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/');
}
void prefixToInfix(char *prefix) {
    Stack s;
    initialize(&s);
    int len = strlen(prefix)
    for (int i = len - 1; i >= 0; i--) {
        char current_char = prefix[i];
        if (isalpha(current_char)) { 
        }
            char operand[2];
            operand[0] = current_char;
            operand[1] = '\0';
            push(&s, operand);
        } else if (isOperator(current_char)) { 
            if (isEmpty(&s)) {
                printf("Invalid input\n");
                return;
            }
            char* op1 = pop(&s);
            if (isEmpty(&s)) {
                printf("Invalid input\n"); 
                return;
            }
            char* op2 = pop(&s);

            char result[MAX_SIZE];
            sprintf(result, "(%s%c%s)", op1, current_char, op2);
            push(&s, result);
        } else { 
            printf("Invalid input\n");
            return;
        }
    }
    if (s.top == 0) { 
        printf("%s\n", pop(&s));
    } else {
        printf("Invalid input\n"); 
    }
}
int main() {
    int n;
    scanf("%d", &n);

    char prefix_expr[MAX_SIZE];
    for (int i = 0; i < n; i++) {
        scanf("%s", prefix_expr); 
        prefixToInfix(prefix_expr);
    }

    return 0;
}