#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX 1000

// Stack to hold strings
typedef struct {
    char *arr[MAX];
    int top;
} Stack;

void init(Stack *s) {
    s->top = -1;
}

int isEmpty(Stack *s) {
    return s->top == -1;
}

void push(Stack *s, char *str) {
    if (s->top < MAX - 1) {
        s->arr[++s->top] = strdup(str);
    }
}

char* pop(Stack *s) {
    if (!isEmpty(s)) {
        return s->arr[s->top--];
    }
    return NULL;
}

// Check if operator
int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/');
}

// Check if valid char
int isValidChar(char c) {
    return (isalpha(c) || isOperator(c));
}

// Convert prefix to infix
int prefixToInfix(char *expr) {
    Stack s;
    init(&s);

    int len = strlen(expr);

    // scan right to left
    for (int i = len - 1; i >= 0; i--) {
        char c = expr[i];

        if (!isValidChar(c)) {
            printf("Invalid input\n");
            return 0;
        }

        if (isalpha(c)) {
            // Operand → push as string
            char temp[2] = {c, '\0'};
            push(&s, temp);
        } else if (isOperator(c)) {
            // Operator → pop two operands
            if (s.top < 1) {
                printf("Invalid input\n");
                return 0;
            }
            char *op1 = pop(&s);
            char *op2 = pop(&s);

            char newExpr[MAX];
            snprintf(newExpr, sizeof(newExpr), "(%s%c%s)", op1, c, op2);

            push(&s, newExpr);

            free(op1);
            free(op2);
        }
    }

    if (s.top != 0) {
        printf("Invalid input\n");
        return 0;
    }

    printf("%s\n", s.arr[s.top]);
    free(s.arr[s.top]);
    return 1;
}

int main() {
    int n;
    if (scanf("%d\n", &n) != 1 || n < 1 || n > 100) {
        printf("Invalid input\n");
        return 0;
    }

    char expr[MAX];
    for (int i = 0; i < n; i++) {
        if (!fgets(expr, sizeof(expr), stdin)) {
            printf("Invalid input\n");
            return 0;
        }
        expr[strcspn(expr, "\n")] = 0;  // remove newline
        prefixToInfix(expr);
    }

    return 0;
}