#include <iostream>
#include <utility>
#include <vector>
using namespace std;
bool dfs(vector<int> v, vector<pair<int, int>> m) {
if (m.size() > v.size()+2) return false;
bool ok = true;
for (int i = 0; i < v.size(); ++i) {
if (v[i] == i+1) continue;
ok = false;
for (int j = 0; j < v.size(); ++j) {
if (abs(j-i) <= 1) continue;
auto v2 = v;
auto m2 = m;
swap(v2[i], v2[j]);
m2.push_back(make_pair(i+1, j+1));
if (dfs(v2, m2)) return true;
}
}
if (ok) {
cout << m.size() << endl;
for (auto &p : m) {
cout << p.first << " " << p.second << endl;
}
return true;
}
return false;
}
void solve(vector<int> &v) {
vector<pair<int, int>> m;
for (int i = 0; i < v.size(); ++i) {
if (v[i] == i + 1) continue;
if (i == v.size() - 1) {
cout << -1 << endl;
return;
}
if (v[i+1] == i + 1) {
int k;
if (i - 2 >= 0) k = i - 2;
else if (i + 3 < v.size()) k = i + 3;
else {
cout << -1 << endl;
return;
}
swap(v[i], v[k]);
m.push_back(make_pair(i+1, k+1));
swap(v[i+1], v[k]);
m.push_back(make_pair(i+2, k+1));
swap(v[i], v[k]);
m.push_back(make_pair(i+1, k+1));
} else {
for (int j = i + 2; j < v.size(); ++j) {
if (v[j] == i + 1) {
swap(v[i], v[j]);
m.push_back(make_pair(i+1, j+1));
}
}
}
if (m.size() > 5 * v.size()) {
cout << -1 << endl;
return;
}
}
cout << m.size() << endl;
for (auto &p : m) {
cout << p.first << " " << p.second << endl;
}
}
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
int n;
cin >> n;
vector<int> v(n);
for (int a = 0; a < n; ++a) {
cin >> v[a];
}
if (n > 4) solve(v);
else {
if (!dfs(v, vector<pair<int, int>>())) {
cout << -1 << endl;
}
}
}
}