import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Solution { static int[] minimumZooNumbers(int m, int n, char[] t, int[] s, int[] d) { // Return a list of length n consisting of the answers Animal[] animals = new Animal[n]; int[] result = new int[n]; for (int i = 0; i < n; i++) { animals[i] = new Animal(t[i], s[i], d[i]); } Arrays.sort(animals, new Comparator() { public int compare(Animal a, Animal b) { return a.getDestination() - b.getDestination(); } }); result[0] = animals[0].getDestination(); for (int i = 1; i < n; i++) { result[i] = -1; } for (int i = 1; i < n; i++) { result[i] = minZoo(animals, n, i + 1, new ArrayList(), 0); if (result[i] == -1) { break; } } return result; } static int minZoo(Animal[] animals, int n, int nr, List picked, int index) { if (nr == 0) { return picked.get(0).destination; } else { for (int i = index; i < n; i++) { Animal chosen = animals[i]; char t = chosen.getType(); int j = 0; boolean canChoose = true; if (j < picked.size()) { while (chosen.source < picked.get(j).getDestination()) { char pick_type = picked.get(j).getType(); if (t == 'E' && (pick_type == 'D' || pick_type == 'M')) { canChoose = false; break; } if (t == 'D' && (pick_type == 'E' || pick_type == 'C')) { canChoose = false; break; } if (t == 'C' && (pick_type == 'D' || pick_type == 'M')) { canChoose = false; break; } if (t == 'M' && (pick_type == 'C' || pick_type == 'E')) { canChoose = false; break; } j++; if(j >= picked.size()) { break; } } } if (canChoose) { picked.add(0, chosen); int res = minZoo(animals, n, nr - 1, picked, i + 1); if( res > 0) { return res; } picked.remove(0); } } } return -1; } 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(); } } class Animal { char type; int source, destination; public Animal(char type, int source, int destination) { super(); this.type = type; this.source = source; this.destination = destination; } public char getType() { return type; } public void setType(char type) { this.type = type; } public int getSource() { return source; } public void setSource(int source) { this.source = source; } public int getDestination() { return destination; } public void setDestination(int destination) { this.destination = destination; } @Override public String toString() { return "Animal [type=" + type + ", source=" + source + ", destination=" + destination + "]"; } }