Why is this code giving wrongs ans. This code is accepted in GFG ide

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

    int t=sc.nextInt();

    while(t-- >0)
    {
        int n = sc.nextInt(); // n elements
        int k = sc.nextInt(); // top k

        HashMap<Integer, Integer> mp = new HashMap<>(); // num->freq

        for(int i=0; i<n; i++)
        {
            int num = sc.nextInt();
            mp.put( num, (mp.get(num)==null)? 1: mp.get(num)+1 );

            // create priority queue with comparator for a map
            PriorityQueue<Map.Entry<Integer, Integer>> pq =
                    new PriorityQueue<>( (a,b)-> a.getValue().equals(b.getValue()) ?
                            Integer.compare(a.getKey(),b.getKey()) : Integer.compare(b.getValue(), a.getValue()) );


            // add all elements of map to pq
            for(Map.Entry<Integer, Integer> e : mp.entrySet())  // for each entry in the set of all entries
            {
                pq.add(e);
            }

// pq.addAll(mp.entrySet()); // we can bulk add all elements using this statement

// System.out.println();
// now iterate the priority queue and print top k elements
for(int j=0;!pq.isEmpty() && j<k; j++)
System.out.print(pq.poll().getKey() +" ");
}

    }

}

}

always follow output format
print for each test case in a new line

got it. Thanks. Wasted lot of my time on this. :()