#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include <algorithm>
#include <cassert>
#include <ctime>
#include <random>
using namespace std;
const int INF = 1e9;
int a[1000000 + 5];
vector < pair < int, int > > ans;
const int array_size = 1e5 + 5;
int t[array_size * 4], lazy[array_size * 4];
void push(int v, int tl, int tr)
{
t[v] += lazy[v] * (tr - tl + 1);
if(tl != tr)
{
lazy[v << 1] += lazy[v];
lazy[(v << 1) + 1] += lazy[v];
}
lazy[v] = 0;
return;
}
void build(int v, int tl, int tr)
{
if(tl == tr)
{
t[v] = a[tl];
return;
}
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm);
build((v << 1) + 1, tm + 1, tr);
t[v] = t[v << 1] + t[(v << 1) + 1];
return;
}
void update(int v, int tl, int tr, int l, int r, int add)
{
if(l > r)
return;
if(l == tl && r == tr)
{
lazy[v] += add;
push(v, tl, tr);
return;
}
push(v, tl, tr);
int tm = (tl + tr) >> 1;
update(v << 1, tl, tm, l, min(r, tm), add);
update((v << 1) + 1, tm + 1, tr, max(l, tm + 1), r, add);
t[v] = t[v << 1] + t[(v << 1) + 1];
return;
}
int find_sum(int v, int tl, int tr, int l, int r)
{
if(l > r)
return 0;
push(v, tl, tr);
if(l == tl && r == tr)
return t[v];
int tm = (tl + tr) >> 1;
return find_sum(v << 1, tl, tm, l, min(r, tm)) + find_sum((v << 1) + 1, tm + 1, tr, max(l, tm + 1), r);
}
int intersection(int k1, int b1, int k2, int b2)
{
int dk = k1 - k2;
int db = b2 - b1;
return db / dk + (db / dk > 0 && abs(db) % abs(dk) > 0);
}
struct ConvexHullTrick
{
int sz = 0;
vector < int > k, b, fr;
void push(int newk, int newb)
{
while(sz)
{
if(k.back() == newk && b.back() > newb || k.back() != newk && fr.back() >= intersection(k.back(), b.back(), newk, newb))
{
sz--;
k.pop_back();
b.pop_back();
fr.pop_back();
continue;
}
break;
}
if(sz && k.back() == newk)
return;
fr.push_back((sz ? intersection(k.back(), b.back(), newk, newb) : -INF));
k.push_back(newk);
b.push_back(newb);
sz++;
}
int convex(int x)
{
if(!sz)
return INF;
int j = upper_bound(fr.begin(), fr.end(), x) - fr.begin() - 1;
return k[j] * x + b[j];
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
ans.clear();
for (int i = 0; i < n; i++)
{
int pos = (n - 1 + i) % n;
int j = (pos - 4 + n) % n;
int cnt = 0;
while (a[pos] > 0)
{
cnt++;
if (cnt == n * 2) break;
if (a[j] > 0 && pos != j)
{
ans.push_back({pos + 1, j + 1});
a[pos]--;
a[j]--;
}
j--;
j += n;
j %= n;
}
}
cout << ans.size() << '\n';
for (auto i : ans)
cout << i.first << " " << i.second << '\n';
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxjdGltZT4KI2luY2x1ZGUgPHJhbmRvbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgSU5GID0gMWU5OwoKaW50IGFbMTAwMDAwMCArIDVdOwoKdmVjdG9yIDwgcGFpciA8IGludCwgaW50ID4gPiBhbnM7CgoKY29uc3QgaW50IGFycmF5X3NpemUgPSAxZTUgKyA1OwppbnQgdFthcnJheV9zaXplICogNF0sIGxhenlbYXJyYXlfc2l6ZSAqIDRdOwoKdm9pZCBwdXNoKGludCB2LCBpbnQgdGwsIGludCB0cikKewogICAgdFt2XSArPSBsYXp5W3ZdICogKHRyIC0gdGwgKyAxKTsKICAgIGlmKHRsICE9IHRyKQogICAgewogICAgICAgIGxhenlbdiA8PCAxXSArPSBsYXp5W3ZdOwogICAgICAgIGxhenlbKHYgPDwgMSkgKyAxXSArPSBsYXp5W3ZdOwogICAgfQogICAgbGF6eVt2XSA9IDA7CiAgICByZXR1cm47Cn0KCnZvaWQgYnVpbGQoaW50IHYsIGludCB0bCwgaW50IHRyKQp7CiAgICBpZih0bCA9PSB0cikKICAgIHsKICAgICAgICB0W3ZdID0gYVt0bF07CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaW50IHRtID0gKHRsICsgdHIpID4+IDE7CiAgICBidWlsZCh2IDw8IDEsIHRsLCB0bSk7CiAgICBidWlsZCgodiA8PCAxKSArIDEsIHRtICsgMSwgdHIpOwogICAgdFt2XSA9IHRbdiA8PCAxXSArIHRbKHYgPDwgMSkgKyAxXTsKICAgIHJldHVybjsKfQoKdm9pZCB1cGRhdGUoaW50IHYsIGludCB0bCwgaW50IHRyLCBpbnQgbCwgaW50IHIsIGludCBhZGQpCnsKICAgIGlmKGwgPiByKQogICAgICAgIHJldHVybjsKICAgIGlmKGwgPT0gdGwgJiYgciA9PSB0cikKICAgIHsKICAgICAgICBsYXp5W3ZdICs9IGFkZDsKICAgICAgICBwdXNoKHYsIHRsLCB0cik7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgcHVzaCh2LCB0bCwgdHIpOwogICAgaW50IHRtID0gKHRsICsgdHIpID4+IDE7CiAgICB1cGRhdGUodiA8PCAxLCB0bCwgdG0sIGwsIG1pbihyLCB0bSksIGFkZCk7CiAgICB1cGRhdGUoKHYgPDwgMSkgKyAxLCB0bSArIDEsIHRyLCBtYXgobCwgdG0gKyAxKSwgciwgYWRkKTsKICAgIHRbdl0gPSB0W3YgPDwgMV0gKyB0Wyh2IDw8IDEpICsgMV07CiAgICByZXR1cm47Cn0KCmludCBmaW5kX3N1bShpbnQgdiwgaW50IHRsLCBpbnQgdHIsIGludCBsLCBpbnQgcikKewogICAgaWYobCA+IHIpCiAgICAgICAgcmV0dXJuIDA7CiAgICBwdXNoKHYsIHRsLCB0cik7CiAgICBpZihsID09IHRsICYmIHIgPT0gdHIpCiAgICAgICAgcmV0dXJuIHRbdl07CiAgICBpbnQgdG0gPSAodGwgKyB0cikgPj4gMTsKICAgIHJldHVybiBmaW5kX3N1bSh2IDw8IDEsIHRsLCB0bSwgbCwgbWluKHIsIHRtKSkgKyBmaW5kX3N1bSgodiA8PCAxKSArIDEsIHRtICsgMSwgdHIsIG1heChsLCB0bSArIDEpLCByKTsKfQoKaW50IGludGVyc2VjdGlvbihpbnQgazEsIGludCBiMSwgaW50IGsyLCBpbnQgYjIpCnsKICAgIGludCBkayA9IGsxIC0gazI7CiAgICBpbnQgZGIgPSBiMiAtIGIxOwogICAgcmV0dXJuIGRiIC8gZGsgKyAoZGIgLyBkayA+IDAgJiYgYWJzKGRiKSAlIGFicyhkaykgPiAwKTsKfQoKc3RydWN0IENvbnZleEh1bGxUcmljawp7CiAgICBpbnQgc3ogPSAwOwogICAgdmVjdG9yIDwgaW50ID4gaywgYiwgZnI7CiAgICB2b2lkIHB1c2goaW50IG5ld2ssIGludCBuZXdiKQogICAgewogICAgICAgIHdoaWxlKHN6KQogICAgICAgIHsKICAgICAgICAgICAgaWYoay5iYWNrKCkgPT0gbmV3ayAmJiBiLmJhY2soKSA+IG5ld2IgfHwgay5iYWNrKCkgIT0gbmV3ayAmJiBmci5iYWNrKCkgPj0gaW50ZXJzZWN0aW9uKGsuYmFjaygpLCBiLmJhY2soKSwgbmV3aywgbmV3YikpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHN6LS07CiAgICAgICAgICAgICAgICBrLnBvcF9iYWNrKCk7CiAgICAgICAgICAgICAgICBiLnBvcF9iYWNrKCk7CiAgICAgICAgICAgICAgICBmci5wb3BfYmFjaygpOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgfQogICAgICAgIGlmKHN6ICYmIGsuYmFjaygpID09IG5ld2spCiAgICAgICAgICAgIHJldHVybjsKICAgICAgICBmci5wdXNoX2JhY2soKHN6ID8gaW50ZXJzZWN0aW9uKGsuYmFjaygpLCBiLmJhY2soKSwgbmV3aywgbmV3YikgOiAtSU5GKSk7CiAgICAgICAgay5wdXNoX2JhY2sobmV3ayk7CiAgICAgICAgYi5wdXNoX2JhY2sobmV3Yik7CiAgICAgICAgc3orKzsKICAgIH0KICAgIGludCBjb252ZXgoaW50IHgpCiAgICB7CiAgICAgICAgaWYoIXN6KQogICAgICAgICAgICByZXR1cm4gSU5GOwogICAgICAgIGludCBqID0gdXBwZXJfYm91bmQoZnIuYmVnaW4oKSwgZnIuZW5kKCksIHgpIC0gZnIuYmVnaW4oKSAtIDE7CiAgICAgICAgcmV0dXJuIGtbal0gKiB4ICsgYltqXTsKICAgIH0KfTsKCmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkKICAgIHsKICAgICAgICBpbnQgbjsKICAgICAgICBjaW4gPj4gbjsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICAgICAgY2luID4+IGFbaV07CiAgICAgICAgYW5zLmNsZWFyKCk7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgewogICAgICAgICAgICBpbnQgcG9zID0gKG4gLSAxICsgaSkgJSBuOwogICAgICAgICAgICBpbnQgaiA9IChwb3MgLSA0ICsgbikgJSBuOwogICAgICAgICAgICBpbnQgY250ID0gMDsKICAgICAgICAgICAgd2hpbGUgKGFbcG9zXSA+IDApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNudCsrOwogICAgICAgICAgICAgICAgaWYgKGNudCA9PSBuICogMikgYnJlYWs7CiAgICAgICAgICAgICAgICBpZiAoYVtqXSA+IDAgJiYgcG9zICE9IGopCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYW5zLnB1c2hfYmFjayh7cG9zICsgMSwgaiArIDF9KTsKICAgICAgICAgICAgICAgICAgICBhW3Bvc10tLTsKICAgICAgICAgICAgICAgICAgICBhW2pdLS07CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBqLS07CiAgICAgICAgICAgICAgICBqICs9IG47CiAgICAgICAgICAgICAgICBqICU9IG47CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBhbnMuc2l6ZSgpIDw8ICdcbic7CiAgICAgICAgZm9yIChhdXRvIGkgOiBhbnMpCiAgICAgICAgICAgIGNvdXQgPDwgaS5maXJzdCA8PCAiICIgPDwgaS5zZWNvbmQgPDwgJ1xuJzsKICAgIH0KfQo=