from collections import defaultdict as dd import sys def debug(*args,**kwargs): print(*args,**kwargs,file=sys.stderr) class Fenwickesque: def __init__(T, n): sz = 1 while (sz < n): sz <<= 1 T.sz = sz T.data = [0]*(2*sz) T.childmax = [0]*(2*sz) def add(T, l, r, val): modified = [] l += T.sz r += T.sz while l < r: if l%2 == 1: T.data[l] += val modified.append(l) l += 1 if r%2 == 1: r -= 1 T.data[r] += val modified.append(r) l >>= 1 r >>= 1 for node in modified: node >>= 1 while node: c1 = 2*node c2 = 2*node + 1 newmax = max(T.data[c1]+T.childmax[c1], T.data[c2]+T.childmax[c2]) if newmax <= T.childmax[node]: break T.childmax[node] = newmax node >>= 1 def maxi(T): return T.data[1]+T.childmax[1] t = int(input()) for _ in range(t): m,n = map(int,input().split()) # m zoos, n animals K = input().replace('E','C').replace('M','D').split() S = [int(x) for x in input().split()] D = [int(x) for x in input().split()] A = [(k,s,d) for k,s,d in zip(K,S,D)] catsAt = [[] for _ in range(m+1)] dogsAt = [[] for _ in range(m+1)] for k,s,d in A: if d < s: continue if k == 'C': catsAt[d].append(s) else: dogsAt[d].append(s) # best vid visst zoo best = [0]*(m+1) bestCfrom = Fenwickesque(m+1) bestDfrom = Fenwickesque(m+1) for i in range(1, m+1): for start in catsAt[i]: bestCfrom.add(0,start+1,1) for start in dogsAt[i]: bestDfrom.add(0,start+1,1) newC = bestCfrom.maxi() newD = bestDfrom.maxi() bestCfrom.add(i,i+1,newD) bestDfrom.add(i,i+1,newC) best[i] = max(newC,newD) ans = [-1]*n ani = 0 for i,b in enumerate(best): while ani