#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__uint128_t;
const int N=3e5+5,M=N*8+5,K=600+5,mod=998244353,Mod=mod-1;const db eps=1e-6;const int INF=1e9+7;mt19937 rnd(263082);
int n,m,A[N],f[N],st[N],sh,del[N];
vector<pii> ans;
void Do(int x){
int L=x;for(int i=x-1;i;i--) if(!del[i]) L^x&&(ans.eb(i,x),0),L=i;
int R=x;for(int i=x+1;i<=n;i++) if(!del[i]) R^x&&(ans.eb(i,x),0),R=i;
if(L+1<R&&L^x&&R^x&&A[L]^A[R]) ans.eb(L,R);
}
void print(){
printf("%d\n",ans.size());
for(auto i:ans) printf("%d %d\n",i.fi,i.se);
}
void Solve(){
int i,j;scanf("%d%d",&n,&m);ans.clear();
fill(f+1,f+m+1,0);fill(del+1,del+n+1,0);
for(i=1;i<=n;i++) scanf("%d",&A[i]),f[A[i]]++;
for(i=1;i<n;i++) if(A[i]^A[i+1]) ans.eb(i,i+1);
sh=0;for(i=1;i<=n;i++){
while(sh>1&&A[i]^A[st[sh-1]]) {
if(f[A[st[sh]]]==1) {Do(st[sh]);print();return;}
del[st[sh]]=1;f[A[st[sh]]]--;sh--;ans.eb(st[sh],i);
}
st[++sh]=i;
}
for(i=4;i<=sh;i+=2) ans.eb(st[1],st[i]);
print();
}
int main(){
int t=1;
scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}