#include <iostream>
#include <deque>
#include <string>
using namespace std;
string processKeys(const string& keys) {
deque<char> text;
auto cursor = text.begin();
for (char key : keys) {
if (key == '[') {
cursor = text.begin();
} else if (key == ']') {
cursor = text.end();
} else {
text.insert(cursor, key);
}
}
return string(text.begin(), text.end());
}
int main() {
int testCases;
cin >> testCases;
cin.ignore();
for (int i = 0; i < testCases; ++i) {
string keys;
getline(cin, keys);
cout << processKeys(keys) << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZGVxdWU+CiNpbmNsdWRlIDxzdHJpbmc+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RyaW5nIHByb2Nlc3NLZXlzKGNvbnN0IHN0cmluZyYga2V5cykgewogICAgZGVxdWU8Y2hhcj4gdGV4dDsKICAgIGF1dG8gY3Vyc29yID0gdGV4dC5iZWdpbigpOwoKICAgIGZvciAoY2hhciBrZXkgOiBrZXlzKSB7CiAgICAgICAgaWYgKGtleSA9PSAnWycpIHsKICAgICAgICAgICAgY3Vyc29yID0gdGV4dC5iZWdpbigpOwogICAgICAgIH0gZWxzZSBpZiAoa2V5ID09ICddJykgewogICAgICAgICAgICBjdXJzb3IgPSB0ZXh0LmVuZCgpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHRleHQuaW5zZXJ0KGN1cnNvciwga2V5KTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIHN0cmluZyh0ZXh0LmJlZ2luKCksIHRleHQuZW5kKCkpOwp9CgppbnQgbWFpbigpIHsKICAgIGludCB0ZXN0Q2FzZXM7CiAgICBjaW4gPj4gdGVzdENhc2VzOwogICAgY2luLmlnbm9yZSgpOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdGVzdENhc2VzOyArK2kpIHsKICAgICAgICBzdHJpbmcga2V5czsKICAgICAgICBnZXRsaW5lKGNpbiwga2V5cyk7CiAgICAgICAgY291dCA8PCBwcm9jZXNzS2V5cyhrZXlzKSA8PCBlbmRsOwogICAgfQoKICAgIHJldHVybiAwOwp9