#include<bits/stdc++.h>
#include
using namespace std;
unordered_map<int,int> freq;
bool comp(int a,int b)
{
if(freq[a]==freq[b])
{
if(a>b)
return true;
else
return false;
}
return freq[a]>freq[b];
}
int main() {
int t;
cin>>t;
while(t–){
int n,k;
cin>>n>>k;
vector<int> a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
freq.clear();
for(int i=0;i<n;i++)
{
//cout<<"insdie loop 1\n";
//unordered_map<int,int> freq;
vector<int> top(k+1);
//alaways put at last
top[i]=a[i];
freq[a[i]]++;
sort(top.begin(),top.end(),comp);
//bubble sort after this
for(int j=0;j<k && top[i]!=0 ;j++)
{
//cout<<"lll";
cout<<a[j]<<" ";
}
}
cout<<"\n";
}
return 0;
}