// NOIP2022 RP++

#include <bits/stdc++.h>
typedef unsigned uint;
typedef unsigned long long ullt;typedef long long llt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _min(T&a,T b){return b<a?(a=b,true):false;}
template<typename T>bol _max(T&a,T b){return a<b?(a=b,true):false;}
uint op;
uint n,k,m;
uint A[200005];
std::mt19937 rng(114);
using vec=std::vector<uint>;
using sol=std::vector<vec>;
vec Now[200005];
sol tryit(){
    for(uint i=0;i<=n;i++)Now[i].clear();
    sol Ans,S(3);
    for(uint i=1;i<=n;i++)S[0].push_back(i);
    auto erase=[&](uint p){
        uint s=Now[p].size();
        S[s].erase(std::lower_bound(S[s].begin(),S[s].end(),p));
    };
    auto insert=[&](uint p){
        uint s=Now[p].size();
        S[s].insert(std::lower_bound(S[s].begin(),S[s].end(),p),p);
    };
    vec Last(k+1,-1u),At(k+1,-1u);
    for(uint i=0;i<m;i++){
        uint v=A[i];
        if(~At[v]){
            if(v==Now[At[v]].back()){
                Ans.push_back({At[v]});
                erase(At[v]);
                Now[At[v]].pop_back();
                insert(At[v]);
            }
            else if(S[0].size()){
                Ans.push_back({S[0][0]});
                Ans.push_back({At[v],S[0][0]});
                erase(At[v]);
                Now[At[v]].erase(Now[At[v]].begin());
                insert(At[v]);
            }
            else{
                uint p=S[1][0];
                uint w=Now[p][0];
                uint u=Now[At[v]].back();
                erase(p);
                erase(At[v]);
                if(Last[w]<Last[u]){
                    Ans[Last[u]]={p},Ans.push_back({At[v]});
                    Now[At[v]]={},Now[p]={w,u};
                    At[u]=p;
                }
                else{
                    Ans[Last[w]]={At[v]};
                    Ans.push_back({p}),Ans.push_back({p,At[v]});
                    Now[At[v]]={u,w},Now[p]={};
                    At[w]=At[v];
                }
                insert(p);
                insert(At[v]);
            }
            At[v]=-1;
            Last[v]=-1;
        }
        else{
            Last[v]=Ans.size();
            uint p=S[S[0].size()==1&&S[1].size()][0];
            At[v]=p;
            erase(p);
            Now[p].push_back(v);
            insert(p);
            Ans.push_back({p});
        }
    }
    return Ans;
}
bol check(sol S){
    for(uint i=0;i<=n;i++)Now[i].clear();
    uint t=0;
    for(auto v:S)
    {
        if(v.size()==1)
        {
            if(t>=m)return false;
            if(Now[v[0]].size()&&Now[v[0]].back()==A[t]){
                Now[v[0]].pop_back();
                continue;
            }
            Now[v[0]].push_back(A[t++]);
        }
        else if(Now[v[0]].size()&&Now[v[1]].size()
            &&Now[v[0]][0]==Now[v[1]][0]){
            Now[v[0]].erase(Now[v[0]].begin());
            Now[v[1]].erase(Now[v[1]].begin());
        }
        // for(uint i=1;i<=n;i++)if(Now[i].size()){
        //     printf("%u:",i);
        //     for(auto s:Now[i])printf("%u ",s);
        //     puts("");
        // }
        // puts("------");
    }
    return 1;
}
voi solve(){
    scanf("%u%u%u",&n,&m,&k);
    for(uint i=0;i<m;i++)
        scanf("%u",A+i);
    sol Ans=tryit();
    // if(1)puts(check(Ans)?"ok":"no");
    // while((Ans=tryit()).empty());
    printf("%u\n",(uint)Ans.size());
    for(uint i=0;i<Ans.size();i++)
        if(Ans[i].size()==1)
            printf("1 %u\n",Ans[i][0]);
        else
            printf("2 %u %u\n",Ans[i][0],Ans[i][1]);
}
int main(){
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
    freopen("QAQ.out","w",stdout);
#else
    freopen("meow.in","r",stdin);
    freopen("meow.out","w",stdout);
#endif
    uint t;scanf("%u",&t),op=t%5;
    while(t--)solve();
    return 0;
}

/*
g++ meow.cpp -o meow -DMYEE -Wall -std=c++11
biu#2019miss
solo@2022
*/

#if 0
#include <bits/stdc++.h>
typedef unsigned uint;
typedef unsigned long long ullt;typedef long long llt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _min(T&a,T b){return b<a?(a=b,true):false;}
template<typename T>bol _max(T&a,T b){return a<b?(a=b,true):false;}
uint op;
uint n,k,m;
uint A[200005];
uint Rng[200005];
std::mt19937 rng(114);
// voi flush(){
//     for(uint i=0;i<=k;i++)
//         Rng[i]=i;
//     std::shuffle(Rng+1,Rng+k+1,rng);
//     for(uint i=0;i<m;i++)
//         A[i]=Rng[A[i]];
// }
using vec=std::vector<uint>;
using sol=std::vector<vec>;
vec Now[200005];
uint At[200005];
sol tryit(){
    if(n==1)return sol(m,vec{1});
    else if(n<=2){
        ;
    }
    for(uint i=1;i<n;i++)Now[i].clear();
    for(uint i=1;i<=k;i++)At[i]=-1;
    sol Ans,S(4);
    for(uint i=1;i<n;i++)S[0].push_back(i);
    auto erase=[&](uint p){
        uint s=Now[p].size();
        S[s].erase(std::lower_bound(S[s].begin(),S[s].end(),p));
    };
    auto insert=[&](uint p){
        uint s=Now[p].size();
        S[s].insert(std::lower_bound(S[s].begin(),S[s].end(),p),p);
    };
    for(uint i=0;i<m;i++){
        uint v=A[i];
        // printf("%u\n",v);
        fflush(stdout);
        if(~At[v]){
            if(v==Now[At[v]][0]){
                Ans.push_back({n}),Ans.push_back({At[v],n});
                erase(At[v]);
                Now[At[v]].erase(Now[At[v]].begin());
                insert(At[v]);
                At[v]=-1;
            }
            else if(v==Now[At[v]].back()){
                Ans.push_back({At[v]});
                erase(At[v]);
                Now[At[v]].pop_back();
                insert(At[v]);
                At[v]=-1;
            }
            else return sol();
        }
        else{
            uint t=0;while(S[t].empty())t++;
            uint p=S[t][rng()%S[t].size()];
            Ans.push_back({p}),At[v]=p;
            erase(p);
            Now[p].push_back(v);
            insert(p);
        }
    }
    return Ans;
}
voi solve(){
    scanf("%u%u%u",&n,&m,&k);
    for(uint i=0;i<m;i++)
        scanf("%u",A+i);
    sol Ans;
    while((Ans=tryit()).empty());
    printf("%u\n",(uint)Ans.size());
    for(uint i=0;i<Ans.size();i++)
        if(Ans[i].size()==1)
            printf("1 %u\n",Ans[i][0]);
        else
            printf("2 %u %u\n",Ans[i][0],Ans[i][1]);
}
int main(){
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
    freopen("QAQ.out","w",stdout);
#else
    freopen("meow.in","r",stdin);
    freopen("meow.out","w",stdout);
#endif
    uint t;scanf("%u",&t),op=t%5;
    while(t--)solve();
    return 0;
}
#endif