import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static void sort(char[] t, int[] s, int[] d){ for (int i=0;i s[j+1]){ int temp = d[j]; d[j] = d[j+1]; d[j+1] = temp; temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; char tt = t[j]; t[j] = t[j+1]; t[j+1] = tt; } else if (s[j] == s[j+1]){ if (d[j] > d[j+1]){ int temp = d[j]; d[j] = d[j+1]; d[j+1] = temp; temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; char tt = t[j]; t[j] = t[j+1]; t[j+1] = tt; } } } } static boolean enemy(int idx,char c , ArrayList al,ArrayList alIdx ,int [] s, int[] d){ if (c == 'E' || c == 'C') { for (int i=0 ; i " + al.get(i) + " " + d[alIdx.get(i)] ); return true; } } } }else{ for (int i=0 ; i " + al.get(i) + " " + d[alIdx.get(i)] ); return true; } } } } return false; } static int[] minimumZooNumbers(int m, int n, char[] t, int[] s, int[] d) { sort(t,s,d); boolean checkLen = true; int idx = 0; int[] result = new int[d.length]; for (int i=0; i> solution = new ArrayList>(); ArrayList> solutionIdx = new ArrayList>(); while(checkLen){ if (animIdx >= t.length){ result[idx] = -1; idx++; checkLen = false; animIdx = 0; solIdx = 0; } else{ if (solIdx >= solution.size()){ solution.add(new ArrayList()); solutionIdx.add(new ArrayList()); int ind = solution.size() -1; solution.get(ind).add(t[animIdx]); solutionIdx.get(ind).add(animIdx); if (solution.get(ind).size() == (i+1)) { checkLen = false; result[idx] = d[animIdx]; animIdx = 0; solIdx = 0; idx++; }else{ animIdx++; solIdx = 0; } } else { if (enemy(animIdx,t[animIdx],solution.get(solIdx), solutionIdx.get(solIdx),s,d)) { solIdx++; } else { //if (i == 2) solution.get(solIdx).add(t[animIdx]); solutionIdx.get(solIdx).add(animIdx); if (solution.get(solIdx).size() == (i+1)) { checkLen = false; result[idx] = d[animIdx]; animIdx = 0; solIdx = 0; idx++; }else{ animIdx++; solIdx = 0; } } } } } checkLen = true; } return result; // Return a list of length n consisting of the answers } 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(); } }