#!/bin/python3 import sys from collections import defaultdict import heapq import itertools # He must visit the zoos in increasing order. # Dogs are scared of elephants # Cats are scared of dogs # Mice are scared of cats # Elephants are scared of mice FEAR_INDEX = { 'D': set(['E', 'C']), 'C': set(['D', 'M']), 'M': set(['C', 'E']), 'E': set(['D', 'M']), } class PriorityQueue(object): def __init__(self): self.pq = [] # list of entries arranged in a heap self.entry_finder = {} # mapping of tasks to entries self.REMOVED = '' # placeholder for a removed task self.counter = itertools.count() # unique sequence count def add(self, task): 'Add a new task or update the priority of an existing task' if task in self.entry_finder: self.remove_task(task) count = next(self.counter) priority = task.time * 2 + (0 if task.end is None else 1) entry = [priority, count, task] self.entry_finder[task] = entry heapq.heappush(self.pq, entry) def remove(self, task): 'Mark an existing task as REMOVED. Raise KeyError if not found.' entry = self.entry_finder.pop(task) entry[-1] = self.REMOVED def pop(self): 'Remove and return the lowest priority task. Raise KeyError if empty.' while self.pq: priority, count, task = heapq.heappop(self.pq) if task is not self.REMOVED: del self.entry_finder[task] return task raise KeyError('pop from an empty priority queue') class Event(object): def __init__(self, time, animal, end): self.time = time self.animal = animal self.end = end def __cmp__(self, other): if self.time < other.time: return -1 if self.time > other.time: return 1 if self.end is True: return -1 if other.end is True: return 1 return 0 def min_zoo(events, num, counts): best = -1 try: e = events.pop() except KeyError: return -1 if e.end: # decide whether to pick this or not best = min_zoo(events, num, counts) if all(counts[a] == 0 for a in FEAR_INDEX[e.animal]): counts[e.animal] += 1 events.add(e.end) mn = min_zoo(events, num, counts) if mn != -1 and (best == -1 or mn < best): best = mn events.remove(e.end) counts[e.animal] -= 1 else: # unload an animal if num - 1 == 0: best = e.time else: counts[e.animal] -= 1 mn = min_zoo(events, num - 1, counts) if mn != -1 and (best == -1 or mn < best): best = mn counts[e.animal] += 1 events.add(e) return best def minimumZooNumbers(m, n, t, s, d): # Return a list of length n consisting of the answers events = PriorityQueue() for i, ti in enumerate(t): end = Event(d[i], ti, None) start = Event(s[i], ti, end) events.add(start) ans = [min(d)] for i in range(2, n+1): ans.append(min_zoo(events, num=i, counts=defaultdict(int))) return ans if __name__ == "__main__": cases = int(input().strip()) for a0 in range(cases): m, n = input().strip().split(' ') m, n = [int(m), int(n)] t = input().strip().split(' ') s = list(map(int, input().strip().split(' '))) d = list(map(int, input().strip().split(' '))) result = minimumZooNumbers(m, n, t, s, d) print(" ".join(map(str, result)))