#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
int evaluate(char *exp)
{
    int stack[MAX],top=-1;
    for(int i=0;exp[i]!='\0';i++)
    {
        char c = exp[i];
        if(isdigit(c)) 
        {
            stack[++top]=c-'0';
        }
        else if(c=='+' || c=='-' || c=='*' || c=='/')
        {
            if(top < 1)
            {
                return -99999;
            }
            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) return -99999;
                res=a/b;
            }
            stack[++top]=res;
        }
        else 
        {
            return -99999;
        }
    }
    if(top!=0) return -99999;
    return stack[top];
}
int main()
{
    int n;
    scanf("%d",&n);
    char exp[105];
    for(int i=0;i<n;i++)
    {
        scanf("%s",exp);
        int result=evaluate(exp);
        if(result == -99999)
            printf("Invalid input\n");
        else
            printf("%d\n",result);
    }
    return 0;
}