#include<bits/stdc++.h>
using namespace std;

template<class T>inline void read(T&x){
	char c,last=' ';
	while(!isdigit(c=getchar()))last=c;
	x=c^48;
	while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+(c^48);
	if(last=='-')x=-x;
}

#define P pair<int,int>
const int MAXN=3e2+7,MAXM=2e6+7;
int n,m,k;
int a[MAXM];
deque<int>q[MAXN];
vector<int>id;
int op;
vector<P>ans;

void Case1(){
	for(int i=1;i<n;++i)id.push_back(i);
	vector<int>c(k+7);
	for(int i=1;i<=m;++i){
		int x=a[i],f=0;
		if(c[x]){
			int y=c[x];
			if(x==q[y].front()){
				++op;ans.push_back(P(y,0));
				q[y].pop_front();
				if(q[y].size()==1)id.push_back(y);
			}
			else{
				++op;ans.push_back(P(n,0));
				++op;ans.push_back(P(y,n));
				q[y].pop_back();
				if(q[y].size()==1)id.push_back(y);
			}
			c[x]=0;
		}
		else{
			int y=id.back();
			id.pop_back();
			++op;ans.push_back(P(y,0));
			q[y].push_front(x);
			if(q[y].size()==1)id.push_back(y);
			c[x]=y;
		}
	}
}

void Case2(){
	vector<int>b(m+7),c(m+7);
	for(int i=1;i<m;++i){
		if(a[i]==a[i+1]&&!b[i]){
			b[i]=b[i+1]=1;
		}
	}
	for(int i=m,last=0;i>=1;--i){
		if(b[i])continue;
		c[i]=last;
		last=a[i];
	}
	for(int i=1;i<=m;++i){
		if(b[i]){
			++op;ans.push_back(P(n,0));
			continue;
		}
		int x=a[i],f=0;
		for(int j=1;j<=n;++j){
			if(!q[j].empty()&&q[j].front()==x){
				++op;ans.push_back(P(j,0));
				q[j].pop_front();
				f=1;break;
			}
			if(!q[j].empty()&&q[j].back()==x){
				++op;ans.push_back(P(n,0));
				++op;ans.push_back(P(j,n));
				q[j].pop_back();
				f=1;break;
			}
		}
		if(!f){
			for(int j=1;j<n;++j){
				if(q[j].size()<2){
					++op;ans.push_back(P(j,0));
					q[j].push_front(x);
					f=1;break;
				}
			}
		}
		if(!f){
			for(int j=1;j<n;++j){
				if(q[j].front()!=c[i]){
					++op;ans.push_back(P(j,0));
					q[j].push_front(x);
					f=1;break;
				}
			}
		}
	}
}

void Case3(){
	vector<int>b(m+7),c(m+7);
	vector<int>to(m+7);
	for(int i=1;i<m;++i){
		if(a[i]==a[i+1]&&!b[i]){
			b[i]=b[i+1]=1;
		}
	}
	for(int i=m,last=0;i>=1;--i){
		if(b[i])continue;
		c[i]=last;
		last=a[i];
	}
	for(int i=1;i<=m;++i){
		if(b[i]){
			++op;ans.push_back(P(n,0));
			continue;
		}
		int x=a[i],f=0;
		if(to[x]){
			int y=to[x];
			if(q[y].front()==x){
				++op;ans.push_back(P(y,0));
				q[y].pop_front();
				if(q[y].size())to[q[y].front()]=y;
			}
			else{
				++op;ans.push_back(P(n,0));
				++op;ans.push_back(P(y,n));
				q[y].pop_back();
				if(q[y].size())to[q[y].back()]=y;
			}
			to[x]=0;
			f=1;
		}
		if(!f){
			for(int j=1;j<n;++j){
				if(q[j].size()<2){
					++op;ans.push_back(P(j,0));
					q[j].push_front(x);
					to[x]=j;
					f=1;break;
				}
			}
		}
		if(!f){
			for(int j=1;j<n;++j){
				if(q[j].front()!=c[i]){
					++op;ans.push_back(P(j,0));
					to[q[j].front()]=0;
					q[j].push_front(x);
					to[x]=j;
					f=1;break;
				}
			}
		}
	}
}

int main()
{
	freopen("meow.in","r",stdin);
	freopen("meow.out","w",stdout);
	int T;read(T);
	while(T--){
		read(n),read(m),read(k);
		op=0;vector<P>().swap(ans);
		for(int i=1;i<=m;++i)read(a[i]);
		if(k==2*n-2)Case1();
		else if(n<=3)Case2();
		else Case3();
		cout<<op<<'\n';
		for(int i=0;i<op;++i){
			if(ans[i].second==0)cout<<1<<' '<<ans[i].first<<'\n';
			else cout<<2<<' '<<ans[i].first<<' '<<ans[i].second<<'\n';
		}
	}
	return 0;
}
