#!/bin/python3 import sys from collections import defaultdict def minimumZooNumbers(m, n, t, s, d): # Return a list of length n consisting of the answers # m zoos, n animals return get_zoos(get_nums(get_runs(get_arr(t)))) def get_arr(t): """arr[t][i][j] = # animals of type t that go from i->j""" t = [0 if x == 'E' or x == 'C' else 1 for x in t] arr = [[[0]*(m+1) for j in range(m+1)] for t in range(2)] for i in range(n): if s[i] < d[i]: arr[t[i]][s[i]][d[i]] += 1 return arr def get_runs(arr3): """runs[t][i][j] stores all animals of type t that can go i->j.""" runs3 = [[[0]*(m+1) for j in range(m+1)] for t in range(2)] for t in range(2): arr, runs = arr3[t], runs3[t] for delta in range(1, m): for i in range(1, m+1-delta): j = i + delta # MYBE why do we need max? runs[i][j] = arr[i][j] + max(runs[i][k] + runs[k][j] for k in range(i, j)) return runs3 def get_nums(runs): """nums[zoo] is the max # animals that can be transported up to zoo i.""" paths = [[], []] # path is a list of tuples of runs nums = [0, 0] for zoo in range(2, m+1): best_path, best_num = [(0,1,zoo)], runs[0][1][zoo] # run of type 0 cur_num = runs[1][1][zoo] # run of type 1 if cur_num > best_num: best_path, best_num = [(1,1,zoo)], runs[1][1][zoo] for i in range(2, zoo-1): # switch at i other_type = 0 if paths[i][-1][0] else 1 cur_num = nums[i] + runs[other_type][i][zoo] if cur_num > best_num: best_path, best_num = paths[i] + [(other_type, i, zoo)], cur_num paths.append(best_path) nums.append(best_num) return nums def get_zoos(nums): zoos = [] zoo = 2 for count in range(n): while True: if nums[zoo] >= count+1: zoos.append(zoo) break elif zoo == m: zoos.append(-1) break else: zoo += 1 return zoos if __name__ == "__main__": cases = int(input().strip()) for a0 in range(cases): m, n = input().strip().split(' ') m, n = [int(m), int(n)] t = input().strip().split(' ') s = list(map(int, input().strip().split(' '))) d = list(map(int, input().strip().split(' '))) result = minimumZooNumbers(m, n, t, s, d) print (" ".join(map(str, result)))