import sys

t = int(sys.stdin.readline().rstrip())

for i in range(t):
    p = sys.stdin.readline().rstrip()
    n = int(sys.stdin.readline().rstrip())
    arr = sys.stdin.readline().rstrip()
    
    revCount = False
    
    try:
        arr = arr[1:-1]
        
        if len(arr) > 0:
            arr = list(map(int, arr.split(',')))
        else:
            arr = []
        
        
        for i in p:
            if i == 'R':
                if revCount == False:
                    revCount = True     
                    
                elif revCount == True:
                    revCount = False
                    
            if i == 'D':
                if revCount == False:
                    del arr[0]
                elif revCount == True:
                    arr.pop()
                
            
        if revCount == True:
            arr.sort(reverse=True)
            print('[', end='')
            print(*arr, sep=',', end='')
            print(']')
        else:
            print('[', end='')
            print(*arr, sep=',', end='')
            print(']')
    except:
        print("error")