#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define P 1000007
#define N 401010
#define M 801010
#define mp make_pair
#define pb push_back
//#define M 202020
#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool del[N],in[N];
vector <int> nei;
queue <int> q,dq;
set <pair<int,int> > graph;
vector <pair<int,int> > Hash[P];

inline void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		//if(x != 23 and x != 54 and x != 41)continue ;
		if(del[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(del[y])	continue;
			//cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
inline void Reset() {
	for(register int i=1;i<=n;++i)
		in[i]=0,head[i]=0,Rdeg[i]=0;
	for(register int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1;
	nei.clear();
	graph.clear();
}
inline void topo() {
	while(!q.empty())
		q.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0;
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k)
			q.push(i),del[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(del[y])	continue;
			if(deg[y] < k)
				q.push(y),del[y]=1;
		}
	}
}
inline void Delete() {
	//printf("delete is : ");
	while(!dq.empty()) {
		int x=dq.front();dq.pop();
		//cout<<x<<' ';
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(!del[y] and deg[y] < k-1)
				dq.push(y),del[y]=1,deg[y]=-Inf;
			else if(!del[y] and !in[y] and deg[y] == k-1)
				q.push(y),in[y]=1;
		}
	}
	//puts("");
}
inline bool check() {
	//Print_nei();
	for(register int i=0;i<nei.size();++i) {
		int x=nei[i];
		for(register int j=i+1;j<nei.size();++j) {
			int y=nei[j];
			if(graph.find(mp(x,y)) == graph.end())
				return false;
		}
	}
	return true;
}
inline void work() {
	while(!q.empty())
		q.pop();
	while(!dq.empty())
		dq.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k-1)
			dq.push(i),deg[i]=-Inf,del[i]=1;
		else if(deg[i] == k-1)
			q.push(i),in[i]=1;
	//puts("start");
	while(1) {
		Delete();
		//Print();
		if(q.empty())	return ;
		int x=q.front();q.pop();
		//cout<<x<<' ';
		if(del[x])	continue;
		nei.pb(x);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!del[y])
				nei.pb(y);
		}
		if(check()) {//puts("");
			return ;}
		nei.clear(),dq.push(x),deg[x]=-Inf,del[x]=1;
	}
	//puts("");
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		//if(T == 9 and t == 6)
		//	printf("now ! data ! \n%d %d %d\n",n,m,k);
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			//if(T == 9 and t == 6) {
			//	printf("%d %d\n",x,y);
			//}
			add(x,y),add(y,x);
			graph.insert(mp(x,y)),graph.insert(mp(y,x));
		}
		//if(T == 4 and t != 4)	continue;
		if(k*(k-1) > 2*m) {
			puts("-1");
			continue;
		}
		//start type 1
		topo();
		int num=0;
		for(int i=1;i<=n;++i)
			if(!del[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!del[i])	writes(i);
			puts("");
			continue;
		}
		//end type 1

		//start type 2
		work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
		//end type 2
	}

	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 401010
#define M 801010
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool vis[N];
vector <int> nei;
int Now[N],dfn;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		if(vis[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0,Now[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1,dfn=0;
}
void topo(int now) {
	queue <int> q;
	for(int i=1;i<=n;++i)
		vis[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k-now)
			q.push(i),vis[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] < k-now)
				q.push(y),vis[y]=1;
		}
	}
}
bool check() {
	queue <int> q;
	for(int i=0;i<nei.size();++i) {
		int x=nei[i],tmp=0;
		for(int j=head[x];j;j=nxt[j]) {
			int y=ver[j];
			if(vis[y])	continue;
			if(Now[y])
				tmp++;
		}
		if(tmp != nei.size()-1)
			return 0;
	}
	return 1;
}
void Work() {
	queue <int> q;
	for(int i=1;i<=n;++i)
		if(!vis[i] and deg[i] == k-1)
			q.push(i);
	while(!q.empty()) {
		int x=q.front();q.pop();
		if(vis[x])	continue;
		nei.clear(),dfn++;
		memset(Now,0,sizeof Now);
		nei.push_back(x),Now[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			nei.push_back(y),Now[y]=1;
		}
		//Print_nei();
		if(check())
			return ;
		vis[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			deg[y]--;
			if(deg[y] < k-1)	vis[y]=1;
			if(deg[y] == k-1)	q.push(y);
		}
	}
	nei.clear();
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			if(T == 4 and t != 4)	continue;
			add(x,y),add(y,x);
		}
		if(T == 4 and t != 4)	continue;
		topo(0);

		int num=0;
		for(int i=1;i<=n;++i)
			if(!vis[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!vis[i])	writes(i);
			puts("");
			continue;
		}
		topo(1);//Print();
		Work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}

/*
1
6 11 4
1 2
2 3
3 4
4 1
1 3
2 4
1 5
4 5
6 2
6 3
2 5
*/


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 201010
#define M 401010
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool vis[N];
vector <int> nei;
int now[N],dfn;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		if(vis[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0,now[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1,dfn=0;
}
void topo(int now) {
	queue <int> q;
	for(int i=1;i<=n;++i)
		vis[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k-now)
			q.push(i),vis[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] < k-now)
				q.push(y),vis[y]=1;
		}
	}
}
bool check() {
	queue <int> q;
	for(int i=0;i<nei.size();++i) {
		int x=nei[i],tmp=0;
		for(int j=head[x];j;j=nxt[j]) {
			int y=ver[j];
			if(vis[y])	continue;
			if(now[y] == dfn)
				tmp++;
		}
		if(tmp != nei.size()-1)
			return 0;
	}
	return 1;
}
void Work() {
	queue <int> q;
	for(int i=1;i<=n;++i)
		if(!vis[i] and deg[i] == k-1)
			q.push(i);
	while(!q.empty()) {
		int x=q.front();q.pop();
		if(vis[x])	continue;
		nei.clear(),dfn++;
		nei.push_back(x),now[x]=dfn;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			nei.push_back(y),now[y]=dfn;
		}
		//Print_nei();
		if(check())
			return ;
		vis[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			deg[y]--;
			if(deg[y] < k-1)	vis[y]=1;
			if(deg[y] == k-1)	q.push(y);
		}
	}
	nei.clear();
}

int main() {
	int T=read();
	while(T--) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			if(T == 0)
			add(x,y),add(y,x);
		}
		if(T != 0)	continue;
		topo(0);

		int num=0;
		for(int i=1;i<=n;++i)
			if(!vis[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!vis[i])	writes(i);
			puts("");
			continue;
		}
		topo(1);//Print();
		Work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}

/*
1
6 11 4
1 2
2 3
3 4
4 1
1 3
2 4
1 5
4 5
6 2
6 3
2 5
*/


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 401010
#define M 801010
#define mp make_pair
#define pb push_back
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool del[N];
vector <int> nei;
queue <int> q,dq;
map <pair<int,int> , bool> graph;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		if(del[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(del[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1;
	nei.clear();
	graph.clear();
}
void topo() {
	while(!q.empty())
		q.pop();
	for(int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k)
			q.push(i),del[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(del[y])	continue;
			if(deg[y] < k)
				q.push(y),del[y]=1;
		}
	}
}
void Delete() {
	while(!dq.empty()) {
		int x=dq.front();dq.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			map<pair<int,int>,bool>::iterator it=graph.find(mp(y,x));
			if(it != graph.end())
				graph.erase(it);
			it=graph.find(mp(y,x));
			if(it != graph.end())
				graph.erase(it);
			deg[y]--;
			if(!del[y] and deg[y] < k-1)
				dq.push(y),del[y]=1;
			else if(!del[y] and deg[y] == k-1)
				q.push(y);
		}
	}
}
bool check() {
	for(int i=0;i<nei.size();++i) {
		int x=nei[i];
		for(int j=0;j<nei.size();++j) {
			int y=nei[j];
			if(x == y)	continue;
			map<pair<int,int>,bool>::iterator it = graph.find(mp(x,y));
			if(it == graph.end())
				return false;
		}
	}
	return true;
}
void work() {
	while(!q.empty())
		q.pop();
	while(!dq.empty())
		dq.pop();
	for(int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k-1)
			dq.push(i),del[i]=1;
		else if(deg[i] == k-1)
			q.push(i);
	while(1) {
		Delete();
		if(q.empty())	return ;
		int x=q.front();q.pop();
		if(del[x])	continue;
		nei.pb(x);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!del[y])
				nei.pb(y);
		}
		if(check())
			return ;
		nei.clear(),dq.push(x);
	}
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			//if(T == 4 and t != 4)	continue;
			add(x,y),add(y,x);
			graph[mp(x,y)]=1,graph[mp(y,x)]=1;
		}
		//if(T == 4 and t != 4)	continue;

		//start type 1
		topo();
		int num=0;
		for(int i=1;i<=n;++i)
			if(!del[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!del[i])	writes(i);
			puts("");
			continue;
		}
		//end type 1

		//start type 2
		work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
		//end type 2
	}

	return 0;
}

/*
1
6 11 4
1 2
2 3
3 4
4 1
1 3
2 4
1 5
4 5
6 2
6 3
2 5
*/


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 201010
#define M 401010
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool vis[N];
vector <int> nei;
int now[N],dfn;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		printf("deg[%d] is %d\n",x,deg[x]);
		if(vis[x])	continue;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0,vis[i]=0,now[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1,dfn=0;
}
void topo(int now) {
	queue <int> q;
	memcpy(deg,Rdeg,sizeof deg);
	memset(vis,0,sizeof vis);
	for(int i=1;i<=n;++i)
		if(deg[i] < k-now)
			q.push(i),vis[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] < k-now)
				q.push(y),vis[y]=1;
		}
	}
}
bool check() {
	queue <int> q;
	for(int i=0;i<nei.size();++i) {
		int x=nei[i],tmp=0;
		for(int j=head[x];j;j=nxt[j]) {
			int y=ver[j];
			if(vis[y])	continue;
			if(now[y] == dfn)
				tmp++;
		}
		if(tmp != nei.size()-1)
			return 0;
	}
	return 1;
}
void Work() {
	queue <int> q;
	for(int i=1;i<=n;++i)
		if(!vis[i] and deg[i] == k-1)
			q.push(i);
	while(!q.empty()) {
		int x=q.front();q.pop();
		if(vis[x])	continue;
		nei.clear(),dfn++;
		nei.push_back(x),now[x]=dfn;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			nei.push_back(y),now[y]=dfn;
		}
		//Print_nei();
		if(check())
			return ;
		vis[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			deg[y]--;
			if(deg[y] < k-1)	vis[y]=1;
			if(deg[y] == k-1)	q.push(y);
		}
	}
	nei.clear();
}

int main() {
	int T=read();
	while(T--) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i)
			x=read(),y=read(),add(x,y),add(y,x);
		topo(0);

		int num=0;
		for(int i=1;i<=n;++i)
			if(!vis[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!vis[i])	writes(i);
			puts("");
			continue;
		}
		topo(1);//Print();
		Work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}

/*
3
5 9 4
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
*/


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define P 1000007
#define N 401010
#define M 801010
#define mp make_pair
#define pb push_back
//#define M 202020
#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool del[N],in[N];
vector <int> nei;
queue <int> q,dq;
vector <pair<int,int> > H[P];

inline void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
inline int Hash(int x,int y) {LL now=0;now=1ll*x*y%P*(x-1)%P*(y-1)%P;return now;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		//if(x != 23 and x != 54 and x != 41)continue ;
		if(del[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(del[y])	continue;
			//cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
inline void Reset() {
	for(register int x=1;x<=n;++x)
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!H[Hash(x,y)].empty())
				H[Hash(x,y)].pop_back();
		}
	for(register int i=1;i<=n;++i)
		in[i]=0,head[i]=0,Rdeg[i]=0;
	for(register int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1;
	nei.clear();
}
inline void topo() {
	while(!q.empty())
		q.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0;
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k)
			q.push(i),del[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(del[y])	continue;
			if(deg[y] < k)
				q.push(y),del[y]=1;
		}
	}
}
inline void Delete() {
	//printf("delete is : ");
	while(!dq.empty()) {
		int x=dq.front();dq.pop();
		//cout<<x<<' ';
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(!del[y] and deg[y] < k-1)
				dq.push(y),del[y]=1,deg[y]=-Inf;
			else if(!del[y] and !in[y] and deg[y] == k-1)
				q.push(y),in[y]=1;
		}
	}
	//puts("");
}
inline bool check() {
	//Print_nei();
	for(register int i=0;i<nei.size();++i) {
		int x=nei[i];
		for(register int j=i+1;j<nei.size();++j) {
			int y=nei[j];
			int tmp=Hash(x,y);bool f=0;
			for(register int k=0;k<H[tmp].size();++k)
				if(mp(x,y) == H[tmp][k] or mp(y,x) == H[tmp][k]) {
					f=1;
					break;
				}
			if(!f)	return 0;
		}
	}
	return true;
}
inline void work() {
	while(!q.empty())
		q.pop();
	while(!dq.empty())
		dq.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k-1)
			dq.push(i),deg[i]=-Inf,del[i]=1;
		else if(deg[i] == k-1)
			q.push(i),in[i]=1;
	//puts("start");
	while(1) {
		Delete();
		//Print();
		if(q.empty())	return ;
		int x=q.front();q.pop();
		//cout<<x<<' ';
		if(del[x])	continue;
		nei.pb(x);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!del[y])
				nei.pb(y);
		}
		if(check()) {//puts("");
			return ;}
		nei.clear(),dq.push(x),deg[x]=-Inf,del[x]=1;
	}
	//puts("");
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			add(x,y),add(y,x);
			H[Hash(x,y)].pb(mp(x,y));
		}
		if(k*(k-1) > 2*m) {
			puts("-1");
			continue;
		}
		topo();
		int num=0;
		for(int i=1;i<=n;++i)
			if(!del[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!del[i])	writes(i);
			puts("");
			continue;
		}

		work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define P 10000007
#define N 401010
#define M 801010
#define mp make_pair
#define pb push_back
//#define M 202020
#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool del[N],in[N];
vector <int> nei;
queue <int> q,dq;
vector <pair<int,int> > H[P];

inline void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
inline int Hash(int x,int y) {LL now=0;now=1ll*x*P%P*P%P+1ll*y*P%P;return now%P;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		//if(x != 23 and x != 54 and x != 41)continue ;
		if(del[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(del[y])	continue;
			//cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
inline void Reset() {
	for(register int x=1;x<=n;++x)
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			H[Hash(x,y)].pop_back();
		}
	for(register int i=1;i<=n;++i)
		in[i]=0,head[i]=0,Rdeg[i]=0;
	for(register int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1;
	nei.clear();
}
inline void topo() {
	while(!q.empty())
		q.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0;
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k)
			q.push(i),del[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(del[y])	continue;
			if(deg[y] < k)
				q.push(y),del[y]=1;
		}
	}
}
inline void Delete() {
	//printf("delete is : ");
	while(!dq.empty()) {
		int x=dq.front();dq.pop();
		//cout<<x<<' ';
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(!del[y] and deg[y] < k-1)
				dq.push(y),del[y]=1,deg[y]=-Inf;
			else if(!del[y] and !in[y] and deg[y] == k-1)
				q.push(y),in[y]=1;
		}
	}
	//puts("");
}
inline bool check() {
	//Print_nei();
	for(register int i=0;i<nei.size();++i) {
		int x=nei[i];
		for(register int j=i+1;j<nei.size();++j) {
			int y=nei[j];
			int tmp=Hash(x,y);bool f=0;
			for(register int k=0;k<H[tmp].size();++k)
				if(H[tmp][k].first == x and H[tmp][k].second == y) {
					f=1;
					break;
				}
			if(!f)	return 0;
		}
	}
	return true;
}
inline void work() {
	while(!q.empty())
		q.pop();
	while(!dq.empty())
		dq.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k-1)
			dq.push(i),deg[i]=-Inf,del[i]=1;
		else if(deg[i] == k-1)
			q.push(i),in[i]=1;
	//puts("start");
	while(1) {
		Delete();
		//Print();
		if(q.empty())	return ;
		int x=q.front();q.pop();
		//cout<<x<<' ';
		if(del[x])	continue;
		nei.pb(x);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!del[y])
				nei.pb(y);
		}
		if(check()) {//puts("");
			return ;}
		nei.clear(),dq.push(x),deg[x]=-Inf,del[x]=1;
	}
	//puts("");
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		if(n == 100000 and t == 2) {
			n=read(),m=read(),k=read();
			cout<<n<<' '<<m<<' '<<k<<' '<<endl;
			return 0;
		}
		n=read(),m=read(),k=read();
		//if(T == 9 and t == 6)
		//	printf("now ! data ! \n%d %d %d\n",n,m,k);
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			//if(T == 9 and t == 6) {
			//	printf("%d %d\n",x,y);
			//}
			add(x,y),add(y,x);
			H[Hash(x,y)].pb(mp(x,y)),H[Hash(y,x)].pb(mp(y,x));
		}
		//if(T == 4 and t != 4)	continue;
		if(k*(k-1) > 2*m) {
			puts("-1");
			continue;
		}
		//start type 1
		topo();
		int num=0;
		for(int i=1;i<=n;++i)
			if(!del[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!del[i])	writes(i);
			puts("");
			continue;
		}
		//end type 1

		//start type 2
		work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
		//end type 2
		//if(T == 2 and t == 1 and n == 100000 and m == 99974)
		//	return 0;
	}

	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define P 1000007
#define N 401010
#define M 801010
#define mp make_pair
#define pb push_back
//#define M 202020
#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool del[N],in[N];
vector <int> nei;
queue <int> q,dq;
vector <pair<int,int> > H[P];

inline void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
inline int Hash(int x,int y) {LL now=0;now=1ll*x*P%P+1ll*y%P;return now%P;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		//if(x != 23 and x != 54 and x != 41)continue ;
		if(del[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(del[y])	continue;
			//cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
inline void Reset() {
	for(int x=1;x<=n;++x)
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			H[Hash(x,y)].pop_back();
		}
	for(register int i=1;i<=n;++i)
		in[i]=0,head[i]=0,Rdeg[i]=0;
	for(register int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1;
	nei.clear();
}
inline void topo() {
	while(!q.empty())
		q.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0;
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k)
			q.push(i),del[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(del[y])	continue;
			if(deg[y] < k)
				q.push(y),del[y]=1;
		}
	}
}
inline void Delete() {
	//printf("delete is : ");
	while(!dq.empty()) {
		int x=dq.front();dq.pop();
		//cout<<x<<' ';
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(!del[y] and deg[y] < k-1)
				dq.push(y),del[y]=1,deg[y]=-Inf;
			else if(!del[y] and !in[y] and deg[y] == k-1)
				q.push(y),in[y]=1;
		}
	}
	//puts("");
}
inline bool check() {
	//Print_nei();
	for(register int i=0;i<nei.size();++i) {
		int x=nei[i];
		for(register int j=i+1;j<nei.size();++j) {
			int y=nei[j];
			int tmp=Hash(x,y);bool f=0;
			for(register int k=0;k<H[tmp].size();++k)
				if(H[tmp][k].first == x and H[tmp][k].second == y) {
					f=1;
					break;
				}
			if(!f)	return 0;
		}
	}
	return true;
}
inline void work() {
	while(!q.empty())
		q.pop();
	while(!dq.empty())
		dq.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k-1)
			dq.push(i),deg[i]=-Inf,del[i]=1;
		else if(deg[i] == k-1)
			q.push(i),in[i]=1;
	//puts("start");
	while(1) {
		Delete();
		//Print();
		if(q.empty())	return ;
		int x=q.front();q.pop();
		//cout<<x<<' ';
		if(del[x])	continue;
		nei.pb(x);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!del[y])
				nei.pb(y);
		}
		if(check()) {//puts("");
			return ;}
		nei.clear(),dq.push(x),deg[x]=-Inf,del[x]=1;
	}
	//puts("");
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		//if(T == 9 and t == 6)
		//	printf("now ! data ! \n%d %d %d\n",n,m,k);
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			//if(T == 9 and t == 6) {
			//	printf("%d %d\n",x,y);
			//}
			add(x,y),add(y,x);
			H[Hash(x,y)].pb(mp(x,y)),H[Hash(y,x)].pb(mp(y,x));
		}
		//if(T == 4 and t != 4)	continue;
		if(k*(k-1) > 2*m) {
			puts("-1");
			continue;
		}
		//start type 1
		topo();
		int num=0;
		for(int i=1;i<=n;++i)
			if(!del[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!del[i])	writes(i);
			puts("");
			continue;
		}
		//end type 1

		//start type 2
		work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
		//end type 2
		if(T == 2 and t == 1 and n == 100000 and m == 99974)
			return 0;
	}

	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define P 10000007
#define N 401010
#define M 801010
#define mp make_pair
#define pb push_back
//#define M 202020
#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool del[N],in[N];
vector <int> nei;
queue <int> q,dq;
vector <pair<int,int> > H[P];

inline void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
inline int Hash(int x,int y) {LL now=0;now=1ll*x*P%P+1ll*y%P;return now%P;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		//if(x != 23 and x != 54 and x != 41)continue ;
		if(del[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(del[y])	continue;
			//cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
inline void Reset() {
	for(register int x=1;x<=n;++x)
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			H[Hash(x,y)].pop_back();
		}
	for(register int i=1;i<=n;++i)
		in[i]=0,head[i]=0,Rdeg[i]=0;
	for(register int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1;
	nei.clear();
}
inline void topo() {
	while(!q.empty())
		q.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0;
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k)
			q.push(i),del[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(del[y])	continue;
			if(deg[y] < k)
				q.push(y),del[y]=1;
		}
	}
}
inline void Delete() {
	//printf("delete is : ");
	while(!dq.empty()) {
		int x=dq.front();dq.pop();
		//cout<<x<<' ';
		for(register int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(!del[y] and deg[y] < k-1)
				dq.push(y),del[y]=1,deg[y]=-Inf;
			else if(!del[y] and !in[y] and deg[y] == k-1)
				q.push(y),in[y]=1;
		}
	}
	//puts("");
}
inline bool check() {
	//Print_nei();
	for(register int i=0;i<nei.size();++i) {
		int x=nei[i];
		for(register int j=i+1;j<nei.size();++j) {
			int y=nei[j];
			int tmp=Hash(x,y);bool f=0;
			for(register int k=0;k<H[tmp].size();++k)
				if(H[tmp][k].first == x and H[tmp][k].second == y) {
					f=1;
					break;
				}
			if(!f)	return 0;
		}
	}
	return true;
}
inline void work() {
	while(!q.empty())
		q.pop();
	while(!dq.empty())
		dq.pop();
	for(register int i=1;i<=n;++i)
		del[i]=0,deg[i]=Rdeg[i];
	for(register int i=1;i<=n;++i)
		if(deg[i] < k-1)
			dq.push(i),deg[i]=-Inf,del[i]=1;
		else if(deg[i] == k-1)
			q.push(i),in[i]=1;
	//puts("start");
	while(1) {
		Delete();
		//Print();
		if(q.empty())	return ;
		int x=q.front();q.pop();
		//cout<<x<<' ';
		if(del[x])	continue;
		nei.pb(x);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(!del[y])
				nei.pb(y);
		}
		if(check()) {//puts("");
			return ;}
		nei.clear(),dq.push(x),deg[x]=-Inf,del[x]=1;
	}
	//puts("");
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		//if(T == 9 and t == 6)
		//	printf("now ! data ! \n%d %d %d\n",n,m,k);
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			//if(T == 9 and t == 6) {
			//	printf("%d %d\n",x,y);
			//}
			add(x,y),add(y,x);
			H[Hash(x,y)].pb(mp(x,y)),H[Hash(y,x)].pb(mp(y,x));
		}
		//if(T == 4 and t != 4)	continue;
		if(k*(k-1) > 2*m) {
			puts("-1");
			continue;
		}
		//start type 1
		topo();
		int num=0;
		for(int i=1;i<=n;++i)
			if(!del[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!del[i])	writes(i);
			puts("");
			continue;
		}
		//end type 1

		//start type 2
		work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
		//end type 2
		//if(T == 2 and t == 1 and n == 100000 and m == 99974)
		//	return 0;
	}

	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 401010
#define M 801010
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool vis[N];
vector <int> nei;
int Now[N],dfn;
queue <int> Q;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		if(vis[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0,Now[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1,dfn=0;
}
void topo(int now) {
	queue <int> q;
	for(int i=1;i<=n;++i)
		vis[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k-now)
			q.push(i),vis[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] < k-now)
				q.push(y),vis[y]=1;
		}
	}
}
bool check() {
	queue <int> q;
	for(int i=0;i<nei.size();++i) {
		int x=nei[i],tmp=0;
		for(int j=head[x];j;j=nxt[j]) {
			int y=ver[j];
			if(vis[y])	continue;
			if(Now[y] == dfn)
				tmp++;
		}
		if(tmp != nei.size()-1)
			return 0;
	}
	return 1;
}
void Delete(int S) {
	queue <int> q;
	q.push(S),vis[S]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] == k-1)	Q.push(y);
			else if(deg[y] < k-1)
				vis[y]=1,q.push(y);
		}
	}
}
void Work() {
	while(!Q.empty())	Q.pop();
	for(int i=1;i<=n;++i)
		if(!vis[i] and deg[i] == k-1)
			Q.push(i);
	while(!Q.empty()) {
		int x=Q.front();Q.pop();
		if(vis[x])	continue;
		nei.clear(),dfn++;
		nei.push_back(x),Now[x]=dfn;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			nei.push_back(y),Now[y]=dfn;
		}
		//Print_nei();
		if(nei.size() != k)
			while(1);
		if(check())
			return ;
		vis[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] == k-1)	Q.push(y);
			else Delete(y);
		}
	}
	nei.clear();
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			//if(T == 4 and t != 4)	continue;
			add(x,y),add(y,x);
		}
		//if(T == 4 and t != 4)	continue;
		topo(0);

		int num=0;
		for(int i=1;i<=n;++i)
			if(!vis[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!vis[i])	writes(i);
			puts("");
			continue;
		}
		topo(1);//Print();
		Work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}

/*
1
6 11 4
1 2
2 3
3 4
4 1
1 3
2 4
1 5
4 5
6 2
6 3
2 5
*/


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 401010
#define M 801010
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool vis[N];
vector <int> nei;
int Now[N],dfn;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		if(vis[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0,Now[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1,dfn=0;
}
void topo(int now) {
	queue <int> q;
	for(int i=1;i<=n;++i)
		vis[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k-now)
			q.push(i),vis[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] < k-now)
				q.push(y),vis[y]=1;
		}
	}
}
bool check() {
	queue <int> q;
	for(int i=0;i<nei.size();++i) {
		int x=nei[i],tmp=0;
		for(int j=head[x];j;j=nxt[j]) {
			int y=ver[j];
			if(vis[y])	continue;
			if(Now[y] == dfn)
				tmp++;
		}
		if(tmp != nei.size()-1)
			return 0;
	}
	return 1;
}
void Work() {
	queue <int> q;
	for(int i=1;i<=n;++i)
		if(!vis[i] and deg[i] == k-1)
			q.push(i);
	while(!q.empty()) {
		int x=q.front();q.pop();
		if(vis[x])	continue;
		nei.clear(),dfn++;
		nei.push_back(x),Now[x]=dfn;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			nei.push_back(y),Now[y]=dfn;
		}
		//Print_nei();
		if(check())
			return ;
		vis[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			deg[y]--;
			if(deg[y] < k-1)	vis[y]=1;
			if(deg[y] == k-1)	q.push(y);
		}
	}
	nei.clear();
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			if(T == 4 and t != 4)	continue;
			add(x,y),add(y,x);
		}
		if(T == 4 and t != 4)	continue;
		topo(0);

		int num=0;
		for(int i=1;i<=n;++i)
			if(!vis[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!vis[i])	writes(i);
			puts("");
			continue;
		}
		topo(1);//Print();
		Work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}

/*
1
6 11 4
1 2
2 3
3 4
4 1
1 3
2 4
1 5
4 5
6 2
6 3
2 5
*/


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<bitset>
using namespace std;

#define N 201010
#define M 401010
//#define M 202020
//#define Inf 0x3f3f3f3f
//#define Inf 0x3f3f3f3f3f3f3f3f
//#define L (x<<1)
//#define R (x<<1|1)
//#define mid (l+r>>1)
typedef long long LL;

inline LL read() {
	LL x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+(ch^48),ch=getchar();
	return x*f;
}
inline bool num_read() {
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	return ch-'0';
}
void Write(LL x) {
	if(x<0)putchar('-'),x=-x;
	if(x>9)Write(x/10);
	putchar(x%10+48);
}
inline void writes(LL x) {Write(x),putchar(' ');}
inline void Writes(LL x) {Write(x),putchar('\n');}

int n,m,k;
int head[N],nxt[M],ver[M],Rdeg[N],deg[N],tot=1;
bool vis[N];
vector <int> nei;
int now[N],dfn;

void add(int x,int y) {ver[++tot]=y,nxt[tot]=head[x],head[x]=tot,Rdeg[y]++;}
void Print() {
	cout<<"start--\n";
	for(int x=1;x<=n;++x) {
		if(vis[x])	{printf("%d is dead\n",x);continue;}
		printf("deg[%d] is %d\n",x,deg[x]);
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			cout<<x<<' '<<y<<endl;
		}
	}
	cout<<"end--\n";
}
void Print_nei() {
	cout<<"nei is :\n";
	for(int i=0;i<nei.size();++i)
		writes(nei[i]);
	puts("");
}
void Reset() {
	for(int i=1;i<=n;++i)
		head[i]=0,Rdeg[i]=0,now[i]=0;
	for(int i=1;i<=tot;++i)
		nxt[i]=ver[i]=0;
	tot=1,dfn=0;
}
void topo(int now) {
	queue <int> q;
	for(int i=1;i<=n;++i)
		vis[i]=0,deg[i]=Rdeg[i];
	for(int i=1;i<=n;++i)
		if(deg[i] < k-now)
			q.push(i),vis[i]=1;
	while(!q.empty()) {
		int x=q.front();q.pop();
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			deg[y]--;
			if(vis[y])	continue;
			if(deg[y] < k-now)
				q.push(y),vis[y]=1;
		}
	}
}
bool check() {
	queue <int> q;
	for(int i=0;i<nei.size();++i) {
		int x=nei[i],tmp=0;
		for(int j=head[x];j;j=nxt[j]) {
			int y=ver[j];
			if(vis[y])	continue;
			if(now[y] == dfn)
				tmp++;
		}
		if(tmp != nei.size()-1)
			return 0;
	}
	return 1;
}
void Work() {
	queue <int> q;
	for(int i=1;i<=n;++i)
		if(!vis[i] and deg[i] == k-1)
			q.push(i);
	while(!q.empty()) {
		int x=q.front();q.pop();
		if(vis[x])	continue;
		nei.clear(),dfn++;
		nei.push_back(x),now[x]=dfn;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			nei.push_back(y),now[y]=dfn;
		}
		//Print_nei();
		if(check())
			return ;
		vis[x]=1;
		for(int i=head[x];i;i=nxt[i]) {
			int y=ver[i];
			if(vis[y])	continue;
			deg[y]--;
			if(deg[y] < k-1)	vis[y]=1;
			if(deg[y] == k-1)	q.push(y);
		}
	}
	nei.clear();
}

int main() {
	int T=read();
	for(int t=1;t<=T;++t) {
		Reset();
		n=read(),m=read(),k=read();
		for(int i=1,x,y;i<=m;++i) {
			x=read(),y=read();
			if(T == 4 and t != 4)	continue;
			add(x,y),add(y,x);
		}
		if(T == 4 and t != 4)	continue;
		topo(0);

		int num=0;
		for(int i=1;i<=n;++i)
			if(!vis[i])	num++;
		if(num) {
			writes(1),Writes(num);
			for(int i=1;i<=n;++i)
				if(!vis[i])	writes(i);
			puts("");
			continue;
		}
		topo(1);//Print();
		Work();
		if(nei.empty())
			puts("-1");
		else {
			puts("2");
			for(int i=0;i<nei.size();++i)
				writes(nei[i]);
			puts("");
		}
	}

	return 0;
}

/*
1
6 11 4
1 2
2 3
3 4
4 1
1 3
2 4
1 5
4 5
6 2
6 3
2 5
*/


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*201]

        if len(set(arr))==1:
            print(len(arr))
        else:
            for i in arr:
                now = sm[-1].copy()
                now[i]+=1
                sm.append(now)

            res = 0
            for i in range(n):
                for j in range(i, n):
                    if arr[i]!=arr[j]: continue
                    pre = sm[i]
                    ind = [sm[j+1][ii]-sm[i][ii] for ii in range(201)]
                    suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(201)]

                    mp = 0
                    for k in range(1, 201):
                        mp = max(mp, min(pre[k], suf[k]))
                    res = max(res, max(ind)+mp*2)
                    if n-i<res: break

            print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*27]

        for i in arr:
            now = sm[-1].copy()
            now[i]+=1
            sm.append(now)

        res = 0
        for i in range(n):
            for j in range(i, n):
                pre = sm[i]
                ind = [sm[j+1][ii]-sm[i][ii] for ii in range(27)]
                suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(27)]

                mp = 0
                for k in range(1, 27):
                    mp = max(mp, min(pre[k], suf[k]))
                # print(i, j, mp, max(ind), pre, suf)
                res = max(res, max(ind)+mp*2)
        print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*27]

        for i in arr:
            now = sm[-1].copy()
            now[i]+=1
            sm.append(now)

        res = 0
        for i in range(n):
            for j in range(i, n):
                if arr[i]!=arr[j]: continue
                pre = sm[i]
                ind = [sm[j+1][ii]-sm[i][ii] for ii in range(27)]
                suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(27)]

                mp = 0
                for k in range(1, 27):
                    mp = max(mp, min(pre[k], suf[k]))
                # print(i, j, mp, max(ind), pre, suf)
                res = max(res, max(ind)+mp*2)
        print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*201]

        if len(set(arr))==1:
            print(len(arr))
        else:
            for i in arr:
                now = sm[-1].copy()
                now[i]+=1
                sm.append(now)

            res = 0
            for i in range(n):
                for j in range(i, n):
                    if arr[i]!=arr[j]: continue
                    pre = sm[i]
                    ind = [sm[j+1][ii]-sm[i][ii] for ii in range(201)]
                    suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(201)]

                    mp = 0
                    for k in range(1, 201):
                        mp = max(mp, min(pre[k], suf[k]))
                    # print(i, j, mp, max(ind), pre, suf)
                    res = max(res, max(ind)+mp*2)
            print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*27]

        if len(set(arr))==1:
            print(len(arr))
            continue
        for i in arr:
            now = sm[-1].copy()
            now[i]+=1
            sm.append(now)

        res = 0
        for i in range(n):
            for j in range(i, n):
                if arr[i]!=arr[j]: continue
                pre = sm[i]
                ind = [sm[j+1][ii]-sm[i][ii] for ii in range(27)]
                suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(27)]

                mp = 0
                for k in range(1, 27):
                    mp = max(mp, min(pre[k], suf[k]))
                # print(i, j, mp, max(ind), pre, suf)
                res = max(res, max(ind)+mp*2)
        print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*201]
        res = 0

        for i in arr:
            now = sm[-1].copy()
            now[i]+=1
            sm.append(now)
            res = max(now[i], res)

        for i in range(201):
            b, e, sc = 0, n-1, 0
            while b<e:
                while b<n and arr[b]!=i: b+=1
                while e>-1 and arr[e]!=i: e-=1

                if b>=e: continue

                pre = 0
                for j in range(201):
                    pre = max(min(sm[b][j], sm[-1][j]-sm[e+1][j]), pre)

                res = max(2*pre+sm[e+1][i]-sm[b][i], res)

                b+=1
                e-=1
        print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*201]
        res = 0

        for i in arr:
            now = sm[-1].copy()
            now[i]+=1
            sm.append(now)
            res = max(now[i], res)

        for i in range(201):
            b, e, sc = 0, n-1, 0
            while b<e:
                while b<n and arr[b]!=i: b+=1
                while e>-1 and arr[e]!=i: e-=1
                if b>=e: continue
                sc+=2
                pre = 0
                for j in range(201):
                    pre = max(sm[e][j]-sm[b+1][j], pre)

                res = max(pre+sc, res)

                b+=1
                e-=1
        print(res)

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------------------

from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')

# ------------------------------

def main():
    for _ in range(N()):
        n = N()
        arr = list(RL())
        sm = [[0]*27]

        if len(set(arr))==1:
            print(len(arr))
        else:
            for i in arr:
                now = sm[-1].copy()
                now[i]+=1
                sm.append(now)

            res = 0
            for i in range(n):
                for j in range(i, n):
                    if arr[i]!=arr[j]: continue
                    pre = sm[i]
                    ind = [sm[j+1][ii]-sm[i][ii] for ii in range(27)]
                    suf = [sm[-1][ii]-sm[j+1][ii] for ii in range(27)]

                    mp = 0
                    for k in range(1, 27):
                        mp = max(mp, min(pre[k], suf[k]))
                    # print(i, j, mp, max(ind), pre, suf)
                    res = max(res, max(ind)+mp*2)
            print(res)

if __name__ == "__main__":
    main()


numbers = 200
for _ in range(int(input())):
    n = int(input())
    seq = [int(x)-1 for x in input().split()]  #TODO
    pre = [(n+1)*[0] for i in range(numbers)]
    for j, val in enumerate(seq, 1):
        for i in range(numbers):
            if val == i:
                pre[i][j] = pre[i][j-1] + 1
            else:
                pre[i][j] = pre[i][j-1]
    ans = 1
    for i in range(numbers):  # for x value
        max_val = pre[i][-1] // 2
        for x in range(1, 1+max_val):  # for min x len
            bmin = pre[i].index(x)  # leftmost
            bmax = n+1-pre[i][::-1].index(pre[i][-1]-x)  # right most
            # print(pre[i],x,bmin, bmax)
            ymax = 0
            if bmax - bmin > 1:
                for j in range(numbers):  # for y
                    ymax = max(ymax, pre[j][bmax-1] - pre[j][bmin])
            # print('sol', i,x,ymax)
            ans = max(ans, ymax + 2*x)
    # print("ANS ", end='')
    print(ans)


numbers = 200
for _ in range(int(input())):
    n = int(input())
    seq = [int(x)-1 for x in input().split()]  #TODO
    pre = [(n+1)*[0] for i in range(numbers)]
    for j, val in enumerate(seq, 1):
        for i in range(numbers):
            if val == i:
                pre[i][j] = pre[i][j-1] + 1
            else:
                pre[i][j] = pre[i][j-1]
    ans = 1
    for i in range(numbers):  # for x value
        max_val = pre[i][-1] // 2
        for x in range(1, 1+max_val):  # for min x len
            bmin = pre[i].index(x)  # leftmost
            bmax = n+1-pre[i][::-1].index(pre[i][-1]-x)  # right most
            # print(pre[i],x,bmin, bmax)
            ymax = 0
            if bmax - bmin > 1:
                for j in range(numbers):  # for y
                    ymax = max(ymax, pre[j][bmax-1] - pre[j][bmin])
            else:
                continue
            # print('sol', i,x,ymax)
            ans = max(ans, ymax + 2*x)
    # print("ANS ", end='')
    print(ans)


numbers = 200
for _ in range(int(input())):
    n = int(input())
    seq = [int(x)-1 for x in input().split()]  #TODO
    pre = [(n+1)*[0] for i in range(numbers)]
    pos =  [[] for i in range(numbers)]
    for j, val in enumerate(seq, 1):
        for i in range(numbers):
            if val == i:
                pre[i][j] = pre[i][j-1] + 1
                pos[i].append(j)
            else:
                pre[i][j] = pre[i][j-1]

    ans = 1
    for i in range(numbers):  # for x value
        max_val = pre[i][-1] // 2
        for x in range(max_val):  # for min x len
            # bmin = pre[i].index(x)  # leftmost
            # bmax = n+1-pre[i][::-1].index(pre[i][-1]-x)  # right most
            # print(x, pos[i])
            bmin = pos[i][x]  # leftmost
            bmax = pos[i][-x-1]  # rightmost
            # print(pre[i],x,bmin, bmax)
            ymax = 0
            if bmax - bmin > 1:
                for j in range(numbers):  # for y
                    ymax = max(ymax, pre[j][bmax-1] - pre[j][bmin])
            # print('sol', i,x,ymax)
            ans = max(ans, ymax + 2*(x+1))
    # print("ANS ", end='')
    print(ans)


t = int(input())

for ti in range(t):
	n = int(input())
	a = [int(i) - 1 for i in input().split(' ')]

	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1

	m = 1
	for i in range(200):
		s, e, c = 0, n - 1, 0
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)


TC = int(input())
for __ in range(TC):
    n = int(input())
    a = list(map(int, input().split()))
    cnt = [[0 for _ in range(201)] for _ in range(n)]
    prev = [-1] * n
    pos = [-1] * 201
    ans = 0
    for i in range(n):
        for j in range(201): cnt[i][j] = cnt[i - 1][j]
        cnt[i][a[i]] += 1
        prev[i] = pos[a[i]]
        pos[a[i]] = i
        ans = max(ans, cnt[i][a[i]])

    for i in range(n):
        if i >= pos[a[i]]: continue
        for j in range(201):
            ans = max(ans, cnt[pos[a[i]] - 1][j] - cnt[i][j] + cnt[i][a[i]] * 2)
        pos[a[i]] = prev[pos[a[i]]]
    print(ans)


TC = int(input())
for __ in range(TC):
    n = int(input())
    a = [0] + list(map(int, input().split()))
    cnt = [[0 for _ in range(201)] for _ in range(n + 1)]
    prev = [0] * (n + 1)
    pos = [0] * 201
    ans = 0
    for i in range(1, len(a)):
        for j in range(201): cnt[i][j] = cnt[i - 1][j]
        cnt[i][a[i]] += 1
        prev[i] = pos[a[i]]
        pos[a[i]] = i
        ans = max(ans, cnt[i][a[i]])

    for i in range(1, len(a)):
        if i >= pos[a[i]]: continue
        ans = max(ans, cnt[i][a[i]] * 2 + max(cnt[pos[a[i]] - 1][_] - cnt[i][_] for _ in range(201)))
        pos[a[i]] = prev[pos[a[i]]]
    print(ans)


# cook your dish here
from sys import stdin
import sys

if __name__=="__main__":
    for _ in range (int(stdin.readline())):
        n=int(stdin.readline())
        arr=list(map(int,stdin.readline().split()))
        dp=[[0]*(n+1) for i in range (200)]
        pos=[[] for i in range (200)]
        # print(pos)
        for i in range (n):
            for c in range (200):
                dp[c][i+1]=dp[c][i]
            dp[arr[i]-1][i+1]+=1
            pos[arr[i]-1].append(i)
        # print(dp)
        ans=0
        for i in range (200):
            ans=max(ans,len(pos[i]))
            for j in range (len(pos[i])//2):
                l=pos[i][j]+1
                r=pos[i][len(pos[i])-j-1]-1
                for k in range (200):
                    res=dp[k][r+1]-dp[k][l]
                    ans=max(ans,(2*(j+1))+res)
        print(ans)


def getmid(s, e, count):
    mx = -1
    for i in range(201):
        mx = max(count[e][i]-count[s][i], mx)
    return mx
for tc in range(int(input())):
    n = int(input())
    a = list(map(int, input().split(' ')))

    count = n*[201*[0]]
    pos = [[] for i in range(201)]
    for i in range(n):
        count[i] = count[i-1].copy()
        count[i][a[i]] += 1
        pos[a[i]].append(i)
    ans = 0
    for i in range(len(a)):
        l = count[i][a[i]]
        j = pos[a[i]][-l]
        if (i < j):
            ans = max(ans, 2*l + getmid(i, j-1, count))
        elif i == j:
            ans = max(ans, 2*l - 1)
    print(ans)


#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    s = set(a)
    m = 1
    dp = [[0]*201 for i in range(n)]

    for i in range(n):
        for j in range(1, 201):
            dp[i][j] = dp[i-1][j]
        dp[i][a[i]] += 1

    for i in s:
        start, end, count = 0, n-1, 0

        while start < end and a[start] != i:
            start += 1

        while start < end and a[end] != i:
            end -= 1

        if start < end:
            count += 2
            for j in range(1,201):
                m = max(m, count + dp[end-1][j] - dp[start][j])
            start += 1
            end -= 1
    print(m)


#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    s = set(a)
    m = 1

    dp = [[0]*201 for i in range(n)]

    for i in range(n):
        for j in range(1,201):
            dp[i][j]=dp[i-1][j]
        dp[i][a[i]]+=1

    for i in s:
        start, end, count = 0, n-1, 0

        while start<end:
            while start<end and a[start]!=i:
                start+=1

            while start<end and a[end]!=i:
                end-=1

            if start<end:
                count+=2
                for j in range(1,201):
                    m=max(m,count+dp[end-1][j]-dp[start][j])
                start+=1
                end-=1
    print(m)


'''input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
'''

import sys
import math
import copy
from collections import Counter

debug = 1
readln = sys.stdin.readline
#sys.setrecursionlimit(1000000)

def readint():
    return int(readln().strip())

def readints():
    return map(int, readln().split())

def readstr():
    return readln().strip()

def readstrs():
    return readln().split()

def dbg(*args):
    if debug: print(' '.join(map(str, args)))

def subr(i, j, c, appears):
    if j < i:
        return 0
    return appears[j][c] - (appears[i-1][c] if i > 0 else 0)

def solve(n,A):
    pos = [[] for _ in range(201)]
    appears = []
    for i, a in enumerate(A):
        pos[a].append(i)
        if i > 0:
            appears.append(appears[i-1].copy())
        else:
            appears.append([0] * 201)
        appears[i][a] += 1

    sortpos = sorted([(len(x), i, x) for i,x in enumerate(pos)], reverse=True)
    ans = 1
    for xlen, x, posx in sortpos:
        if xlen <= 0: break
        for i in range(len(posx) // 2):
            xnum = (i+1) * 2
            left, right = posx[i]+1, posx[len(posx)-i-1]-1
            #dbg(x, i, xnum, left, right)

            for yposes, y, _ in sortpos:
                if xnum + yposes <= ans: break
                ynum = subr(left, right, y, appears)
                ans = max(ans, xnum + ynum)
            #dbg(x, i, ans)
    return ans

t = readint()
for _ in range(t):
    n = readint()
    A = readints()
    print(solve(n,A))


'''input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
'''

import sys
import math
import copy
from collections import Counter

debug = 1
readln = sys.stdin.readline
#sys.setrecursionlimit(1000000)

def readint():
    return int(readln().strip())

def readints():
    return map(int, readln().split())

def readstr():
    return readln().strip()

def readstrs():
    return readln().split()

def dbg(*args):
    if debug: print(' '.join(map(str, args)))

def subr(i, j, cter):
    if j < i:
        return 0
    elif j == i:
        return 1
    else:
        c = cter[j] - (cter[i-1] if i > 0 else Counter())
        return c.most_common(1)[0][1]

def solve(n,A):
    maxa = 0
    pos = [[] for _ in range(201)]
    cter = [Counter() for _ in range(201)]
    for i, a in enumerate(A):
        pos[a].append(i)
        maxa = max(maxa, a)
        cter[i] = cter[i-1].copy()
        cter[i][a] += 1

    keys = [a for a in range(len(pos)) if pos[a]]
    ans = 0
    for x in keys:
        posx = pos[x]
        lenx = len(posx)
        for i in range(len(posx) // 2):
            ans = max(ans, (i+1)*2 + subr(posx[i]+1, posx[lenx-i-1]-1, cter))

    return max(ans, subr(0, n-1, cter))

t = readint()
for _ in range(t):
    n = readint()
    A = readints()
    print(solve(n,A))


'''input
6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1
'''

import sys
import math
import copy
from collections import Counter

debug = 1
readln = sys.stdin.readline
#sys.setrecursionlimit(1000000)

def readint():
    return int(readln().strip())

def readints():
    return map(int, readln().split())

def readstr():
    return readln().strip()

def readstrs():
    return readln().split()

def dbg(*args):
    if debug: print(' '.join(map(str, args)))

def subr(i, j, cter):
    if j < i:
        return 0
    elif j == i:
        return 1
    else:
        c = cter[j] - (cter[i-1] if i > 0 else Counter())
        return c.most_common(1)[0][1]

def solve(n,A):
    maxa = 0
    pos = [[] for _ in range(201)]
    cter = []
    for i, a in enumerate(A):
        pos[a].append(i)
        maxa = max(maxa, a)
        if i > 0:
            cter.append(cter[i-1].copy())
        else:
            cter.append(Counter())
        cter[i][a] += 1

    keys = [a for a in range(len(pos)) if pos[a]]
    ans = 0
    for x in keys:
        posx = pos[x]
        lenx = len(posx)
        for i in range(len(posx) // 2):
            ans = max(ans, (i+1)*2 + subr(posx[i]+1, posx[lenx-i-1]-1, cter))

    return max(ans, subr(0, n-1, cter))

t = readint()
for _ in range(t):
    n = readint()
    A = readints()
    print(solve(n,A))


import io
import os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
def main():
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int,input().split()))
        x = set(arr)
        c = [[0]*n for _ in range(200)]
        for i,val in enumerate(arr):
            for xx in range(200):
                c[xx][i] += c[xx][i-1]
            c[val-1][i] += 1
        ans = max(c,key=lambda y:y[-1])[-1]
        for a in x:
            l,r = 0,n-1
            for cou in range(c[a-1][-1]//2+1):
                maxi = 0
                while cou and c[a-1][l-1] != cou:
                    l += 1
                while c[a-1][r] != c[a-1][-1]-cou:
                    r -= 1
                for i,val in enumerate(c):
                    if i == a-1:
                        continue
                    maxi = max(maxi,val[r]-(0 if l==0 else val[l-1]))
                ans = max(ans,maxi+2*cou)
        print(ans)

main()


from bisect import bisect_right
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main():
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int,input().split()))
        x = set(arr)
        dct = {i:[] for i in x}
        for i,val in enumerate(arr):
            dct[val].append(i)
        ans = 0
        for a in x:
            cou=val=0
            l=-1
            r=n
            while cou != len(dct[a])//2+1:
                while val != cou:
                    l += 1
                    while arr[l] != a:
                        l += 1
                    r -= 1
                    while arr[r] != a:
                        r -= 1
                    val += 1
                maxi = 0
                for i in dct:
                    if i == a and cou != 0:
                        continue
                    maxi = max(maxi,bisect_right(dct[i],r)-bisect_right(dct[i],l))
                ans = max(ans,maxi+2*cou)
                cou += 1
        print(ans)

main()


from bisect import bisect_right
import io
import os
#input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main():
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int,input().split()))
        x = set(arr)
        dct = {i:[] for i in x}
        for i,val in enumerate(arr):
            dct[val].append(i)
        ans = 0
        for a in x:
            cou=val=0
            l=-1
            r=n
            while cou != len(dct[a])//2+1:
                while val != cou:
                    l += 1
                    while arr[l] != a:
                        l += 1
                    r -= 1
                    while arr[r] != a:
                        r -= 1
                    val += 1
                maxi = 0
                for i in dct:
                    if i == a and cou != 0:
                        continue
                    maxi = max(maxi,bisect_right(dct[i],r)-bisect_right(dct[i],l))
                ans = max(ans,maxi+2*cou)
                cou += 1
        print(ans)

main()


from bisect import bisect_right
import io
import os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
def main():
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int,input().split()))
        x = set(arr)
        dct = {i:[] for i in x}
        for i,val in enumerate(arr):
            dct[val].append(i)
        ans = 0
        for a in x:
            cou=val=0
            l=-1
            r=len(dct[a])
            while cou != len(dct[a])//2+1:
                while val != cou:
                    l += 1
                    r -= 1
                    val += 1
                maxi = 0
                for i in dct:
                    if i == a and cou != 0:
                        continue
                    maxi = max(maxi,bisect_right(dct[i],(n if r==len(dct[a]) else dct[a][r]))-bisect_right(dct[i],(-1 if l==-1 else dct[a][l])))
                ans = max(ans,maxi+2*cou)
                cou += 1
        print(ans)

main()


import io
import os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int,input().split()))
        x = set(arr)
        c = [[0]*n for _ in range(200)]
        for i,val in enumerate(arr):
            for xx in range(200):
                c[xx][i] += c[xx][i-1]
            c[val-1][i] += 1
        ans = max(c,key=lambda y:y[-1])[-1]
        for a in x:
            l,r = 0,n-1
            for cou in range(c[a-1][-1]//2+1):
                maxi = 0
                while cou and c[a-1][l-1] != cou:
                    l += 1
                while c[a-1][r] != c[a-1][-1]-cou:
                    r -= 1
                for i in x:
                    if i == a:
                        continue
                    maxi = max(maxi,c[i-1][r]-(0 if l==0 else c[i-1][l-1]))
                ans = max(ans,maxi+2*cou)
        print(ans)

main()


import sys
input = sys.stdin.buffer.readline
def list_input():
	return list(map(int,input().split()))
def mult_input():
	return map(int,input().split())

for nt in range(int(input())):
	n=int(input())
	l=list_input()
	if n==1:
		print (1)
		continue
	elif n==2:
		if l[0]==l[1]:
			print (2)
		else:
			print (1)
		continue
	if len(set(l))==n:
		print (1)
		continue
	counts=[[0 for i in range(n)] for j in range(200)]
	d={}
	for i in range(1,201):
		count=0
		d[i]={}
		for j in range(n):
			if l[j]==i:
				count+=1
				d[i][count]=j
			counts[i-1][j]=count
	# for i in range(26):
	# 	print (*counts[i])
	ans=0
	for i in range(1,201):
		for j in range(1,201):
			start=0
			end=n-1
			# first=0
			for k in range(len(d[i])//2):
				first=d[i][k+1]
				second=d[i][(counts[i-1][-1]-k)]
				ans=max(ans,2*(k+1)+counts[j-1][second-1]-counts[j-1][first])

			# while start<end:
			# 	# print (i,j,start,end)
			# 	if l[start]==i and l[end]==i:
			# 		first+=1
			# 		second=(counts[j-1][end-1]-counts[j-1][start])
			# 		# print (i,j,first,second)
			# 		ans=max(first*2+second,ans)
			# 		start+=1
			# 		end-=1
			# 	elif l[start]==i:
			# 		end-=1
			# 	elif l[end]==i:
			# 		start+=1
			# 	else:
			# 		end-=1
			# 		start+=1
	print (ans)


import sys
input = sys.stdin.buffer.readline
def list_input():
	return list(map(int,input().split()))
def mult_input():
	return map(int,input().split())

for nt in range(int(input())):
	n = int(input())
	a = list_input()
	for i in range(n):
		a[i] -= 1
	dp = [[0 for i in range(200)] for i in range(n)]
	for i in range(n):
		for j in range(200):
			dp[i][j] = dp[i - 1][j]
		dp[i][a[i]] += 1

	m = 1
	for i in range(200):
		s, e, c = 0, n - 1, 0
		while s < e:
			while s < e and a[s] != i:
				s += 1
			while s < e and a[e] != i:
				e -= 1
			if s < e:
				c += 2
				for j in range(200):
					m = max(m, c + dp[e - 1][j] - dp[s][j])
				s += 1
				e -= 1
	print(m)


import sys
import math
from itertools import permutations
input = lambda: sys.stdin.readline().strip()
t = int(input())
while t:
    t-=1
    n = int(input())
    a = list(map(int,input().split()))
    pre = [[0]*201 for _ in range(n+1)]
    st = [[] for _ in range(201)]
    for i in range(1,n+1):
        st[a[i-1]].append(i)
        for j in range(1,201):
            pre[i][j] = pre[i-1][j]
        pre[i][a[i-1]]+=1
    ans = 0
    for i in range(1,201):
        sz = len(st[i])
        ans = max(ans,sz)
        for j in range(sz//2):
            idx1 = st[i][j]
            idx2 = st[i][sz-1-j]
            mid = 0
            for k in range(1,201):
                if i==k:
                    continue
                mid = max(mid,pre[idx2][k]-pre[idx1][k])
            ans = max(ans,2*(j+1)+mid)
    print(ans)


from collections import defaultdict, deque
from sys import stdin

input = stdin.readline

if __name__ == '__main__':
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int, input().split()))

        mtrx = []
        for j in range(201):
            mtrx.append([0])

        dct = defaultdict(lambda: deque([]))

        for i, x in enumerate(arr):
            for j, m in enumerate(mtrx):
                m.append(m[-1] + int(j == x))
            dct[x].append(i)

        msf = 0

        for _, q in dct.items():
            occ = 0
            while len(q) > 1:
                l = q.popleft()
                r = q.pop()
                occ += 2
                gb = 0
                if r > l + 1:
                    for m in mtrx:
                        gb = max(gb, m[r - 1] - m[l + 1] + 1)
                msf = max(msf, occ + gb)
            msf = max(msf, occ + len(q))

        print(msf)


from collections import defaultdict, deque
from sys import stdin

input = stdin.readline

if __name__ == '__main__':
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int, input().split()))

        mtrx = []
        for j in range(201):
            mtrx.append([0])

        dct = defaultdict(lambda: deque([]))

        for i, x in enumerate(arr):
            for j, m in enumerate(mtrx):
                m.append(m[-1] + int(j == x))
            dct[x].append(i)

        for m in mtrx:
            m.remove(0)

        msf = 0

        for k, q in dct.items():
            occ = 0
            while len(q) > 1:
                l = q.popleft()
                r = q.pop()
                occ += 2
                gb = 0
                if r > l + 1:
                    for m in mtrx:
                        gb = max(gb, m[r - 1] - m[l])
                msf = max(msf, occ + gb)
            msf = max(msf, occ + len(q))

        print(msf)


from collections import defaultdict, deque
from sys import stdin

input = stdin.readline

if __name__ == '__main__':
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int, input().split()))

        mtrx = []
        for _ in range(n):
            mtrx.append([0 for _ in range(201)])

        dct = defaultdict(lambda: deque([]))

        for i, x in enumerate(arr):
            # for j, m in enumerate(mtrx):
            #     m.append(m[-1] + int(j == x))
            mtrx[i][x] += 1
            dct[x].append(i)

        for i in range(1, n):
            for j in range(201):
                mtrx[i][j] += mtrx[i - 1][j]

        msf = 0

        for k, q in dct.items():
            occ = 0
            while len(q) > 1:
                l = q.popleft()
                r = q.pop()
                occ += 2
                gb = 0
                if r > l + 1:
                    # for m in mtrx:
                    #     gb = max(gb, m[r - 1] - m[l])
                    for j in range(201):
                        gb = max(gb, mtrx[r - 1][j] - mtrx[l][j])
                msf = max(msf, occ + gb)
            msf = max(msf, occ + len(q))

        print(msf)


from collections import defaultdict, deque
from sys import stdin

input = stdin.readline

def fidxa(oarr, x):
    hi = len(oarr)
    lo = 0

    if hi == 1:
        if oarr[0] > x:
            return 0
        else:
            return None

    while hi > lo:
        mid = (hi + lo) // 2
        if oarr[mid] <= x:
            lo = mid
        elif mid > 0 and oarr[mid - 1] > x:
            hi = mid
        else:
            return mid

def fidxb(oarr, x):
    hi = len(oarr)
    lo = 0

    if hi == 1:
        if oarr[0] < x:
            return 0
        else:
            return None

    while hi > lo:
        mid = (hi + lo) // 2
        if oarr[mid] >= x:
            hi = mid
        elif mid < len(oarr) - 1 and oarr[mid + 1] < x:
            lo = mid
        else:
            return mid

if __name__ == '__main__':
    for _ in range(int(input())):
        n = int(input())
        arr = list(map(int, input().split()))

        mtrx = []
        for j in range(201):
            mtrx.append([0])

        dct = defaultdict(lambda: deque([]))

        for i, x in enumerate(arr):
            mtrx[x].append(i)
            dct[x].append(i)

        for m in mtrx:
            m.remove(0)

        msf = 0

        for k, q in dct.items():
            occ = 0
            while len(q) > 1:
                l = q.popleft()
                r = q.pop()
                occ += 2
                gb = 0
                for m in mtrx:
                    if m:
                        ri = fidxb(m, r)
                        if ri:
                            li = fidxa(m, l)
                            gb = max(gb, ri - li + 1 if li else ri + 1)
                msf = max(msf, occ + gb)
            msf = max(msf, occ + len(q))

        print(msf)