#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int getInd(int v, const vector<int>& cur) {
for (int i = 0; i < cur.size(); ++i) {
if (cur[i] == v) return i;
}
return -1;
}
void makeSwap(int a, int b, vector<int>& cur, vector<pair<int, int>>& res) {
res.emplace_back(a+1, b+1);
swap(cur[a], cur[b]);
}
void solve() {
int n;
cin >> n;
vector<int> as(n);
for (int& a : as) {
cin >> a;
--a;
}
if (n == 1) cout << 0 << '\n';
else if (n == 2) {
if (as[0] != 0) cout << -1 << '\n';
else cout << 0 << '\n';
} else if (n == 3) {
if (as[1] != 1) cout << -1 << '\n';
else if (as[0] == 0) cout << 0 << '\n';
else cout << 1 << '\n' << 1 << ' ' << 3 << '\n';
} else {
vector<pair<int, int>> res;
for (int t = n-1; t > 3; --t) {
int ind = getInd(t, as);
if (ind == t) continue;
if (ind == t-1) {
makeSwap(0, ind, as, res);
ind = 0;
}
makeSwap(ind, t, as, res);
}
int ind = getInd(1, as);
if (ind == 2) {
makeSwap(0, 2, as, res);
ind = 0;
}
if (ind == 0) {
makeSwap(0, 3, as, res);
ind = 3;
}
if (ind == 3) {
makeSwap(1, 3, as, res);
}
ind = getInd(2, as);
if (ind == 3) {
makeSwap(0, 3, as, res);
ind = 0;
}
if (ind == 0) {
makeSwap(0, 2, as, res);
}
if (as[0] != 0) makeSwap(0, 3, as, res);
cout << res.size() << '\n';
for (auto pr : res) cout << pr.first << ' ' << pr.second << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
for (int ti = 0; ti < t; ++ti) solve();
}