I tried all test cases. They are all right but submission is showing that it is wrong answer

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
long long int n,k;
cin>>n>>k;
long long int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
if(k<4)
cout<<-1<<endl;
else
{
long long int count=1;
for(int i=1;i<n;i++)
{
if(a[i]!=a[i-1])
count++;
}
if(count>=(k-1))
cout<<-1<<endl;
else
{
long long int ans=1;
long long int c=0;
for(int i=0;i<n;i++)
{
if(c>=2)
break;
if(i+1<n && a[i+1]==a[i])
{
ans*=a[i];
i++;
c++;
}
}
cout<<ans<<endl;
}
}

}
}

Please remove your code and replace it by-

  • Your logic and intuition. What you did to solve question
  • A link to your submission.