#include <bits/stdc++.h>
#define ll long long
#define tatlem "test"
#define MOD 2021
#define enl endl
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
int t;
cin >> t;
while(t--)
{
string s;
cin >> s;
stack<char> st;
for (char c : s)
{
if (isalnum(c))
{
cout << c;
}
else if (c == '(')
{
st.push(c);
}
else if (c == ')')
{
while (!st.empty() && st.top() != '(')
{
cout << st.top();
st.pop();
}
if (!st.empty() && st.top() == '(')
{
st.pop();
}
}
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^')
{
int gt;
if (c == '+' || c == '-') gt = 1;
else if (c == '*' || c == '/') gt = 2;
else if (c == '^') gt = 3;
while (!st.empty())
{
int gtt;
if (st.top() == '+' || st.top() == '-') gtt = 1;
else if (st.top() == '*' || st.top() == '/') gtt = 2;
else if (st.top() == '^') gtt = 3;
if (st.top() != '(' && gtt >= gt)
{
cout << st.top();
st.pop();
}
else
{
break;
}
}
st.push(c);
}
}
while (!st.empty())
{
cout << st.top();
st.pop();
}
cout << enl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgdGF0bGVtICJ0ZXN0IgojZGVmaW5lIE1PRCAyMDIxCiNkZWZpbmUgZW5sIGVuZGwKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjb3V0LnRpZSgwKTsKICAgIGNpbi50aWUoMCk7CiAgICBmcmVvcGVuKCJ0ZXN0LmlucCIsInIiLHN0ZGluKTsKICAgIGZyZW9wZW4oInRlc3Qub3V0IiwidyIsc3Rkb3V0KTsKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgc3RyaW5nIHM7CiAgICAgICAgY2luID4+IHM7CiAgICAgICAgc3RhY2s8Y2hhcj4gc3Q7CiAgICAgICAgZm9yIChjaGFyIGMgOiBzKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGlzYWxudW0oYykpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvdXQgPDwgYzsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChjID09ICcoJykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgc3QucHVzaChjKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChjID09ICcpJykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgd2hpbGUgKCFzdC5lbXB0eSgpICYmIHN0LnRvcCgpICE9ICcoJykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBjb3V0IDw8IHN0LnRvcCgpOwogICAgICAgICAgICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKCFzdC5lbXB0eSgpICYmIHN0LnRvcCgpID09ICcoJykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBzdC5wb3AoKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChjID09ICcrJyB8fCBjID09ICctJyB8fCBjID09ICcqJyB8fCBjID09ICcvJyB8fCBjID09ICdeJykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaW50IGd0OwogICAgICAgICAgICAgICAgaWYgKGMgPT0gJysnIHx8IGMgPT0gJy0nKSBndCA9IDE7CiAgICAgICAgICAgICAgICBlbHNlIGlmIChjID09ICcqJyB8fCBjID09ICcvJykgZ3QgPSAyOwogICAgICAgICAgICAgICAgZWxzZSBpZiAoYyA9PSAnXicpIGd0ID0gMzsKICAgICAgICAgICAgICAgIHdoaWxlICghc3QuZW1wdHkoKSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpbnQgZ3R0OwogICAgICAgICAgICAgICAgICAgIGlmIChzdC50b3AoKSA9PSAnKycgfHwgc3QudG9wKCkgPT0gJy0nKSBndHQgPSAxOwogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHN0LnRvcCgpID09ICcqJyB8fCBzdC50b3AoKSA9PSAnLycpIGd0dCA9IDI7CiAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAoc3QudG9wKCkgPT0gJ14nKSBndHQgPSAzOwogICAgICAgICAgICAgICAgICAgIGlmIChzdC50b3AoKSAhPSAnKCcgJiYgZ3R0ID49IGd0KQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCBzdC50b3AoKTsKICAgICAgICAgICAgICAgICAgICAgICAgc3QucG9wKCk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHN0LnB1c2goYyk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgd2hpbGUgKCFzdC5lbXB0eSgpKQogICAgICAgIHsKICAgICAgICAgICAgY291dCA8PCBzdC50b3AoKTsKICAgICAgICAgICAgc3QucG9wKCk7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZW5sOwogICAgfQogICAgcmV0dXJuIDA7Cn0K