放到堆里,每次取出堆顶的两个元素来判断就好了
// Problem: D. Productive Meeting
// Contest: Codeforces - Codeforces Round #744 (Div. 3)
// URL: https://codeforces.com/contest/1579/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
using namespace std;
#define ff first
#define ss second
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b; i>=a; --i)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+100;
int n;
struct node{
int l,r;
}ans[N];
void solve()
{
priority_queue<PII, vector<PII>, less<PII> >s;
scanf("%d", &n);
rep(i,1,n)
{
int tmp;
cin >> tmp;
s.push({tmp,i});
}
int cnt = 0;
while(true)
{
auto a = s.top(); s.pop();
auto b = s.top(); s.pop();
if(a.ff==0 or b.ff == 0) break;
else
{
ans[cnt++] = {a.ss, b.ss};
s.push({a.ff-1,a.ss});
s.push({b.ff-1,b.ss});
}
}
cout << cnt << endl;
for(int i = 0; i < cnt; i++)
{
cout << ans[i].l << " " << ans[i].r << endl;
}
}
int main()
{
int Case;scanf("%d", &Case);
while(Case--)
solve();
return 0;
}