import itertools # Enter your code here. Read input from STDIN. Print output to STDOUT # are animals of same type interchangable? # are dogs and mice interchangable/ C and E? t = int(input()) # number of test cases def check_truck(truck): if ('D' in truck or 'M' in truck) and ('C' in truck or 'E' in truck): return False else: return True def check_if_valid(animals, picks, zoo_count): #print(animals, picks, zoo_count) truck = [] max_zoo = 0 for zoo in range(1, zoo_count+1): for animal, pick in zip(animals, picks): #print(pick) if pick: #print(animal) if animal[1] == zoo: truck.append(animal[0]) valid = check_truck(truck) if not valid: return False, 0 if animal[2] == zoo: truck.remove(animal[0]) max_zoo = zoo return True, max_zoo # if the truck is empty pick up the first animal you see # if there is a friendly animal, let's pick them up too # if we pass a place to drop them off, drop them # if we have options, split off for _ in range(t): zoo_count, animal_count = map(int, input().split()) animals = list(input().split()) source_zoo = list(map(int, input().split())) destination_zoo = list(map(int, input().split())) animals_to_go = tuple(zip(animals, source_zoo, destination_zoo)) sources = dict() dests = dict() for i in range(zoo_count): for j in range(animal_count): if destination_zoo[j] == i: d = dests.get(i, list()) dests[i] = d + [animals[j]] if source_zoo[j] == i: s = sources.get(i, list()) sources[i] = s + [animals[j]] binary = itertools.product(range(2), repeat=animal_count) valid_picks = {} for picks in binary: if(sum(picks)== 0): continue #print(picks) valid, max_zoo = check_if_valid(animals_to_go, picks, zoo_count) if valid: count = sum(picks) cur_max = valid_picks.get(count, valid_picks.get(count, float('inf'))) valid_picks[count] = max_zoo if max_zoo < cur_max else cur_max for i in range(1, animal_count+1): print(valid_picks.get(i, -1), sep='', end=' ') print()