#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
#define el endl
#define int int64_t
#define fi first
#define se second
#define file(x) freopen(x".inp", "r", stdin); freopen(x".out", "w", stdout)
#define pb push_back
#define gd() ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define rizzler signed main()
#define tc() int t; cin >> t; while(t--)
void tle() {
    double time_taken = 1000.0 * clock() / CLOCKS_PER_SEC;
    
    if (time_taken < 1000.0) {
        std::cerr << "Time: ";
        std::cerr << time_taken << "ms" << std::string(27, '\t');
    } else {
        std::cerr << "TLE Warning!\n";
        std::cerr << "Time: ";
        std::cerr << (time_taken / 1000.0) << "s" << std::string(27, '\n');
    }
}




rizzler{
    gd();
    tc(){
        int n; cin >> n;
        string s; getline(cin >> ws, s);
        vector<string> v;
        stack<int> st;
        stringstream ss(s);
        string word;
        while(ss >> word){
            v.pb(word);
        }
        for (int i = 0; i < v.size(); i++){
            if (isdigit(v[i].back())){
                st.push(stoll(v[i]));
            } else {
                auto top2 = st.top(); st.pop();
                auto top1 = st.top(); st.pop();
                if (v[i] == "+"){
                    st.push(top1 + top2);
                } else if (v[i] == "-"){
                    st.push(top1 - top2);
                } else if (v[i] == "*"){
                    st.push(top1*top2);
                } else {
                    st.push(top1/top2);
                }
            }
        }
        cout << st.top() << el;

    }


    tle();
    
}
