using namespace std;
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
typedef long long ll;
typedef pair<int,int> pii;
#define FOR(i,n) for (int i = 0; i < n; i++)
#define SZ(x) ((int)x.size())
#define PB push_back
#define sf(x) scanf("%d",&x)
#define pf(x) printf("%d\n",x)
#define split(str) {vs.clear();istringstream ss(str);while(ss>>(str))vs.push_back(str);}
struct node
{
bool type;
node *left;
string op;
node *right;
node(string c, node *pl, node *pr) : op(c), left(pl), right(pr), type(false) {}
node(string c) : op(c), type(true) {}
};
int main()
{
int t;
sf(t);
while(t--)
{
string s;
cin>>s;
//getline(cin, s);
stack<node*> mystack;
FOR(i,s.size())
{
if(s[i]>='A'&&s[i]<='Z')
{
node *r=mystack.top();
mystack.pop();
node *l=mystack.top();
mystack.pop();
char buf[10];
sprintf(buf,"%c",s[i]);
mystack.push(new node((string)buf,l,r));
continue;
}
else
{
char buf[10];
sprintf(buf,"%c",s[i]);
mystack.push(new node((string)buf));
}
}
//cout<<"hello"<<endl;
queue<node*> q;
q.push(mystack.top());
string ans="";
while(!q.empty())
{
struct node *n=q.front();
q.pop();
if(n->type)
ans=n->op+ans;
else
{
ans=n->op+ans;
q.push(n->left);
q.push(n->right);
}
}
char *fileName = (char*)ans.c_str();
printf("%s\n",fileName);
//cout<<ans<<endl;
}
}
dXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxxdWV1ZT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKI2RlZmluZSBhbGwoYykgKGMpLmJlZ2luKCksKGMpL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