typeMap = {"E": 1, "D": 0, "C": 1, "M": 0} notMap = {1: 0, 0: 1} for _ in range(int(input())): n, q = map(int, input().split()) types = input().split() begins = map(int, input().split()) ends = map(int, input().split()) animals = sorted([(e, b, typeMap[t]) for t, b, e in zip(types, begins, ends)]) """ dp[type][x] is the number of animals you can transport up to zoo `x` ending with type `type` """ dp = {0:{}, 1:{}} """ zoosVisited[type] is the keys in order of the map for dp[type] (for a binary search) """ zoosVisited = {0: [], 1: []} def maxUntilNow(type, end): """ get the max number of animals tranported ending with an animal of type `type` and ending <= `end` """ l, h = 0, len(zoosVisited[type]) - 1 if h == -1: return 0 while h - l > 1: m = (l + h) // 2 if zoosVisited[type][m] > end: h = m else: l = m # print(zoosVisited[type], l, h, end) if zoosVisited[type][h] > end: if l == h: return 0 ans = zoosVisited[type][l] else: ans = zoosVisited[type][h] return dp[type][ans] ans = [10**20 for i in range(q + 1)] for e, b, t in animals: m = max(maxUntilNow(t, e), maxUntilNow(notMap[t], b)) + 1 # print(t, b, e, m, maxUntilNow(t, e), maxUntilNow(notMap[t], b)) ans[m] = min(ans[m], e) if e in dp[t]: dp[t][e] = max(dp[t][e], m) else: dp[t][e] = m zoosVisited[t].append(e) ans = [x if x != 10 ** 20 else -1 for x in ans[1:]] print(*ans)