#include <iostream>
#include <deque>
#include <string>
using namespace std;
string processKeys(const string& keys) {
deque<string> parts;
string current;
bool atBeginning = false;
for (char key : keys) {
if (key == '[') {
if (atBeginning) {
parts.push_front(current);
} else {
parts.push_back(current);
}
current = "";
atBeginning = true;
} else if (key == ']') {
if (atBeginning) {
parts.push_front(current);
} else {
parts.push_back(current);
}
current = "";
atBeginning = false;
} else {
current += key;
}
}
if (!current.empty()) {
if (atBeginning) {
parts.push_front(current);
} else {
parts.push_back(current);
}
}
string result;
for (const string& part : parts) {
result += part;
}
return result;
}
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+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RyaW5nIHByb2Nlc3NLZXlzKGNvbnN0IHN0cmluZyYga2V5cykgewogICAgZGVxdWU8c3RyaW5nPiBwYXJ0czsKICAgIHN0cmluZyBjdXJyZW50OwogICAgYm9vbCBhdEJlZ2lubmluZyA9IGZhbHNlOwoKICAgIGZvciAoY2hhciBrZXkgOiBrZXlzKSB7CiAgICAgICAgaWYgKGtleSA9PSAnWycpIHsKICAgICAgICAgICAgaWYgKGF0QmVnaW5uaW5nKSB7CiAgICAgICAgICAgICAgICBwYXJ0cy5wdXNoX2Zyb250KGN1cnJlbnQpOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcGFydHMucHVzaF9iYWNrKGN1cnJlbnQpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGN1cnJlbnQgPSAiIjsKICAgICAgICAgICAgYXRCZWdpbm5pbmcgPSB0cnVlOwogICAgICAgIH0gZWxzZSBpZiAoa2V5ID09ICddJykgewogICAgICAgICAgICBpZiAoYXRCZWdpbm5pbmcpIHsKICAgICAgICAgICAgICAgIHBhcnRzLnB1c2hfZnJvbnQoY3VycmVudCk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwYXJ0cy5wdXNoX2JhY2soY3VycmVudCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3VycmVudCA9ICIiOwogICAgICAgICAgICBhdEJlZ2lubmluZyA9IGZhbHNlOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGN1cnJlbnQgKz0ga2V5OwogICAgICAgIH0KICAgIH0KCiAgICBpZiAoIWN1cnJlbnQuZW1wdHkoKSkgewogICAgICAgIGlmIChhdEJlZ2lubmluZykgewogICAgICAgICAgICBwYXJ0cy5wdXNoX2Zyb250KGN1cnJlbnQpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBhcnRzLnB1c2hfYmFjayhjdXJyZW50KTsKICAgICAgICB9CiAgICB9CgogICAgc3RyaW5nIHJlc3VsdDsKICAgIGZvciAoY29uc3Qgc3RyaW5nJiBwYXJ0IDogcGFydHMpIHsKICAgICAgICByZXN1bHQgKz0gcGFydDsKICAgIH0KCiAgICByZXR1cm4gcmVzdWx0Owp9CgppbnQgbWFpbigpIHsKICAgIGludCB0ZXN0Q2FzZXM7CiAgICBjaW4gPj4gdGVzdENhc2VzOwogICAgY2luLmlnbm9yZSgpOyAvLyDZhNiq2KzYp9mH2YQg2KfZhNiz2LfYsSDYp9mE2KzYr9mK2K8g2KjYudivINi52K/YryDYp9mE2K3Yp9mE2KfYqgoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdGVzdENhc2VzOyArK2kpIHsKICAgICAgICBzdHJpbmcga2V5czsKICAgICAgICBnZXRsaW5lKGNpbiwga2V5cyk7CiAgICAgICAgY291dCA8PCBwcm9jZXNzS2V5cyhrZXlzKSA8PCBlbmRsOwogICAgfQoKICAgIHJldHVybiAwOwp9