{
    int n;
    if (scanf("%d\n", &n) != 1) {
        printf("Invalid input\n");
        return 0;
    }

    for (int t = 0; t < n; t++) {
        char expr[MAX];
        if (!fgets(expr, MAX, stdin)) {
            printf("Invalid input\n");
            continue;
        }

        top = -1;
        int valid = 1;

        for (int i = 0; expr[i] != '\0' && expr[i] != '\n'; i++) {
            char c = expr[i];

            if (isdigit(c)) {
                stack[++top] = c - '0';  
            } 
            else if (c == '+' || c == '-' || c == '*' || c == '/') {
                if (top < 1) {  
                    valid = 0;
                    break;
                }

                int b = stack[top--];  
                int a = stack[top--];  

                int res;
                if (c == '+') res = a + b;
                else if (c == '-') res = a - b;
                else if (c == '*') res = a * b;
                else {
                    if (b == 0) { 
                        valid = 0;
                        break;
                    }
                    res = a / b;
                }
                stack[++top] = res;  
            }
            else {
                valid = 0;  
                break;
            }
        }
        if (!valid || top != 0) {
            printf("Invalid input\n");
        } else {
            printf("%d\n", stack[top]);
        }
    }
    return 0;
}