from itertools import * import copy T = int(raw_input()) def solve(b,score,i,state,e_act,active): # base case if i >= m: return 0 # Animal drop-off if state == 'N': pass elif state == 'A' or state == 'B': if i in e_act and e_act > 0: score += e_act[i] active -= e_act[i] e_act[i] = 0 if active == 0: state = 'N' # Update score if score > b[i]: b[i] = score # Animal pick-up if state == 'A' or state == 'N': if i in A: s = A[i] tmp = chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) for group in tmp: te_act = copy.deepcopy(e_act) for ele in group: if ele in te_act: te_act[ele] += 1 else: te_act[ele] = 1 solve(b,score,i+1,'A',te_act,active+len(group)) if state == 'B' or state == 'N': if i in B: s = B[i] tmp = chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) for group in tmp: te_act = copy.deepcopy(e_act) for ele in group: if ele in te_act: te_act[ele] += 1 else: te_act[ele] = 1 solve(b,score,i+1,'B',te_act,active+len(group)) for t in xrange(T): m,n = map(int, raw_input().split()) # n animals, m zoos animals = raw_input().split() start = map(int, raw_input().split()) end = map(int, raw_input().split()) # Reduce problem scale & clean up data A = {} B = {} for i in xrange(n): if animals[i] == 'E' or animals[i] == 'C': if start[i]-1 in A: A[start[i]-1].append(end[i]-1) else: A[start[i]-1] = [end[i]-1] else: if start[i]-1 in B: B[start[i]-1].append(end[i]-1) else: B[start[i]-1] = [end[i]-1] best = [0]*m print A,B solve(best,0,0,'N',{},0) print best