#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_LEN 110
#define MAX_STACK 1100

typedef struct {
    char items[MAX_STACK][MAX_LEN];
    int top;
} Stack;

void init(Stack *s) {
    s->top = -1;
}

int isEmpty(Stack *s) {
    return s->top == -1;
}

int push(Stack *s, const char *str) {
    if (s->top >= MAX_STACK - 1) return 0;
    s->top++;
    strcpy(s->items[s->top], str);
    return 1;
}

int pop(Stack *s, char *str) {
    if (s->top == -1) return 0;
    strcpy(str, s->items[s->top]);
    s->top--;
    return 1;
}

int isOperator(char c) {
    return (c == '+' || c == '-' || c == '*' || c == '/');
}

int isOperand(char c) {
    return (isalpha(c));
}

int main() {
    int n;
    if (scanf("%d", &n) != 1) {
        printf("Invalid input\n");
        return 0;
    }
    getchar(); // consume newline after n

    for (int i = 0; i < n; i++) {
        char expr[MAX_LEN];
        if (!fgets(expr, sizeof(expr), stdin)) {
            printf("Invalid input\n");
            continue;
        }
        // Remove trailing newline
        int len = strlen(expr);
        if (len > 0 && expr[len - 1] == '\n') expr[len - 1] = '\0';

        if (strlen(expr) == 0) {
            printf("Invalid input\n");
            continue;
        }

        // Validate characters - only operators and alphabets allowed
        int valid = 1;
        for (int j = 0; expr[j] != '\0'; j++) {
            if (!isOperator(expr[j]) && !isOperand(expr[j])) {
                valid = 0;
                break;
            }
        }
        if (!valid) {
            printf("Invalid input\n");
            continue;
        }

        Stack stack;
        init(&stack);

        // Process from right to left
        for (int j = strlen(expr) - 1; j >= 0; j--) {
            char c = expr[j];
            if (isOperand(c)) {
                char op[2] = {c, '\0'};
                if (!push(&stack, op)) {
                    valid = 0;
                    break;
                }
            } 
            else if (isOperator(c)) {
                char op1[MAX_LEN], op2[MAX_LEN];
                if (!pop(&stack, op1) || !pop(&stack, op2)) {
                    valid = 0; // not enough operands
                    break;
                }
                char combined[MAX_LEN];
                snprintf(combined, sizeof(combined), "(%s%c%s)", op1, c, op2);
                if (!push(&stack, combined)) {
                    valid = 0;
                    break;
                }
            } 
            else {
                valid = 0;
                break;
            }
        }

        // After processing, stack should contain exactly one element
        if (!valid || stack.top != 0) {
            printf("Invalid input\n");
        } else {
            printf("%s\n", stack.items[stack.top]);
        }
    }

    return 0;
}