#include<bits/stdc++.h>
#define ll long long
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
using namespace std;
template<typename T_>void operator+=(vector<T_>&x,const T_&y){x.push_back(y);}
template<typename T_>void operator--(vector<T_>&x,int){x.pop_back();}
const int N=1007,M=2e6+7;
int T,n,m,k;
int a[M],s[N],in[N],ins[N];
void work1(){
	vector<pair<int,int>>ans;
	vector<int>emp(n*2-2);
	memset(in,0,sizeof(in));
	iota(all(emp),1),reverse(all(emp));
	for(int i=1,x,y;i<=m;i++){
		cin>>x;
		if(in[x]){
			emp+=in[x],y=in[x],in[x]=0;
			if(y<=n-1)ans+={n,0},ans+={y,n};
			else ans+={y-n+1,0};
		}
		else y=emp.back(),in[x]=y,emp--,ans+={y>n-1?(y-n+1):y,0};
	}
	cout<<ans.size()<<'\n';
	for(auto i:ans){
		if(i.se)cout<<"2 "<<i.fi<<' '<<i.se<<'\n';
		else cout<<"1 "<<i.fi<<'\n';
	}
}
void work2(){
	for(int i=1;i<=m;i++)cin>>a[i];
	vector<pair<int,int>>ans;
	vector<int>emp,st;
	int lst=0,E=n;
	for(int i=1;i<=n-1;i++)emp+=i;
	for(int i=1;i<=n-1;i++)emp+=n+i;
	for(int i=1,x,y;i<=m;i++){
		x=a[i];
		if(!emp.empty()){
			if(in[x])emp+=in[x],y=in[x],in[x]=0,ans+={y,0};
			else in[x]=y=emp.back(),emp--,ans+={(y>n-1)?(y-n+1):y,0};
		}else{
			if(in[x]){
				y=in[x];
				if(!lst){
					if(y<=n-1)ans+={E,0},ans+={y,E};
					else ans+={y-n+1,0};
					emp+=y;
				}
				else{
					int j=i;
					for(;j<=m;j++)
						if(in[a[j]]<=n-1)break;
						else st+=a[j],ins[a[j]]++;
					vector<int>e;
					int tt=1;
					for(int K=1;K<=n;K++){
						if(tt==a[j])tt++;
						e+=tt++;
					}
					if(ins[a[j]+n-1]){
						ans+={n,0};
						for(int K:st){
							if(in[K])e+=in[K],y=in[K],in[K]=0,ans+={y,0};
							else in[K]=y=e.back(),e--,ans+={y,0};
						}
						ans+={in[a[j]],0};
						emp+=E,emp+=E+n-1,in[lst]=0,E=in[a[j]],in[a[j]]=0;
					}
					else{
						ans+={in[a[j]],0};
						for(int K:st){
							if(in[K])e+=in[K],y=in[K],in[K]=0,ans+={y,0};
							else in[K]=y=e.back(),e--,ans+={y,0};
						}
						ans+={in[a[j]],E},emp+=in[lst],emp+=in[a[j]],
						in[lst]=0,in[a[j]]=0;
					}
					i=j;
					for(int K:st)ins[K]=0;
					st.clear();
				}
			}
			else if(lst==x)ans+={E,0},ans+={E,0};
			else lst=x;
		}
	}
	cout<<ans.size()<<'\n';
	for(auto i:ans){
		if(i.se)cout<<"2 "<<i.fi<<' '<<i.se<<'\n';
		else cout<<"1 "<<i.fi<<'\n';
	}
}
void work(){
	cin>>n>>m>>k;
	if(k==2*n-2)work1();
	else work2();
}
int main(){
	freopen("meow.in","r",stdin);
	freopen("meow.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--)work();
	return 0;
}
