#include <iostream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;
// assume n >= 4
// if i == 1
// a b c
// a c b
// b c a
// b a c
// if i == 2
// d a b c, a c
// d c b a, a d
// a c b d, a b
// b c a d, b d
// d c a b, b c
// d b a c
// if i == 3
// c a b, c b
// b a c, a b
// a b c, a c
// c b a
// 1 2 3 4 5 6 7 8
// 2 4 3 1
// 2 1 3 4
// 4 1 3 2
// 4 2 3 1
// 1 2 3 4
bool testAns(vector<pair<int, int> > ans, vector<int> v) {
for(auto x: ans) {
if(abs(x.first-x.second) <= 1) return 0;
swap(v[x.first], v[x.second]);
}
int ok = 1;
for(int i = 0; i < v.size(); ++i) {
if(v[i] != i) ok = 0;
cout<<v[i]<<' ';
}
cout<<'\n';
if(ok) {
cout<<"OK"<<endl;
}
else {
cout<<"FAIL"<<endl;
}
return ok;
}
void printAns(vector<pair<int, int> > v) {
cout<<v.size()<<endl;
for(auto x: v) {
cout<<x.first+1<<' '<<x.second+1<<endl;
}
}
void swapTwo(vector<int>& v, int a, int b, vector<pair<int, int> >& ans) {
if(a > b) swap(a, b);
int c;
swap(v[a], v[b]);
if(a+1 == b) {
int d;
if(b == 1) {
c = 3;
ans.push_back({b, c});
swap(b, c);
ans.push_back({a, b});
swap(a, b);
ans.push_back({a, c});
}
else if(b == 2) {
d = 0;
c = 3;
ans.push_back({a, c});
swap(a, c);
ans.push_back({a, d});
swap(a, d);
ans.push_back({a, b});
swap(a, b);
ans.push_back({b, d});
swap(b, d);
ans.push_back({b, c});
}
else {
c = 0;
ans.push_back({c, b});
swap(c, b);
ans.push_back({a, b});
swap(a, b);
ans.push_back({a, c});
}
}
else {
ans.push_back({a, b});
}
}
void solve(vector<int> v) {
vector<int> v_copy = v;
if(v.size() == 1) {
cout<<0<<endl;
return;
}
if(v.size() == 2) {
if(v[0] == 0) {
cout<<0<<endl;
return;
}
cout<<-1<<endl;
return;
}
vector<pair<int, int> > ans;
if(v.size() == 3) {
if(v[0] > v[2]) {
swap(v[0], v[2]);
ans.push_back({0, 2});
}
if(v[0] == 0 && v[1] == 1 && v[2] == 2) {
printAns(ans);
return;
}
else {
cout<<-1<<endl;
return;
}
}
for(int i = 0; i < v.size();) {
if(v[i] != i) {
swapTwo(v, i, v[i], ans);
}
else {
++i;
}
}
printAns(ans);
//std::cerr << testAns(ans, v_copy) << std::endl;
}
int main() {
int tt;
cin>>tt;
for(int xx = 0; xx < tt; ++xx) {
int n;
cin>>n;
vector<int> v(n);
for(int i = 0; i < n; ++i) {
cin>>v[i];
--v[i];
}
solve(v);
}
}