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;
  }
}