#include <bits/stdc++.h>
using namespace std;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int tt;
    cin >> tt;
    auto prec = [](char c) {
        return (c == '+' || c == '-' ? 1 : 2);
    };
    while (tt--) {
        string str;
        cin >> str;
        string ans = "";
        stack<char> p;
        for (auto c : str) {
            if (isalpha(c) || isdigit(c))
                ans += c;
            else if (c == '(' || c == '^')
                p.push(c);
            else if (c == ')') {
                while (!p.empty() && p.top() != '(') {
                    ans += p.top();
                    p.pop();
                }
                if (!p.empty())
                    p.pop();
            } else {
                while (!p.empty() && p.top() != '(' && prec(c) <= prec(p.top())) {
                    ans += p.top();
                    p.pop();
                }
                p.push(c);
            }
        }
        while (!p.empty()) {
            ans += p.top();
            p.pop();
        }
        cout << ans << "\n";
    }
}