public class TopKMostFrequentNumberInStream {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int k=sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
topK(arr,k);
}
}
public static void topK(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
PriorityQueue<Pair> heap = new PriorityQueue<>();
for(int i=0;i<nums.length;i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
System.out.println(map);
ArrayList<Integer> keys = new ArrayList<>(map.keySet());
// System.out.println(keys);
int j=0;
for(j=0;j<k && j<keys.size();j++) {
Pair np = new Pair(keys.get(j), map.get(keys.get(j)));
heap.add(np);
}
System.out.print(heap);
for(;j<keys.size();j++) {
Pair check = new Pair(keys.get(j), map.get(keys.get(j)));
System.out.println("Heap peek = " + heap.peek());
if(heap.peek().compareTo(check) >=0) {
heap.remove();
heap.add(check);
}
}
System.out.print(heap);
while(!heap.isEmpty()) {
System.out.print(heap.remove().data + " ");
}
System.out.println();
// System.out.println(heap);
}
}
private static class Pair implements Comparable<Pair>{
int data;
int freq;
Pair(int data, int freq){
this.data=data;
this.freq=freq;
}
@Override
public int compareTo(Pair o) {
if(o.freq>this.freq) {
return 1;
}else if(o.freq<this.freq) {
return -1;
}else {
if(o.data>this.data) {
return -1;
}else if(o.data<this.data) {
return 1;
}else {
return -1;
}
}
}
public String toString() {
return this.data + " -> " + this.freq;
}
}