#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define mp make_pair
#define x first
#define y secound
using namespace std;

inline int read()
{
	int num=0,f=1;char ch=getchar();
	while(!((ch<='9'&&ch>='0')||ch=='-'))ch=getchar();
	if (ch=='-')f=-1,ch=getchar();
	while(ch<='9'&&ch>='0')num=num*10+ch-'0',ch=getchar();
	return num*f;
}

void ckmax(int &a,int b){a=(a>b?a:b);}

const int M=2e5+10;
int n,a[M],p[M],ans[M],sum[M],val[M],lst[M],s1[M],s2[M],t[M],t2[M],vv[310][M];
vector<int>v[M];
map<int,int>tt;

void solve()
{
	n=read();int B=1;
	for (int i=1;i<=n;i++)a[i]=p[i]=read(),v[i].clear(),ans[i]=0;
	sort(p+1,p+1+n);int tot=0;tt.clear();
	for (int i=1;i<=n;i++)
		if (p[i]!=p[i-1])tt[p[i]]=++tot,p[tot]=p[i];
	for (int i=1;i<=n;i++)a[i]=tt[a[i]],v[a[i]].pb(i);
	for (int i=1;i<=tot;i++)
		if (v[i].size()>=B)
		{
			int sz=v[i].size();
			for (int j=1;j<=tot;j++)val[j]=lst[j]=0;
			for (int j=1;j<=n;j++)s1[j]=s2[j]=0;
			for (int j=1;j<=n;j++)
			{
				if (a[j]==i){sum[j]=sum[j-1]+1;continue;}
				sum[j]=sum[j-1];
				val[a[j]]=max(val[a[j]]+1-(lst[a[j]]==0?0:(sum[j]-sum[lst[a[j]]])),1);
				s1[j]=s1[lst[a[j]]]+1-sum[j]+sum[lst[a[j]]];
				lst[a[j]]=j,ckmax(ans[i],val[a[j]]+sz);
			}sum[n+1]=sum[n];
			for (int j=1;j<=tot;j++)val[j]=0,lst[j]=n+1;
			for (int j=n;j>=1;j--)
			{
				if (a[j]==i)continue;
				s2[j]=s2[lst[a[j]]]+1-(sum[lst[a[j]]]-sum[j]);
				ckmax(ans[a[j]],s1[j]+val[a[j]]+sz);
				ckmax(val[a[j]],s2[j]);
			}
		}
//	puts("?");
	int maxn=0;
	for (int i=1;i<=tot;i++)ckmax(maxn,ans[i]);
//	for (int i=1;i<B;i++)
//	{
//		memset(vv[i],0,sizeof(vv[i]));
//		memset(t2,0,sizeof(t2));
//		memset(t,0,sizeof(t));
//		int r=n;vv[i][n+1]=n+1;
//		for (int j=n;j>=1;j--)
//		{
//			t2[t[a[j]]]--;t[a[j]]++;t2[t[a[j]]]++;
//			while((t2[i]||t2[i+1])&&r>=i)t2[t[a[r]]]--,t[a[r]]--,t2[t[a[r]]]++,r--;
//			if (r!=n)
//				r++,t2[t[a[r]]]--,t[a[r]]++,t2[t[a[r]]]++,vv[i][j]=r;
//			else vv[i][j]=n+1;
//		}
//	}
//	for (int i=1;i<=tot;i++)
//		if (v[i].size()<B)
//		{
//			int sz=v[i].size();
//			for (int j=-1;j<sz;j++)
//				for (int k=(j==-1?0:j);k<sz;k++)
//				{
//					//k+1+x>=maxn
//					//x>=maxn-k-1
//					int x=max(0,maxn-1-k),pos=(k==j?n+1:v[i][sz-(k-j)]),tmp=j==-1?0:v[i][j];
//					if (x>=B)continue;
//					while(x<B&&vv[x][tmp+1]<pos)
//						x++;
//					x--;
//					if (x>=0&&vv[x][tmp+1]<pos)ckmax(ans[i],x+k+1),ckmax(maxn,x+k+1);
//				}
//		}
	cout<<maxn<<'\n';
	for (int i=1;i<=tot;i++)
		if (ans[i]==maxn)cout<<p[i]<<'\n';
}

signed main()
{
	freopen("mode.in","r",stdin);
	freopen("mode.out","w",stdout);
//	freopen("qwq.txt","r",stdin);
//	freopen("1.out","w",stdout);
//	freopen("mode_ex2.in","r",stdin);
	int TT=read();
	while(TT--)solve();
	return 0;
}
/*
1
5
1 1 3 1 1

*/
