importsysfromcollectionsimportdequeforTinrange(int(sys.stdin.readline().rstrip())):P=sys.stdin.readline().rstrip()N=int(sys.stdin.readline().rstrip())## 다양하게 input 받는 거 고민하기 좋다.arr=deque(list(sys.stdin.readline().rstrip()[1:-1].split(",")))ifN==0orlen(arr)==0:arr=deque([])reverse=Falseerror=FalseforiinP:ifi=="R":reverse=notreverseelifi=="D":iflen(arr)==0:error=Truebreakifreverse:arr.pop()else:arr.popleft()iferror:print("error")else:ifreverse:arr.reverse()print("[",end="")print(",".join(arr),end="")print("]")## reverse() 여러번 돌리면 시간초과 뜬다. # flag = True# for i in P:# if i == "R":# arr.reverse()# else:# if len(arr) != 0:# arr.popleft()# else:# print("error")# flag = False# break# if flag:# if len(arr) != 0:# print("[", end="") # print(",".join(arr), end="") # print("]")# else:# print('error')