https://www.acmicpc.net/problem/5430
import sys
from collections import deque
def solution():
read = sys.stdin.readline
t = int(read())
for _ in range(t):
# 입력 받기
functions = read().rstrip()
n = int(read())
numbers = deque()
tmp = read().rstrip()[1:-1]
if tmp:
numbers = deque(tmp.split(','))
# 반복
front, error = True, False
for f in functions:
# 방향 바꿔주기
if f == 'R':
if front:
front = False
else:
front = True
# 방향에 따라 pop
elif f == 'D':
if numbers:
if front:
numbers.popleft()
else:
numbers.pop()
else:
error = True
break
if not front:
numbers.reverse()
# 반복 결과
if error:
print('error')
else:
print('[' + ','.join(numbers) + ']')
solution()