#include <stdio.h> // include before iostream for faster scanf
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <set>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <tuple>
#include <utility>
#include <iomanip>
#include <iterator>
using namespace std;
typedef long long LL;
#define printv(printVec) for (auto printVecIter : (printVec)) cout << printVecIter << " "; cout << endl;
// g++ -Wall -Wshadow -std=c++11 a.cpp && ./a.out
vector<pair<int,int>> ans;
vector<int> curvec;
bool is_sorted(vector<int> & v) {
for (int i = 1; i < v.size(); i++) {
if (v[i] < v[i-1]) return false;
}
return true;
}
int get_idx(int item) {
for (int i = 0; i < curvec.size(); i++) {
if (item == curvec[i]) return i;
}
}
void sw(int i1, int i2) {
ans.push_back({i1+1, i2+1});
swap(curvec[i1], curvec[i2]);
}
int main() {
std::ios::sync_with_stdio(false);cin.tie(0);
int t; cin>>t;
while (t--) {
int n; cin>>n;
vector<int> inp(n);
for (int i =0;i<n;i++) {
cin>>inp[i];
}
curvec = inp;
if (curvec.size() <= 2) {
if (is_sorted(curvec)) {
cout << 0 << "\n";
} else {
cout << -1 << "\n";
}
continue;
}
if (curvec.size() == 3) {
if (!is_sorted(curvec)) {
swap(curvec[0], curvec[2]);
if (is_sorted(curvec)) {
cout << 1 << "\n";
cout << 1 << " " << 3 << "\n";
} else {
cout << -1 << "\n";
}
} else {
cout << 0 << "\n";
}
continue;
}
int two = get_idx(2);
if (two == 0) {
sw(0, 3);
sw(1, 3);
} else if (two == 2) {
sw(0, 2);
sw(0, 3);
sw(1, 3);
} else if (two > 2) {
sw(1, two);
}
int one = get_idx(1);
if (one >= 2) {
sw(0, one);
}
for (int i = 0; i < n; i++) {
int idx = i+1;
if (curvec[i] == i + 1) continue; // already in place
while (curvec[idx] != i+1) idx++;
sw(0, idx);
sw(i, 0);
}
// cout << "AA " << endl;
printv(curvec);
cout << ans.size() << "\n";
for (auto p : ans) {
cout << p.first << " " << p.second << "\n";
}
}
}