#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, time = 0;
cin >> n;
int a[n], b[n], c[3] = {0};
vector<pair<int, int>> swaps;
for(int i = 0; i < 3; i++) {
c[i] = 0;
}
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] < 3) {
c[a[i]]++;
}
}
for(int i = 0; i < c[0]; i++) {
b[i] = 0;
}
for(int i = c[0]; i < c[0] + c[1]; i++) {
b[i] = 1;
}
for(int i = c[0] + c[1]; i < n; i++) {
b[i] = 2;
}
for(int i = n - 1; i >= 0; i--) {
if(a[i] != b[i]) {
if(abs(a[i] - b[i]) == 1) {
for(int j = n - 1; j >= 0; j--) {
if(a[j] == b[i]&&j<i) {
swap(a[i], a[j]);
swaps.push_back({i+1, j+1});
time++;
break;
}
else if(a[j] == b[i]&&b[j]!=b[i]){
swap(a[i], a[j]);
swaps.push_back({i+1, j+1});
time++;
break;
}
}
} else {
int m;
for(int j = n - 1; j >= 0; j--) {
if(a[j] == 1&&j<i) {
swap(a[i], a[j]);
swaps.push_back({i+1, j+1});
time++;
break;
}
else if(a[j] == 1&&b[j]!=b[i]){
swap(a[i], a[j]);
swaps.push_back({i+1, j+1});
time++;
break;
}
}
for(int j = m; j >= 0; j--) {
if(a[j] == b[i]&&j<i) {
swap(a[i], a[j]);
swaps.push_back({i+1, j+1});
time++;
break;
}
else if(a[j] == b[i]&&b[j]!=b[i]){
swap(a[i], a[j]);
swaps.push_back({i+1, j+1});
time++;
break;
}
}
}
}
}
cout << time << endl;
for (auto &p : swaps) {
cout << p.first << " " << p.second << endl;
}
for(int i = 0; i< n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKICAgIGludCB0OwogICAgY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgaW50IG4sIHRpbWUgPSAwOwogICAgICAgIGNpbiA+PiBuOwogICAgICAgIGludCBhW25dLCBiW25dLCBjWzNdID0gezB9OwogICAgICAgIHZlY3RvcjxwYWlyPGludCwgaW50Pj4gc3dhcHM7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IDM7IGkrKykgewogICAgICAgICAgICBjW2ldID0gMDsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgY2luID4+IGFbaV07CiAgICAgICAgICAgIGlmIChhW2ldIDwgMykgewogICAgICAgICAgICAgICAgY1thW2ldXSsrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBjWzBdOyBpKyspIHsKICAgICAgICAgICAgYltpXSA9IDA7CiAgICAgICAgfQogICAgICAgIGZvcihpbnQgaSA9IGNbMF07IGkgPCBjWzBdICsgY1sxXTsgaSsrKSB7CiAgICAgICAgICAgIGJbaV0gPSAxOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGkgPSBjWzBdICsgY1sxXTsgaSA8IG47IGkrKykgewogICAgICAgICAgICBiW2ldID0gMjsKICAgICAgICB9CiAgICAgICAgZm9yKGludCBpID0gbiAtIDE7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgIGlmKGFbaV0gIT0gYltpXSkgewogICAgICAgICAgICAgICAgaWYoYWJzKGFbaV0gLSBiW2ldKSA9PSAxKSB7CiAgICAgICAgICAgICAgICAgICAgZm9yKGludCBqID0gbiAtIDE7IGogPj0gMDsgai0tKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKGFbal0gPT0gYltpXSYmajxpKSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzd2FwKGFbaV0sIGFbal0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3dhcHMucHVzaF9iYWNrKHtpKzEsIGorMX0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGltZSsrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZihhW2pdID09IGJbaV0mJmJbal0hPWJbaV0pewogICAgICAgICAgICAgICAgICAgICAgICAJc3dhcChhW2ldLCBhW2pdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN3YXBzLnB1c2hfYmFjayh7aSsxLCBqKzF9KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRpbWUrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICBpbnQgbTsKICAgICAgICAgICAgICAgICAgICBmb3IoaW50IGogPSBuIC0gMTsgaiA+PSAwOyBqLS0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoYVtqXSA9PSAxJiZqPGkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN3YXAoYVtpXSwgYVtqXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzd2Fwcy5wdXNoX2JhY2soe2krMSwgaisxfSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0aW1lKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGFbal0gPT0gMSYmYltqXSE9YltpXSl7CiAgICAgICAgICAgICAgICAgICAgICAgIAlzd2FwKGFbaV0sIGFbal0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3dhcHMucHVzaF9iYWNrKHtpKzEsIGorMX0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGltZSsrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZm9yKGludCBqID0gbTsgaiA+PSAwOyBqLS0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoYVtqXSA9PSBiW2ldJiZqPGkpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN3YXAoYVtpXSwgYVtqXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzd2Fwcy5wdXNoX2JhY2soe2krMSwgaisxfSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB0aW1lKys7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGlmKGFbal0gPT0gYltpXSYmYltqXSE9YltpXSl7CiAgICAgICAgICAgICAgICAgICAgICAgIAlzd2FwKGFbaV0sIGFbal0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgc3dhcHMucHVzaF9iYWNrKHtpKzEsIGorMX0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGltZSsrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCB0aW1lIDw8IGVuZGw7CiAgICAgICAgZm9yIChhdXRvICZwIDogc3dhcHMpIHsKICAgICAgICAgICAgY291dCA8PCBwLmZpcnN0IDw8ICIgIiA8PCBwLnNlY29uZCA8PCBlbmRsOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGkgPSAwOyBpPCBuO2krKyl7CiAgICAgICAgCWNvdXQ8PGFbaV08PCIgIjsKICAgICAgICB9CiAgICAgICAgY291dDw8ZW5kbDsKICAgIH0gICAKfQo=