#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
// Count of each number and their positions
int count2 = 0, count1 = 0, count0 = 0;
vector<int> pos0, pos1, pos2;
for(int i = 0; i < n; i++) {
cin >> a[i];
if(a[i] == 0) {
count0++;
pos0.push_back(i);
}
else if(a[i] == 1) {
count1++;
pos1.push_back(i);
}
else {
count2++;
pos2.push_back(i);
}
}
vector<pair<int,int>> moves;
int target = n-1;
// Only process 2s if we have any
if(count2 > 0) {
while(target >= n - count2) {
if(a[target] != 2) {
if(a[target] == 1) {
// Direct swap from 1 to 2
moves.push_back({target + 1, pos2.back() + 1});
a[target] = 2;
a[pos2.back()] = 1;
pos1.push_back(pos2.back());
pos2.pop_back();
} else { // a[target] == 0
// Two moves: 0->1->2
moves.push_back({target + 1, pos1.back() + 1});
moves.push_back({pos1.back() + 1, pos2.back() + 1});
a[target] = 2;
a[pos1.back()] = 0;
a[pos2.back()] = 1;
pos0.push_back(pos1.back());
pos1.pop_back();
pos1.push_back(pos2.back());
pos2.pop_back();
}
}
target--;
}
}
// After handling all 2s, process remaining 1s
while(target >= n - count2 - count1) {
if(a[target] == 0) {
moves.push_back({target + 1, pos1.back() + 1});
a[target] = 1;
a[pos1.back()] = 0;
pos0.push_back(pos1.back());
pos1.pop_back();
}
target--;
}
cout << moves.size() << "\n";
for(const auto& move : moves) {
cout << move.second << " " << move.first << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) {
solve();
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBzb2x2ZSgpIHsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICB2ZWN0b3I8aW50PiBhKG4pOwogICAgLy8gQ291bnQgb2YgZWFjaCBudW1iZXIgYW5kIHRoZWlyIHBvc2l0aW9ucwogICAgaW50IGNvdW50MiA9IDAsIGNvdW50MSA9IDAsIGNvdW50MCA9IDA7CiAgICB2ZWN0b3I8aW50PiBwb3MwLCBwb3MxLCBwb3MyOwogICAgCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICAgICAgaWYoYVtpXSA9PSAwKSB7CiAgICAgICAgICAgIGNvdW50MCsrOwogICAgICAgICAgICBwb3MwLnB1c2hfYmFjayhpKTsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihhW2ldID09IDEpIHsKICAgICAgICAgICAgY291bnQxKys7CiAgICAgICAgICAgIHBvczEucHVzaF9iYWNrKGkpOwogICAgICAgIH0KICAgICAgICBlbHNlIHsKICAgICAgICAgICAgY291bnQyKys7CiAgICAgICAgICAgIHBvczIucHVzaF9iYWNrKGkpOwogICAgICAgIH0KICAgIH0KICAgIAogICAgdmVjdG9yPHBhaXI8aW50LGludD4+IG1vdmVzOwogICAgaW50IHRhcmdldCA9IG4tMTsKICAgIAogICAgLy8gT25seSBwcm9jZXNzIDJzIGlmIHdlIGhhdmUgYW55CiAgICBpZihjb3VudDIgPiAwKSB7CiAgICAgICAgd2hpbGUodGFyZ2V0ID49IG4gLSBjb3VudDIpIHsKICAgICAgICAgICAgaWYoYVt0YXJnZXRdICE9IDIpIHsKICAgICAgICAgICAgICAgIGlmKGFbdGFyZ2V0XSA9PSAxKSB7CiAgICAgICAgICAgICAgICAgICAgLy8gRGlyZWN0IHN3YXAgZnJvbSAxIHRvIDIKICAgICAgICAgICAgICAgICAgICBtb3Zlcy5wdXNoX2JhY2soe3RhcmdldCArIDEsIHBvczIuYmFjaygpICsgMX0pOwogICAgICAgICAgICAgICAgICAgIGFbdGFyZ2V0XSA9IDI7CiAgICAgICAgICAgICAgICAgICAgYVtwb3MyLmJhY2soKV0gPSAxOwogICAgICAgICAgICAgICAgICAgIHBvczEucHVzaF9iYWNrKHBvczIuYmFjaygpKTsKICAgICAgICAgICAgICAgICAgICBwb3MyLnBvcF9iYWNrKCk7CiAgICAgICAgICAgICAgICB9IGVsc2UgeyAvLyBhW3RhcmdldF0gPT0gMAogICAgICAgICAgICAgICAgICAgIC8vIFR3byBtb3ZlczogMC0+MS0+MgogICAgICAgICAgICAgICAgICAgIG1vdmVzLnB1c2hfYmFjayh7dGFyZ2V0ICsgMSwgcG9zMS5iYWNrKCkgKyAxfSk7CiAgICAgICAgICAgICAgICAgICAgbW92ZXMucHVzaF9iYWNrKHtwb3MxLmJhY2soKSArIDEsIHBvczIuYmFjaygpICsgMX0pOwogICAgICAgICAgICAgICAgICAgIGFbdGFyZ2V0XSA9IDI7CiAgICAgICAgICAgICAgICAgICAgYVtwb3MxLmJhY2soKV0gPSAwOwogICAgICAgICAgICAgICAgICAgIGFbcG9zMi5iYWNrKCldID0gMTsKICAgICAgICAgICAgICAgICAgICBwb3MwLnB1c2hfYmFjayhwb3MxLmJhY2soKSk7CiAgICAgICAgICAgICAgICAgICAgcG9zMS5wb3BfYmFjaygpOwogICAgICAgICAgICAgICAgICAgIHBvczEucHVzaF9iYWNrKHBvczIuYmFjaygpKTsKICAgICAgICAgICAgICAgICAgICBwb3MyLnBvcF9iYWNrKCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdGFyZ2V0LS07CiAgICAgICAgfQogICAgfQogICAgCiAgICAvLyBBZnRlciBoYW5kbGluZyBhbGwgMnMsIHByb2Nlc3MgcmVtYWluaW5nIDFzCiAgICB3aGlsZSh0YXJnZXQgPj0gbiAtIGNvdW50MiAtIGNvdW50MSkgewogICAgICAgIGlmKGFbdGFyZ2V0XSA9PSAwKSB7CiAgICAgICAgICAgIG1vdmVzLnB1c2hfYmFjayh7dGFyZ2V0ICsgMSwgcG9zMS5iYWNrKCkgKyAxfSk7CiAgICAgICAgICAgIGFbdGFyZ2V0XSA9IDE7CiAgICAgICAgICAgIGFbcG9zMS5iYWNrKCldID0gMDsKICAgICAgICAgICAgcG9zMC5wdXNoX2JhY2socG9zMS5iYWNrKCkpOwogICAgICAgICAgICBwb3MxLnBvcF9iYWNrKCk7CiAgICAgICAgfQogICAgICAgIHRhcmdldC0tOwogICAgfQogICAgCiAgICBjb3V0IDw8IG1vdmVzLnNpemUoKSA8PCAiXG4iOwogICAgZm9yKGNvbnN0IGF1dG8mIG1vdmUgOiBtb3ZlcykgewogICAgICAgIGNvdXQgPDwgbW92ZS5zZWNvbmQgPDwgIiAiIDw8IG1vdmUuZmlyc3QgPDwgIlxuIjsKICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwogICAgCiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgc29sdmUoKTsKICAgIH0KICAgIHJldHVybiAwOwp9