/* Walter Guttmann */
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

#define in cin

char *sc, *sp;
string level[1<<13];
int maxl;

void rec(int l) {
  if (l > maxl)
    maxl = l;
  char ch = *sp;
  --sp;
  level[l] += ch;
  if (isupper(ch)) {
    rec(l+1);
    rec(l+1);
  }
}

int main() {
  int t;
  in >> t;
  while (t--) {
    string s;
    in >> s;
    sc = (char*)s.c_str();
    sp = sc + s.size()-1;
    maxl = 0;
    rec(0);
    assert(sp+1 == sc);
    for (int i=maxl ; i>=0 ; i--) {
      cout << level[i];
      level[i] = "";
    }
    cout << endl;
  }
  return 0;
}

