#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define M 1000000007
#define N (1<<18)
using namespace std;
int n, t, a[1010], r[1010];
void asdfasdf(vector<pair<int,int>> *ans, int k1, int k2) {
ans->push_back(make_pair(k1, k2));
swap(r[a[k1]], r[a[k2]]);
swap(a[k1], a[k2]);
for(int i=1; i<=n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio();
cin >> t;
for(int g=0; g<t; g++) {
cin >> n;
for(int i=1; i<=n; i++) {
cin >> a[i];
r[a[i]] = i;
}
if(n == 1) {
cout << 0;
cout << "\n";
} else if(n == 2) {
if(a[1] != 1)
cout << -1;
else
cout << 0;
cout << "\n";
} else if(n == 3) {
if(a[2] == 2) {
if(a[1] == 1) {
cout << 0;
} else {
cout << "1\n1 3";
}
} else {
cout << -1;
}
cout << "\n";
} else {
vector<pair<int,int>> ans;
if(a[1] != 1) {
if(r[1] < 3) {
asdfasdf(&ans, r[1], n);
}
asdfasdf(&ans, r[1], 1);
}
if(a[2] != 2) {
if(r[2] == 3 && n == 4) {
asdfasdf(&ans, r[2], 1);
asdfasdf(&ans, r[2], 2);
asdfasdf(&ans, 1, r[1]);
} else if(r[2] < 4) {
asdfasdf(&ans, r[2], n);
}
asdfasdf(&ans, r[2], 2);
}
for(int i=3; i<=n; i++) {
int tmp1 = r[i];
asdfasdf(&ans, r[i], 1);
asdfasdf(&ans, r[i], i);
if(a[1] != 1)
asdfasdf(&ans, tmp1, 1);
}
cout << ans.size() << "\n";
for(auto u:ans)
cout << u.first << " " << u.second << "\n";
}
}
}