


The first line of input contains an integer T, the number of test cases.
The first line of each test case contains an integer that denotes the value of K.
The next 2*K lines of each test case follow:
The first line contains an integer ‘N’ denoting the size of the array.
The second line contains N space-separated integers.
The first and only line of output contains space-separated elements of the merged and sorted array, as described in the task.
You don’t have to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 5
1 <= K <= 5
1 <= N <= 20
-10^5 <= DATA <= 10^5
Time Limit: 1 sec
The idea is based on the divide and conquer strategy. We take pairs of arrays at each step. Then merge the pairs using the two-pointer technique of merging two sorted arrays. Thus, after merging all the pairs, the number of arrays will reduce by half.
We will continue this till the number of remaining arrays doesn’t become 1.
Algorithm:
The idea is to use the concept of min-heap. As we know, the root of the min-heap is always the minimum element in the heap. Thus, insert the first elements of all the ‘K’ arrays into the heap along with their indices, and then removing the minimum ( root ) element and adding it to the output array will give us the required result. We will store indices into the heap because we can get the next greater element from the same array where the current element has been popped.
Algorithm: