#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char ch=getchar();int x=0;bool f=0;
	for(;!isdigit(ch);ch=getchar())if(ch=='-')f=1;
	for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
	if(f){x=-x;}return x;
}
const int maxn=2e6+5;
int n,m,a[maxn],T,k;
int ans[maxn][3],sum=0;
deque<int>g[505];
bool check(){
	int u=1;
	for(int i=1;i<=sum;i++){
		if(ans[i][0]==1){
			int z=ans[i][1];
			if(!g[z].empty()&&g[z].back()==a[u])g[z].pop_back();
			else g[z].push_back(a[u]);
			u++;
		}
		else if(ans[i][0]==2){
			int x=ans[i][1],y=ans[i][2];
			if(g[x].empty()||g[y].empty())return 0;
			if(g[x].front()==g[y].front()){
				g[x].pop_front(),g[y].pop_front();
			}
			else return 0;
		}
	}
	for(int i=1;i<=n;i++){
		if(!g[i].empty())return 0;
	}
	return 1;
}
namespace solve1{
	int flag[maxn],top=0;
	int id[maxn],st[maxn];
	
	void work(){
		top=sum=0;
		for(int i=1;i<n;i++)st[++top]=i+n,st[++top]=i;
		for(int i=1;i<=m;i++){
			a[i]=read();
			if(!flag[a[i]]){
				flag[a[i]]=st[top];id[st[top]]=a[i];
				ans[++sum][0]=1,ans[sum][1]=st[top];
				if(ans[sum][1]>n)ans[sum][1]-=n;
				top--;
			}else{
				int x=flag[a[i]];
				if(flag[a[i]]>n){
					x-=n;
					ans[++sum][0]=1,ans[sum][1]=x;
					st[++top]=x+n;flag[a[i]]=0;id[x+n]=0;
					
				}else{
					ans[++sum][0]=1,ans[sum][1]=n;
					ans[++sum][0]=2,ans[sum][1]=x;ans[sum][2]=n;
					flag[a[i]]=0;
					if(id[x+n])flag[id[x+n]]=x,id[x]=id[x+n],id[x+n]=0,st[++top]=x+n;
					else st[++top]=x,id[x]=0;
				}
			}
		}
		cout<<sum<<endl;
		for(int i=1;i<=sum;i++){
			printf("%d ",ans[i][0]);
			for(int j=1;j<=ans[i][0];j++)printf("%d ",ans[i][j]);
			puts("");
		}
	}
}
namespace solve2{
	int flag[maxn],b[maxn],tot=0;
	int id[5],f[5],st[maxn];
	int cnt;
	void work(){
		cnt=tot=0;int top=0;
		for(int i=1;i<=m;i++)a[i]=read(),flag[i]=0;
		for(int i=1;i<=m;i++){
			if(top&&a[st[top]]==a[i]){
				flag[i]=1;flag[st[top]]=1;
				top--;
			}else st[++top]=i;
		}int cnt=0;tot=top;
		for(int i=1;i<=tot;i++)b[i]=a[st[i]];
		for(int i=1;i<=m;i++){
			if(flag[i]){
				ans[++sum][0]=1,ans[sum][1]=n;
				continue;
			}cnt++;
		//	cout<<i<<" "<<id[1]<<" "<<b[cnt]<<endl;
			if(id[1]==b[cnt]){
				id[1]=0;
				
				ans[++sum][0]=1,ans[sum][1]=1;
			}
			else if(!id[2]||id[2]==a[i]){
				if(id[2])id[2]=0;else id[2]=a[i];
			//	cout<<i<<endl;
				ans[++sum][0]=1,ans[sum][1]=2;
			}
			else if(!id[1]){
				id[1]=a[i];
				ans[++sum][0]=1,ans[sum][1]=1;
			}
			else {
				if(b[cnt+1]==id[1]){
					ans[++sum][0]=1,ans[sum][1]=2;
				}
				else if(b[cnt+1]==id[2]){
					ans[++sum][0]=1,ans[sum][1]=1;
				}int x=a[i];
				int l=i+1;
				while(flag[l]){
					l++;ans[++sum][0]=1,ans[sum][1]=n;
				}cnt++;l++;
				if(b[cnt]==id[1]){
					id[1]=0;
					ans[++sum][0]=1,ans[sum][1]=1;
					cnt++;
					if(b[cnt]==id[2]){
						ans[++sum][0]=1,ans[sum][1]=1;
						ans[++sum][0]=2,ans[sum][1]=1;ans[sum][2]=2;
						id[2]=x;
					}else {
						ans[++sum][0]=1,ans[sum][1]=2;
					}
				}
				if(b[cnt]==id[2]){
					id[2]=0;
					ans[++sum][0]=1,ans[sum][1]=2;
					cnt++;
					if(b[cnt]==id[1]){
						ans[++sum][0]=1,ans[sum][1]=2;
						ans[++sum][0]=2,ans[sum][1]=1;ans[sum][2]=2;
						id[1]=x;
					}else {
						ans[++sum][0]=1,ans[sum][1]=1;
					}
				}i=l+2;
			}
		}
		cout<<sum<<endl;
		for(int i=1;i<=sum;i++){
			printf("%d ",ans[i][0]);
			for(int j=1;j<=ans[i][0];j++)printf("%d ",ans[i][j]);
			puts("");
		}
		//cout<<check()<<endl;
	
	}
}
namespace solve3{
	int b[maxn],res=0;
	int F=0;
	deque<int>g[5];
	bool check1(){
		int res=0;
		for(int i=1;i<=n;i++){
			if(b[i]==1){
				g[1].push_back(a[i]);
				if(g[1].back()==a[1]){
					g[1].pop_back();
				}
				ans[++res][0]=1;
				ans[res][1]=1;
			}
			if(b[i]==2){
				g[2].push_back(a[i]);
				if(g[2].back()==a[2]){
					g[2].pop_back();
				}
				ans[++res][0]=1;
				ans[res][1]=2;
			}
			if(b[i]==3){
				g[3].push_back(a[i]);
				if(g[3].back()==a[3]){
					g[3].pop_back();
				}
				ans[++res][0]=1;
				ans[res][1]=3;
			}
			for(int o=1;o<=m;o++){
				int G=0;
				for(int i=1;i<=3;i++)
					for(int j=i+1;j<=3;j++){
						if(!g[i].empty()&&!g[j].empty()&&g[i].front()==g[j].front()){
							ans[++res][0]=2;g[i].pop_front(),g[j].pop_front();
							ans[res][1]=i,ans[res][2]=j;
							G=1;
						}
				if(!G)break;
				}
			}
		}int o=0;
		for(int i=1;i<=3;i++){
			if(!g[i].empty()){
				o=1;
			}g[i].clear();
		}
		if(o)return 0;
		sum=res;F=1;
		return 1;
	}
	
	void dfs(int x){
		if(F)return;
		if(x==n+1){
			if(check1())F=1;
			return ;
		}
		b[x]=1;
		dfs(x+1);
		b[x]=2;
		dfs(x+1);
		b[x]=3;
		dfs(x+1);
	}
	void work(){
		for(int i=1;i<=m;i++)a[i]=read();
		dfs(1);
		cout<<sum<<endl;
		for(int i=1;i<=sum;i++){
			printf("%d ",ans[i][0]);
			for(int j=1;j<=ans[i][0];j++)printf("%d ",ans[i][j]);
			puts("");
		}
	}
}
void solve(){
	n=read(),m=read();k=read();
	if(k==2*n-2)solve1::work();
	else if(n==2)solve2::work();
	else if(n==3)solve3::work();
	
}
signed main(){
	freopen("meow.in","r",stdin);
	freopen("meow.out","w",stdout);
	T=read();
	while(T--)solve();
	return 0;
}
