#include <bits/stdc++.h>
using namespace std;
void first(int k,deque<int> &D,int &flag) {
//cout << "frist 시작"<<'\n';
char ch;
while (k--) {
cin >> ch;
if (ch == '[' || ch == ',') {
int n;
cin >> n;
D.push_back(n);
}
}
cin >> ch; // ']' 를 받아서 제거시킴
//cout << "frist 끝" << '\n';
}
void second(string s, deque<int> &D,int &flag ) {
//cout << "second 시작" << '\n';
for (auto c : s) {
if (c == 'R') {
reverse(D.begin(), D.end());
}
else { // 'D'
if (D.empty()) {
flag = 1;
}
else {
D.pop_front();
}
}
}
//cout << "second 끝 " << '\n';
}
void third(deque<int> &D) {
cout << '[';
while (!D.empty()) {
cout << D.front();
D.pop_front();
if (!D.empty()) {
cout << ',';
}
}
cout << ']' << '\n';
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) {
int flag = 0;
deque<int> D;
//cout << "s 입력 : ";
string s;
cin >> s;
//cout << "입력된 s : "<< s<<'\n';
int k;
cin >> k;
first(k, D, flag);
second(s, D, flag);
if (flag == 1) {
cout << "error\n";
}
else third(D);
}
}