import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); AnimalTransport solver = new AnimalTransport(); int testCount = Integer.parseInt(in.next()); for (int i = 1; i <= testCount; i++) solver.solve(i, in, out); out.close(); } static class AnimalTransport { int len; public void solve(int testNumber, InputReader in, PrintWriter out) { int m = in.nextInt(); int n = in.nextInt(); len = m; int[] tmax1 = new int[4 * len]; int[] tadd1 = new int[4 * len]; int[] tmax2 = new int[4 * len]; int[] tadd2 = new int[4 * len]; char[] t = new char[n]; for (int i = 0; i < n; i++) { t[i] = in.next().toCharArray()[0]; } int[] s = new int[n]; for (int i = 0; i < n; i++) { s[i] = in.nextInt() - 1; } List[] close = new List[m]; for (int i = 0; i < m; i++) { close[i] = new ArrayList<>(); } int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = in.nextInt() - 1; close[d[i]].add(i); } int[] dp = new int[m]; for (int i = 0; i < m; i++) { for (int j : close[i]) { if (s[j] < i) { if (t[j] == 'E' || t[j] == 'C') { add(tmax1, tadd1, 0, s[j], 1); } else { add(tmax2, tadd2, 0, s[j], 1); } } } int v1 = max(tmax1, tadd1, 0, i); int v2 = max(tmax2, tadd2, 0, i); dp[i] = Math.max(v1, v2); add(tmax1, tadd1, i, i, dp[i]); add(tmax2, tadd2, i, i, dp[i]); } int[] res = new int[n]; Arrays.fill(res, -1); for (int i = 1; i < m; i++) { for (int j = dp[i - 1]; j < dp[i]; j++) { if (j < n) res[j] = i + 1; } } for (int v : res) { out.print(v + " "); } out.println(); } void push(int[] tmax, int[] tadd, int root) { tmax[root] += tadd[root]; tadd[2 * root + 1] += tadd[root]; tadd[2 * root + 2] += tadd[root]; tadd[root] = 0; } public int max(int[] tmax, int[] tadd, int from, int to) { return max(tmax, tadd, from, to, 0, 0, len - 1); } int max(int[] tmax, int[] tadd, int from, int to, int root, int left, int right) { if (from == left && to == right) { return tmax[root] + tadd[root]; } push(tmax, tadd, root); int mid = (left + right) >> 1; int res = Integer.MIN_VALUE; if (from <= mid) res = Math.max(res, max(tmax, tadd, from, Math.min(to, mid), 2 * root + 1, left, mid)); if (to > mid) res = Math.max(res, max(tmax, tadd, Math.max(from, mid + 1), to, 2 * root + 2, mid + 1, right)); return res; } public void add(int[] tmax, int[] tadd, int from, int to, int delta) { add(tmax, tadd, from, to, delta, 0, 0, len - 1); } void add(int[] tmax, int[] tadd, int from, int to, int delta, int root, int left, int right) { if (from == left && to == right) { tadd[root] += delta; return; } // push can be skipped for add, but is necessary for other operations such as set push(tmax, tadd, root); int mid = (left + right) >> 1; if (from <= mid) add(tmax, tadd, from, Math.min(to, mid), delta, 2 * root + 1, left, mid); if (to > mid) add(tmax, tadd, Math.max(from, mid + 1), to, delta, 2 * root + 2, mid + 1, right); tmax[root] = Math.max(tmax[2 * root + 1] + tadd[2 * root + 1], tmax[2 * root + 2] + tadd[2 * root + 2]); } } static class InputReader { final InputStream is; final byte[] buf = new byte[1024]; int pos; int size; public InputReader(InputStream is) { this.is = is; } public int nextInt() { int c = read(); while (isWhitespace(c)) c = read(); int sign = 1; if (c == '-') { sign = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = res * 10 + c - '0'; c = read(); } while (!isWhitespace(c)); return res * sign; } public String next() { int c = read(); while (isWhitespace(c)) c = read(); StringBuilder sb = new StringBuilder(); do { sb.append((char) c); c = read(); } while (!isWhitespace(c)); return sb.toString(); } int read() { if (size == -1) throw new InputMismatchException(); if (pos >= size) { pos = 0; try { size = is.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (size <= 0) return -1; } return buf[pos++] & 255; } static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }