#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
int q;
cin >> q;
bool isReversed = false;
deque<char> dq(s.begin(), s.end()); // Using deque to perform efficient front and back operations
while (q--) {
int type;
cin >> type;
if (type == 1) {
// Toggle the reversed state
isReversed = !isReversed;
} else {
int d;
char c;
cin >> d >> c;
if (!isReversed) {
// Normal order
if (d == 1) {
dq.push_front(c); // Append to the front
} else {
dq.push_back(c); // Append to the back
}
} else {
// Reversed order
if (d == 1) {
dq.push_back(c); // In reversed state, appending to front is like appending to back
} else {
dq.push_front(c); // In reversed state, appending to back is like appending to front
}
}
}
}
// If the string is reversed at the end, reverse the deque before printing
if (isReversed) {
reverse(dq.begin(), dq.end());
}
for (char c : dq) {
cout << c;
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}