Using maps and custom comp giving me wrong ans

#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;

}

@YatharthVardan share your code using cb ide

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.