#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX 100

int stack[MAX];
int top = -1;

// Push to stack
void push(int val) {
    if (top < MAX - 1)
        stack[++top] = val;
}

// Pop from stack
int pop() {
    if (top >= 0)
        return stack[top--];
    return -1; // should not happen in valid case
}

// Check valid operator
int isOperator(char ch) {
    return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}

// Evaluate single postfix expression
int evaluate(char* expr) {
    top = -1; // reset stack for each expression

    for (int i = 0; expr[i]; i++) {
        char ch = expr[i];

        if (isdigit(ch)) {
            push(ch - '0');  // Convert char to int
        } else if (isOperator(ch)) {
            if (top < 1) return -9999; // Not enough operands

            int b = stack[top--];  // RHS operand
            int a = stack[top--];  // LHS operand

            int res;
            if (ch == '+') res = a + b;
            else if (ch == '-') res = a - b;
            else if (ch == '*') res = a * b;
            else if (ch == '/') {
                if (b == 0) return -9999; // Division by zero
                res = a / b;
            }

            push(res);
        } else {
            return -9999; // Invalid character
        }
    }

    if (top != 0) return -9999; // Extra operands

    return stack[top--];
}

int main() {
    int n;
    char expr[MAX];

    if (scanf("%d", &n) != 1 || n < 1 || n > 100) {
        printf("Invalid input\n");
        return 0;
    }
    getchar(); // Consume newline after number

    for (int i = 0; i < n; i++) {
        if (!fgets(expr, sizeof(expr), stdin)) {
            printf("Invalid input\n");
            continue;
        }

        // Remove newline if exists
        expr[strcspn(expr, "\n")] = '\0';

        int result = evaluate(expr);
        if (result == -9999) {
            printf("Invalid input\n");
        } else {
            printf("%d\n", result);
        }
    }

    return 0;
}