import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private static final int E = 0; private static final int D = 1; private static final int C = 2; private static final int M = 3; private static Zoo[] zoos; private static int[] ret; private static boolean[] animalsOdd; private static class Zoo { Set provides = new HashSet<>(); Set demands = new HashSet<>(); } private static class Truck { Set loaded = new HashSet<>(); int shipped = 0; public void load(int animal) { loaded.add(animal); } public void unload(int animal) { loaded.remove(animal); shipped++; } public boolean isLoaded(int animal) { return loaded.contains(animal); } public boolean empty() { return loaded.isEmpty(); } public Truck(int shipped) { this.shipped = shipped; } } static int[] minimumZooNumbers(int m, int n, char[] t, int[] s, int[] d) { // Return a list of length n consisting of the answers zoos = new Zoo[m]; ret = new int[n]; Arrays.fill(ret, -1); animalsOdd = new boolean[n]; for (int i = 0; i < n; i++) { if (d[i] < s[i]) { continue; } animalsOdd[i] = t[i] == 'M' || t[i] == 'D'; if (zoos[s[i]-1] == null) zoos[s[i]-1] = new Zoo(); zoos[s[i]-1].provides.add(i); if (zoos[d[i]-1] == null) zoos[d[i]-1] = new Zoo(); zoos[d[i]-1].demands.add(i); } run(0, m, true, 0); run(0, m, false, 0); return ret; } static void run(int start, int end, boolean odd, int shipped) { Truck truck = new Truck(shipped); for (int i = start; i < end; i++) { if (zoos[i] == null) continue; // unload boolean unloaded = false; for (int demand : zoos[i].demands) { if (odd == animalsOdd[demand] && truck.isLoaded(demand)) { truck.unload(demand); unloaded = true; if (ret[truck.shipped - 1] == -1 || ret[truck.shipped - 1] > i + 1) ret[truck.shipped - 1] = i + 1; } } if (unloaded && truck.empty() && !zoos[i].provides.isEmpty()) { // System.out.println("new truck at stop: " + (i+1) + odd); run(i, end, !odd, truck.shipped); } // load for (int provide : zoos[i].provides) { if (animalsOdd[provide] == odd) { truck.load(provide); } } } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int cases = in.nextInt(); for(int a0 = 0; a0 < cases; a0++){ int m = in.nextInt(); int n = in.nextInt(); char[] t = new char[n]; for(int t_i = 0; t_i < n; t_i++){ t[t_i] = in.next().charAt(0); } int[] s = new int[n]; for(int s_i = 0; s_i < n; s_i++){ s[s_i] = in.nextInt(); } int[] d = new int[n]; for(int d_i = 0; d_i < n; d_i++){ d[d_i] = in.nextInt(); } int[] result = minimumZooNumbers(m, n, t, s, d); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + (i != result.length - 1 ? " " : "")); } System.out.println(""); } in.close(); } }