#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void testi(){
int n;
cin >> n;
int t[n+2]={}, p[n+2]={}, k=0, ans[5*n+2][3];
for (int i=1; i<=n; ++i){
int a;
cin >> a;
t[i]=a;
p[a]=i;
}
if (n==1) {
cout << "0\n";
return;
}
if (n==2){
if (t[1]==1){
cout << "0\n";
return;
}
else{
cout << "-1\n";
return;
}
}
if (n==3){
if (t[2]!=2){
cout << "-1\n";
return;
}
else{
if (t[1]==1){
cout << "0\n";
return;
}
else{
cout << "1\n1 3\n";
return;
}
}
}
for (int i=1; i<n; ++i){
if (t[i]!=i){
if (p[i]!=i+1){
k++;
ans[k][1]=i;
ans[k][2]=p[i];
t[p[i]]=t[i];
p[t[i]]=p[i];
t[i]=i;
}
else{
if ((n-(i+1))>=2){
k++;
ans[k][1]=i+1;
ans[k][2]=n;
k++;
ans[k][1]=i;
ans[k][2]=n;
int a=t[i], b=t[i+1], c=t[n];
t[i+1]=c;
t[n]=a;
p[a]=n;
p[c]=i+1;
}
else if (i>=3){
k++;
ans[k][1]=i+1;
ans[k][2]=1;
k++;
ans[k][1]=i;
ans[k][2]=1;
k++;
ans[k][1]=1;
ans[k][2]=i+1;
int a=t[1], b=t[i], c=t[i+1];
t[i]=c;
t[i+1]=b;
p[b]=i+1;
}
else if (n==4){
if (t[2]==3){
k++;
ans[k][1]=1;
ans[k][2]=3;
k++;
ans[k][1]=2;
ans[k][2]=4;
k++;
ans[k][1]=1;
ans[k][2]=4;
k++;
ans[k][1]=1;
ans[k][2]=3;
k++;
ans[k][1]=2;
ans[k][2]=4;
t[2]=2;
t[3]=3;
p[3]=3;
}
else if (t[2]==4){
k++;
ans[k][1]=1;
ans[k][2]=3;
k++;
ans[k][1]=1;
ans[k][2]=4;
k++;
ans[k][1]=1;
ans[k][2]=3;
k++;
ans[k][1]=2;
ans[k][2]=4;
t[3]=3;
t[4]=4;
p[4]=4;
p[3]=3;
}
}
}
}
}
cout << k << "\n";
for (int i=1; i<=k; ++i){
cout << ans[i][1] << " " << ans[i][2] << "\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//Älä käytä, jos tehtävä on interaktiivinen!
int t;
cin >> t;
while (t--) testi();
}