#include <iostream>
#include <string>
#include <vector>
using namespace std;
void get_levels(string& Expr, int& index, int depth, vector<string>& Answer) {
Answer[depth] += Expr[index];
index--;
if (isupper(Expr[index+1])) {
get_levels(Expr, index, depth + 1, Answer);
get_levels(Expr, index, depth + 1, Answer);
}
}
int main() {
int n, index;
cin >> n;
string Expr;
for (int k = 0; k < n; k++) {
cin >> Expr;
vector<string> Answer(Expr.size(), "");
index = Expr.size() - 1;
get_levels(Expr, index, 0, Answer);
for (int k = Expr.size() - 1; k >= 0; k--)
cout << Answer[k];
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAp2b2lkIGdldF9sZXZlbHMoc3RyaW5nJiBFeHByLCBpbnQmIGluZGV4LCBpbnQgZGVwdGgsIHZlY3RvcjxzdHJpbmc+JiBBbnN3ZXIpIHsKCUFuc3dlcltkZXB0aF0gKz0gRXhwcltpbmRleF07CglpbmRleC0tOwoJaWYgKGlzdXBwZXIoRXhwcltpbmRleCsxXSkpIHsKCQlnZXRfbGV2ZWxzKEV4cHIsIGluZGV4LCBkZXB0aCArIDEsIEFuc3dlcik7CgkJZ2V0X2xldmVscyhFeHByLCBpbmRleCwgZGVwdGggKyAxLCBBbnN3ZXIpOwoJfQp9CgoKaW50IG1haW4oKSB7CglpbnQgbiwgaW5kZXg7CgljaW4gPj4gbjsKCXN0cmluZyBFeHByOwoJZm9yIChpbnQgayA9IDA7IGsgPCBuOyBrKyspIHsKCQljaW4gPj4gRXhwcjsJCQkJCQkKCQl2ZWN0b3I8c3RyaW5nPiBBbnN3ZXIoRXhwci5zaXplKCksICIiKTsKCQlpbmRleCA9IEV4cHIuc2l6ZSgpIC0gMTsKCQlnZXRfbGV2ZWxzKEV4cHIsIGluZGV4LCAwLCBBbnN3ZXIpOwoJCWZvciAoaW50IGsgPSBFeHByLnNpemUoKSAtIDE7IGsgPj0gMDsgay0tKQoJCQljb3V0IDw8IEFuc3dlcltrXTsKCQljb3V0IDw8IGVuZGw7Cgl9CglyZXR1cm4gMDsKfQ==