#!/bin/python3 import sys def longest_increasing_subsequence_indices(seq): from bisect import bisect_right if len(seq) == 0: return seq # m[j] in iteration i is the last index of the increasing subsequence of seq[:i] # that ends with the lowest possible value while having length j m = [None] * len(seq) predecessor = [None] * len(seq) best_len = 0 for i, item in enumerate(seq): j = bisect_right([seq[k] for k in m[:best_len]], item) m[j] = i predecessor[i] = m[j-1] if j > 0 else None best_len = max(best_len, j+1) result = [] i = m[best_len-1] while i is not None: result.append(i) i = predecessor[i] result.reverse() return result def longest_increasing_subsequence(seq): return [seq[i] for i in longest_increasing_subsequence_indices(seq)] def minimumZooNumbers(m, n, t, s, d): # Return a list of length n consisting of the answers a = [-1]*n b= longest_increasing_subsequence(d) a[0]=b[0] p=1 for i in range(1,len(b)): if(b[i]==b[i-1]): b[i]=0 else: a[p]=b[i] p=p+1 return a 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)))