An important meeting is to be held and there are exactly $n$ people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting.

Each person has limited sociability. The sociability of the $i$-th person is a non-negative integer $a_i$. This means that after exactly $a_i$ talks this person leaves the meeting (and does not talk to anyone else anymore). If $a_i = 0$, the $i$-th person leaves the meeting immediately after it starts.

A meeting is considered most productive if the maximum possible number of talks took place during it.

You are given an array of sociability $a$, determine which people should talk to each other so that the total number of talks is as large as possible.


-----Input-----

The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases.

The next $2t$ lines contain descriptions of the test cases.

The first line of each test case description contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) —the number of people in the meeting. The second line consists of $n$ space-separated integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 2 \cdot 10^5$) — the sociability parameters of all people.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is also guaranteed that the sum of all $a_i$ (over all test cases and all $i$) does not exceed $2 \cdot 10^5$.


-----Output-----

Print $t$ answers to all test cases.

On the first line of each answer print the number $k$ — the maximum number of talks possible in a meeting.

On each of the next $k$ lines print two integers $i$ and $j$ ($1 \le i, j \le n$ and $i \neq j$) — the numbers of people who will have another talk.

If there are several possible answers, you may print any of them.


-----Examples-----

Input
8
2
2 3
3
1 2 3
4
1 2 3 4
3
0 0 2
2
6 2
3
0 0 2
5
8 2 0 1 1
5
0 1 0 0 6
Output
2
1 2
1 2
3
1 3
2 3
2 3
5
1 3
2 4
2 4
3 4
3 4
0
2
1 2
1 2
0
4
1 2
1 5
1 4
1 2
1
5 2


-----Note-----

None

Difficulty: MEDIUM
Tags: Greedy algorithms

Graph algorithms

Constructive algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
import math
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192
from bisect import bisect_left
from bisect import bisect_right

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import heapq
from collections import deque
import heapq
t = int(input())
for i in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append([-b[i], i + 1])
	ans = []
	heapq.heapify(l)
	le = n
	while le > 1:
		curr1 = heapq.heappop(l)
		curr2 = heapq.heappop(l)
		if curr2[0] == 0 or curr1[0] == 0:
			break
		else:
			heapq.heappush(l, [-(-curr1[0] - 1), curr1[1]])
			heapq.heappush(l, [-(-curr2[0] - 1), curr2[1]])
			ans.append([curr1[1], curr2[1]])
	print(len(ans))
	for j in ans:
		print(*j)


from sys import stdin
from heapq import *
for _ in range(int(input())):
	n = int(input())
	heap = []
	for (j, i) in enumerate(map(int, input().split())):
		if i != 0:
			heap.append([-i, j + 1])
	heapify(heap)
	j = n - 1
	ans = []
	while heap:
		curr = heappop(heap)
		while curr[0] and heap:
			nxt = heappop(heap)
			nxt[0] += 1
			curr[0] += 1
			ans.append(f'{nxt[1]} {curr[1]}')
			if nxt[0]:
				heappush(heap, nxt)
	print(len(ans))
	print(*ans, sep='\n')


import decimal
import heapq
import math
import os
import sys
from collections import Counter, deque
from io import BytesIO, IOBase
import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def ints_get():
	return map(int, input().strip().split())

def list_get():
	return list(map(int, sys.stdin.readline().strip().split()))

def chars_get():
	return list(map(str, sys.stdin.readline().strip().split()))

def output(to_be_printed, end='\n'):
	sys.stdout.write(str(to_be_printed) + end)

def ipn():
	return int(input())

def main():
	for _ in range(ipn()):
		n = ipn()
		a = list_get()
		v = []
		heapq.heapify(v)
		dic = {}
		val = 0
		for i in range(n):
			if a[i] == 0:
				continue
			heapq.heappush(v, [-1 * a[i], i + 1])
			dic[i + 1] = a[i]
			val += 1
		out = []
		while val > 1:
			x = heapq.heappop(v)
			x[0] += 1
			e = v.pop()
			y = e[1]
			out.append([x[1], y])
			dic[y] -= 1
			if dic[y] == 0:
				dic.pop(y)
				val -= 1
			dic[x[1]] -= 1
			if dic[x[1]] == 0:
				dic.pop(x[1])
				val -= 1
			e[0] += 1
			if x[0] < 0:
				heapq.heappush(v, x)
			if e[0] < 0:
				heapq.heappush(v, e)
		output(len(out))
		if len(out) > 0:
			for i in out:
				print(*i)
	return
main()


from collections import Counter, defaultdict, deque
from sys import stdin, stdout
from heapq import heapify, heappop, heappush
import io, os, sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from collections import OrderedDict
from bisect import bisect_left, bisect_right
mod = 998244353

def mapinput():
	return map(int, input().split())

def listinput():
	return [int(i) for i in input().split()]

def intinput():
	return int(input())

def strinput():
	return input().strip()

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	ans = []
	for p in range(n + 1):
		if prime[p]:
			ans.append(p)
	return ans

def writ(ss):
	stdout.write(str(ss) + '\n')

def mult(a, b, mod):
	return a % mod * (b % mod) % mod
for test in range(intinput()):

	def solve():
		n = intinput()
		arr = listinput()
		maxh = [[-arr[i], i] for i in range(n) if arr[i] != 0]
		heapify(maxh)
		ans = []
		while len(maxh) > 1:
			ele1 = heappop(maxh)
			ele2 = heappop(maxh)
			ans.append((ele1[1], ele2[1]))
			ele1[0] += 1
			ele2[0] += 1
			if ele1[0] != 0:
				heappush(maxh, ele1)
			if ele2[0] != 0:
				heappush(maxh, ele2)
		print(len(ans))
		for i in ans:
			ss = str(i[0] + 1) + ' ' + str(i[1] + 1)
			writ(ss)
	solve()


import heapq as hp
t = int(input())
for i in range(t):
	answers = [[] for i in range(2)]
	_ = int(input())
	heap = [(-1 * int(item), idx + 1) for (idx, item) in enumerate(input().split()) if int(item) > 0]
	hp.heapify(heap)
	while len(heap) >= 2:
		person1 = hp.heappop(heap)
		person2 = hp.heappop(heap)
		answers[0].append(person1[1])
		answers[1].append(person2[1])
		if person1[0] <= -2:
			hp.heappush(heap, (person1[0] + 1, person1[1]))
		if person2[0] <= -2:
			hp.heappush(heap, (person2[0] + 1, person2[1]))
	print(len(answers[0]))
	for (i, ele) in enumerate(answers[0]):
		print(answers[1][i], answers[0][i])


from heapq import *
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	meet = [[-arr[i], i] for i in range(n) if arr[i] != 0]
	heapify(meet)
	ans = []
	while len(meet) > 1:
		p1 = heappop(meet)
		p2 = heappop(meet)
		p1[0] += 1
		p2[0] += 1
		ans.append([p1[1] + 1, p2[1] + 1])
		if p1[0] != 0:
			heappush(meet, p1)
		if p2[0] != 0:
			heappush(meet, p2)
	print(len(ans))
	for x in ans:
		print(*x)


from heapq import heapify, heappop, heappush

def f():
	n = int(input())
	l = list(map(int, input().split()))
	m = []
	for (i, x) in enumerate(l):
		if x != 0:
			m.append((-x, i + 1))
	heapify(m)
	c = 0
	t = []
	while len(m) > 1:
		(a, b) = heappop(m)
		(x, y) = heappop(m)
		c += 1
		t.append([b, y])
		a += 1
		x += 1
		if a != 0:
			heappush(m, (a, b))
		if x != 0:
			heappush(m, (x, y))
	print(len(t))
	for i in t:
		print(*i)
for _ in range(int(input())):
	f()


import heapq
from heapq import heapify, heappop, heappush, heappushpop, heapreplace
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = []
	for i in range(n):
		if a[i] == 0:
			continue
		x.append([-a[i], i + 1])
	heapify(x)
	ans = []
	while True:
		if len(x) <= 1:
			break
		(mx, pos1) = heappop(x)
		(mn, pos2) = heappop(x)
		mn += 1
		mx += 1
		ans.append([pos1, pos2])
		if mn != 0:
			heappush(x, [mn, pos2])
		if mx != 0:
			heappush(x, [mx, pos1])
	print(len(ans))
	for i in range(len(ans)):
		print(*ans[i])


from heapq import *
for u in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = []
	ans = []
	for i in range(n):
		if a[i] > 0:
			heappush(l, (-a[i], i + 1))
	while len(l) > 1:
		(a, b) = heappop(l)
		(x, y) = heappop(l)
		ans.append([b, y])
		(a, x) = (-a, -x)
		a -= 1
		x -= 1
		if a > 0:
			heappush(l, (-a, b))
		if x > 0:
			heappush(l, (-x, y))
	print(len(ans))
	for i in ans:
		print(*i)


from sys import stdin, stdout
input = stdin.readline
import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	vals = []
	for i in range(n):
		if a[i]:
			vals.append((-a[i], i))
	heapq.heapify(vals)
	ans = []
	while 1:
		if len(vals) <= 1:
			break
		a = heapq.heappop(vals)
		b = heapq.heappop(vals)
		ans.append((a[1] + 1, b[1] + 1))
		p = (a[0] + 1, a[1])
		if p[0] < 0:
			heapq.heappush(vals, p)
		p = (b[0] + 1, b[1])
		if p[0] < 0:
			heapq.heappush(vals, p)
	print(len(ans))
	for i in ans:
		print(*i)


import heapq

def get_first_and_second_largest(A):
	fl = A[0]
	fli = 0
	sl = -math.inf
	sli = -1
	for i in range(1, len(A)):
		v = A[i]
		if v >= fl:
			sl = fl
			sli = fli
			fl = v
			fli = i
		elif v > sl:
			sl = v
			sli = i
	assert sl != -math.inf
	assert sli != fli
	return ((fl, fli), (sl, sli))

def main():
	t = int(input())
	i = 0
	for _ in range(t):
		i += 1
		n = int(input())
		A = [int(i) for i in input().split(' ')]
		heap = [(-v, i) for (i, v) in enumerate(A)]
		heapq.heapify(heap)
		c = 0
		ans = []
		while True:
			(_, i) = heapq.heappop(heap)
			(_, j) = heapq.heappop(heap)
			if A[j] == 0:
				break
			c += 1
			ans.append(sorted([i + 1, j + 1]))
			A[i] -= 1
			A[j] -= 1
			heapq.heappush(heap, (-A[i], i))
			heapq.heappush(heap, (-A[j], j))
		print(c)
		for (l, r) in ans:
			print(l, r)
main()


from heapq import heappush, heappop
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	lst = []
	cnt = 0
	hq = []
	for i in range(n):
		heappush(hq, [-1 * arr[i], i])
	while len(hq) > 1:
		x = heappop(hq)
		y = heappop(hq)
		while len(hq) > 0 and hq[0][0] == 0:
			heappop(hq)
		if len(hq) == 0:
			cnt += min(-1 * x[0], -1 * y[0])
			lst.append([x[1], y[1], min(-1 * x[0], -1 * y[0])])
			break
		z = hq[0]
		cnt += -1 * y[0] - -1 * z[0] + 1
		lst.append([x[1], y[1], -1 * y[0] - -1 * z[0] + 1])
		if x[0] + -1 * y[0] - -1 * z[0] + 1 != 0:
			heappush(hq, [x[0] + -1 * y[0] - -1 * z[0] + 1, x[1]])
		if y[0] + -1 * y[0] - -1 * z[0] + 1 != 0:
			heappush(hq, [y[0] + -1 * y[0] - -1 * z[0] + 1, y[1]])
	print(cnt)
	for i in lst:
		for j in range(i[2]):
			print(i[0] + 1, i[1] + 1)


import sys
import os
from math import ceil, floor, sqrt, gcd, factorial, log, log2
from io import BytesIO, IOBase
from collections import Counter
from itertools import permutations, combinations
from bisect import bisect
from functools import reduce
from heapq import heappush, heappop, heapify
inf = int(10000000000.0)
BUFSIZE = 8192
inp = lambda : int(input())
mp = lambda : map(int, input().split())
lst = lambda : list(map(int, input().split()))

def main():
	for _ in range(inp()):
		n = inp()
		a = lst()
		f = [[-a[i], i] for i in range(n) if a[i] != 0]
		heapify(f)
		l = []
		while True:
			if len(f) == 0 or len(f) == 1:
				break
			else:
				a = heappop(f)
				b = heappop(f)
				a[0] += 1
				b[0] += 1
				l.append((a[1] + 1, b[1] + 1))
				if a[0] != 0:
					heappush(f, a)
				if b[0] != 0:
					heappush(f, b)
		print(len(l))
		for j in l:
			print(*j)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import collections as cc
import heapq as hp
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))

def heap_remove(heap, value):
	tombstones[value] = tombstones.get(value, 0) + 1
	while len(heap) and heap[0] in tombstones and tombstones[heap[0]]:
		heappop(heap)
for tc in range(int(input())):
	(n,) = I()
	ar = I()
	mx = []
	for i in range(n):
		if ar[i] > 0:
			hp.heappush(mx, [-ar[i], i])
	ans = 0
	temp = []
	while len(mx) > 1:
		l = hp.heappop(mx)
		r = hp.heappop(mx)
		l[0] = l[0] * -1
		r[0] = r[0] * -1
		temp.append([l[1] + 1, r[1] + 1])
		l[0] -= 1
		r[0] -= 1
		ans += 1
		if l[0] > 0:
			hp.heappush(mx, [-l[0], l[1]])
		if r[0] > 0:
			hp.heappush(mx, [-r[0], r[1]])
	print(ans)
	for i in temp:
		print(*i)


import heapq

def solve(n, array):
	modified = []
	for i in range(n):
		if array[i] != 0:
			heapq.heappush(modified, [-array[i], i + 1])
	heapq.heapify(modified)
	result = []
	count = 0
	while len(modified) > 1:
		(first, firstIndex) = heapq.heappop(modified)
		(second, secondIndex) = heapq.heappop(modified)
		if first + 1 != 0:
			heapq.heappush(modified, [first + 1, firstIndex])
		if second + 1 != 0:
			heapq.heappush(modified, [second + 1, secondIndex])
		result.append([firstIndex, secondIndex])
		count += 1
	return [count, result]
t = int(input())
for _ in range(t):
	n = int(input())
	array = [int(x) for x in input().split()]
	(count, result) = solve(n, array)
	print(count)
	for x in result:
		print(x[0], x[1])


import sys
import math
import collections
import heapq
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]
r = sys.stdin.readline
for _ in range(int(r())):
	N = int(r())
	L = list(map(int, r().split()))
	pq = []
	for i in range(N):
		if L[i]:
			heapq.heappush(pq, [-L[i], i + 1])
	ansL = []
	while len(pq) > 1:
		a = heapq.heappop(pq)
		b = heapq.heappop(pq)
		if a[0] + 1 < 0:
			heapq.heappush(pq, [a[0] + 1, a[1]])
		if b[0] + 1 < 0:
			heapq.heappush(pq, [b[0] + 1, b[1]])
		ansL.append((a[1], b[1]))
	print(len(ansL))
	for i in ansL:
		print(*i)


import sys, math, collections

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
MOD = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = get_array()
	A = [[arr[i], i + 1] for i in range(n)]
	A.sort()
	s = 0
	for i in range(n - 1):
		s += A[i][0]
	mx = A[-1][0]
	ans = []
	for i in range(n - 1):
		if s <= mx:
			break
		while s > mx and A[i][0]:
			ans.append((A[i][1], A[i + 1][1]))
			A[i][0] -= 1
			A[i + 1][0] -= 1
			s -= 2
	for i in range(n - 1):
		while A[i][0] and mx:
			ans.append((A[i][1], A[-1][1]))
			A[i][0] -= 1
			mx -= 1
	print(len(ans))
	for ele in ans:
		print(*ele)


import sys
import heapq

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_list_strings():
	return list(map(str, sys.stdin.readline().strip().split()))

def solve(n, arr):
	final = []
	temp = []
	for (i, ele) in enumerate(arr):
		if ele != 0:
			temp.append([-ele, i + 1])
	heapq.heapify(temp)
	l = len(temp)
	while l > 1:
		f = heapq.heappop(temp)
		s = heapq.heappop(temp)
		l -= 2
		c1 = abs(f[0])
		c2 = abs(s[0])
		c1 -= 1
		c2 -= 1
		final.append([f[1], s[1]])
		if c1 > 0:
			heapq.heappush(temp, [-c1, f[1]])
			l += 1
		if c2 > 0:
			heapq.heappush(temp, [-c2, s[1]])
			l += 1
	sys.stdout.write(str(len(final)) + '\n')
	for ele in final:
		sys.stdout.write(' '.join(map(str, ele)) + '\n')
T = get_int()
while T:
	n = get_int()
	arr = get_list()
	solve(n, arr)
	T -= 1


from heapq import *
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [[-arr[i], i] for i in range(n)]
	heapify(arr)
	ans = []
	while 1:
		l1 = heappop(arr)
		l2 = heappop(arr)
		if l1[0] == 0 or l2[0] == 0:
			break
		l1[0] += 1
		l2[0] += 1
		heappush(arr, l1)
		heappush(arr, l2)
		ans.append((l1[1] + 1, l2[1] + 1))
	print(len(ans))
	for i in ans:
		print(*i)


from __future__ import print_function
from math import *
from collections import deque
import os
import sys
from io import BytesIO, IOBase
import heapq

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	l = inlt()
	q = []
	out = []
	for i in range(n):
		heapq.heappush(q, (-1 * l[i], i + 1))
	while True:
		a = heapq.heappop(q)
		b = heapq.heappop(q)
		if b[0] == 0:
			break
		out.append([a[1], b[1]])
		heapq.heappush(q, (a[0] + 1, a[1]))
		heapq.heappush(q, (b[0] + 1, b[1]))
	print(len(out))
	for i in out:
		print(*i)


import sys
import math
ttime = int(input())
for ccount in range(ttime):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		for j in range(a[i]):
			b.append((a[i], i))
	b.sort()
	if len(b) % 2:
		b.pop()
	k = len(b) // 2
	ans = []
	for i in range(k):
		if b[i][1] != b[i + k][1]:
			ans.append((b[i][1], b[i + k][1]))
	print(len(ans))
	for x in ans:
		print(x[0] + 1, x[1] + 1)


import heapq
for _ in range(int(input())):
	input()
	heap = []
	for (i, x) in enumerate(map(int, input().split())):
		if x:
			heapq.heappush(heap, (-x, i))
	talks = []
	while len(heap) > 1:
		(x, i) = heapq.heappop(heap)
		(y, j) = heapq.heappop(heap)
		if x != -1:
			heapq.heappush(heap, (x + 1, i))
		if y != -1:
			heapq.heappush(heap, (y + 1, j))
		talks.append((i, j))
	print(len(talks))
	for (i, j) in talks:
		print(i + 1, j + 1)


import heapq
for i in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	arr = []
	c = 0
	for i in ar:
		if i != 0:
			arr.append([-1 * i, c])
		c += 1
	heapq.heapify(arr)
	ans = []
	while len(arr) > 1:
		x = heapq.heappop(arr)
		y = heapq.heappop(arr)
		ans.append([x[1] + 1, y[1] + 1])
		x[0] += 1
		y[0] += 1
		if x[0] < 0:
			heapq.heappush(arr, x)
		if y[0] < 0:
			heapq.heappush(arr, y)
	print(len(ans))
	for i in ans:
		print(*i)


import heapq
import math
import sys
import collections
import bisect
import random
import itertools
import string
from collections import Counter, defaultdict

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	n = int(input())
	temp = get_list()
	arr = []
	for (i, j) in enumerate(temp):
		if j == 0:
			continue
		arr.append([-j, i + 1])
	heapq.heapify(arr)
	ans = []
	while len(arr) > 1:
		(m1, p1) = heapq.heappop(arr)
		(m2, p2) = heapq.heappop(arr)
		ans.append([p1, p2])
		m1 += 1
		m2 += 1
		if m1 != 0:
			heapq.heappush(arr, [m1, p1])
		if m2 != 0:
			heapq.heappush(arr, [m2, p2])
	print(len(ans))
	for i in ans:
		print(*i)


import collections
import math
from functools import lru_cache
import heapq

def process(tc, N, A):
	res = 0
	h = []
	out = []
	for i in range(N):
		if A[i] > 0:
			heapq.heappush(h, (-A[i], i + 1))
	while len(h) > 1:
		(fc, f) = heapq.heappop(h)
		(sc, s) = heapq.heappop(h)
		res += 1
		fc += 1
		sc += 1
		out.append([f, s])
		if fc < 0:
			heapq.heappush(h, (fc, f))
		if sc < 0:
			heapq.heappush(h, (sc, s))
	print(res)
	if res:
		for v in out:
			print(*v)
tcs = int(input())
for tc in range(tcs):
	N = int(input())
	A = list(map(int, input().split()))
	process(tc + 1, N, A)


Q = int(input())

def main():
	N = int(input())
	A = list(map(int, input().split()))
	B = []
	for (i, a) in enumerate(A):
		for _ in range(a):
			B.append((a, i))
	B.sort()
	M = len(B)
	ans = []
	for i in range(M // 2):
		j = i + M // 2
		if i > j:
			break
		(p, q) = B[i]
		(r, s) = B[j]
		if q == s:
			break
		ans.append([q + 1, s + 1])
	print(len(ans))
	for aa in ans:
		print(*aa)
for _ in range(Q):
	main()


import heapq

def solve():
	N = int(input())
	arr = list(map(int, input().split()))
	data = []
	output = []
	for i in range(N):
		if arr[i] > 0:
			data.append([-arr[i], i + 1])
	heapq.heapify(data)
	while len(data) > 1:
		a1 = heapq.heappop(data)
		b1 = heapq.heappop(data)
		output.append([a1[1], b1[1]])
		if a1[0] < -1:
			heapq.heappush(data, [a1[0] + 1, a1[1]])
		if b1[0] < -1:
			heapq.heappush(data, [b1[0] + 1, b1[1]])
	return output
remained_test_cases = 1
remained_test_cases = int(input())
while remained_test_cases > 0:
	output = solve()
	print(len(output))
	for i in range(len(output)):
		print(output[i][0], output[i][1])
	remained_test_cases -= 1


from heapq import *

def get_max(a):
	a = [(-i, index + 1) for (index, i) in enumerate(a) if i > 0]
	heapify(a)
	ans = 0
	m = []
	while len(a) > 1:
		ans += 1
		(f, i1) = heappop(a)
		(s, i2) = heappop(a)
		m.append((min(i1, i2), max(i1, i2)))
		s += 1
		if s != 0:
			heappush(a, (s, i2))
		f += 1
		if f != 0:
			heappush(a, (f, i1))
	return (ans, m)
for _ in range(int(input())):
	n = input()
	a = input().split()
	a = [int(i) for i in a]
	(ans, m) = get_max(a)
	print(ans)
	for i in m:
		print(*i)


def solve():
	n = int(input())
	a = list(map(int, input().split()))
	b = [None] * n
	for i in range(n):
		b[i] = (-a[i], i)
	b.sort()
	c = []
	l = 0
	s = sum(a)
	z = b[0][1]
	while a[z] > s // 2:
		a[z] -= 1
		s -= 1
	for i in range(n):
		z = b[i][1]
		for j in range(a[z]):
			c.append(z)
	if len(c) % 2 == 1:
		c.pop()
	r = []
	for i in range(len(c) // 2):
		r.append(str(c[i] + 1) + ' ' + str(c[i + len(c) // 2] + 1))
	print(r.__len__())
	if len(r) > 0:
		print('\n'.join(r))
for i in range(int(input())):
	solve()


import os, sys
from io import BytesIO, IOBase
from collections import deque, Counter, defaultdict as dft
from heapq import heappop, heappush, heapify
from math import log, sqrt, factorial, cos, tan, sin, radians, log2, ceil, floor, gcd
from bisect import bisect, bisect_left, bisect_right
from decimal import *
import sys, threading
from itertools import permutations, combinations
from copy import deepcopy
input = sys.stdin.readline

def modI(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x += m0
	return x
ii = lambda : int(input())
si = lambda : input().rstrip()
mp = lambda : map(int, input().split())
ms = lambda : map(str, input().strip().split(' '))
ml = lambda : list(mp())
mf = lambda : map(float, input().split())
alphs = 'abcdefghijklmnopqrstuvwxyz'

def solve():
	n = ii()
	arr = ml()
	h = []
	for i in range(n):
		if arr[i]:
			heappush(h, [-arr[i], i + 1])
	res = []
	while len(h) > 1:
		(x, i1) = heappop(h)
		(y, i2) = heappop(h)
		x = -x
		y = -y
		res.append([i1, i2])
		x -= 1
		y -= 1
		if x:
			heappush(h, [-x, i1])
		if y:
			heappush(h, [-y, i2])
	print(len(res))
	for ls in res:
		print(*ls)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=gcd):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		start += self._size
		stop += self._size
		res_left = res_right = self._default
		while start < stop:
			if start & 1:
				res_left = self._func(res_left, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res_right = self._func(self.data[stop], res_right)
			start >>= 1
			stop >>= 1
		return self._func(res_left, res_right)

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
tc = 1
tc = ii()
for i in range(tc):
	solve()


t = int(input())
import heapq
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	maxHeap = []
	for i in range(len(arr)):
		if arr[i] > 0:
			maxHeap.append((-arr[i], i))
	heapq.heapify(maxHeap)
	answer = []
	while len(maxHeap) > 1:
		(first, fidx) = heapq.heappop(maxHeap)
		(second, sidx) = heapq.heappop(maxHeap)
		answer.append([sidx + 1, fidx + 1])
		if -1 * first - 1 > 0:
			heapq.heappush(maxHeap, (first + 1, fidx))
		if -1 * second - 1 > 0:
			heapq.heappush(maxHeap, (second + 1, sidx))
	print(len(answer))
	for a in answer:
		print(*a)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	(*a,) = map(int, input().split())
	b = [None] * n
	for i in range(n):
		b[i] = (-a[i], i)
	b.sort()
	c = []
	l = 0
	s = sum(a)
	while True:
		if a[b[0][1]] > s // 2:
			for i in range(0, n):
				z = b[i][1]
				if a[z] > s // 2:
					a[z] -= 1
					s -= 1
				else:
					break
		else:
			break
	for i in range(n):
		id = b[i][1]
		for j in range(a[id]):
			c.append(id)
	if len(c) % 2 == 1:
		c.pop()
	r = []
	for i in range(len(c) // 2):
		r.append(str(c[i] + 1) + ' ' + str(c[i + len(c) // 2] + 1))
	print(len(r))
	if len(r) > 0:
		print('\n'.join(r))
for i in range(int(input())):
	solve()


from heapq import heappush, heappop, heapify

def getIntegerInputs():
	return map(int, input().split())

def getListInput():
	return list(map(int, input().split()))

def inbound(r, c, n, m):
	return 0 <= r < n and 0 <= c < m

def solve():
	for _ in range(int(input())):
		ans = []
		n = int(input())
		arr = getListInput()
		heap = [(-val, i) for (i, val) in enumerate(arr)]
		heapify(heap)
		while len(heap) > 1:
			r = heappop(heap)
			l = heappop(heap)
			if r[0] == 0 or l[0] == 0:
				break
			heappush(heap, (l[0] + 1, l[1]))
			heappush(heap, (r[0] + 1, r[1]))
			ans.append((r[1] + 1, l[1] + 1))
		print(len(ans))
		for a in ans:
			print(*a)
solve()


import sys
import io, os
import math
from heapq import *
gcd = math.gcd
sqrt = math.sqrt

def ceil(a, b):
	a = -a
	k = a // b
	k = -k
	return k
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def strinp(testcases):
	k = 5
	if testcases == -1 or testcases == 1:
		k = 1
	f = str(input())
	f = f[2:len(f) - k]
	return f

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = list(map(int, input().split()))
		for i in range(n):
			arr[i] = [-arr[i], i + 1]
		heapify(arr)
		ans = []
		while True:
			x = heappop(arr)
			x1 = -x[0]
			y = heappop(arr)
			y1 = -y[0]
			if y1 == 0:
				break
			ans.append('{} {}'.format(x[1], y[1]))
			x[0] += 1
			y[0] += 1
			heappush(arr, x)
			heappush(arr, y)
		c = len(ans)
		print(c)
		for i in range(c):
			print(ans[i])
main()


from heapq import heappop, heappush
for t in range(int(input())):
	n = input()
	a = list(map(int, input().split()))
	q = []
	ans = []
	x = 1
	for c in a:
		if c != 0:
			heappush(q, (-c, x))
		x += 1
	while len(q) > 1:
		(c, x) = heappop(q)
		(d, y) = heappop(q)
		if c + 1 != 0:
			heappush(q, (c + 1, x))
		if d + 1 != 0:
			heappush(q, (d + 1, y))
		ans += ((x, y),)
	print(len(ans))
	for (i, j) in ans:
		print(i, j)


import os
import sys
from io import BytesIO, IOBase

def main():
	from heapq import heapify, heappush, heappop
	t = int(input())
	for _ in range(t):
		n = int(input())
		li = list(map(int, input().split()))
		heap = [(-v, i + 1) for (i, v) in enumerate(li) if v != 0]
		heapify(heap)
		count = 0
		result = []
		while len(heap) >= 2:
			count += 1
			(av, ai) = heappop(heap)
			(bv, bi) = heappop(heap)
			result.append((ai, bi))
			av += 1
			if av:
				heappush(heap, (av, ai))
			bv += 1
			if bv:
				heappush(heap, (bv, bi))
		print(count)
		for v in result:
			print(v[0], v[1])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
from collections import defaultdict as dd
import heapq
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdmap = lambda : map(int, stdstr().split())
stdarr = lambda : list(map(int, stdstr().split()))
mod = 1000000007
for _ in range(stdint()):
	n = stdint()
	arr = stdarr()
	res = []
	maxHeap = []
	for i in range(n):
		if arr[i] != 0:
			heapq.heappush(maxHeap, [-arr[i], i])
	while len(maxHeap) > 1:
		currMax = heapq.heappop(maxHeap)
		currMin = heapq.heappop(maxHeap)
		imax = currMax[1]
		imin = currMin[1]
		if imax == imin:
			continue
		ma = -currMax[0]
		mi = -currMin[0]
		res.append([imin + 1, imax + 1])
		newMax = [-(ma - 1), imax]
		newMin = [-(mi - 1), imin]
		if -newMax[0] > 0:
			heapq.heappush(maxHeap, newMax)
		if -newMin[0] > 0:
			heapq.heappush(maxHeap, newMin)
	print(len(res))
	for i in res:
		print(*i)


from heapq import heappop, heappush

def List():
	return list(map(int, input().split()))

def Lst():
	return list(input())

def Int():
	return int(input())

def two():
	return map(int, input().split())
for _ in range(Int()):
	tot_num_of_attendants = Int()
	mx_heap = []
	attendants_talk = List()
	for (index, talk) in enumerate(attendants_talk):
		if talk != 0:
			heappush(mx_heap, (-talk, index + 1))
	output = []
	length = len(mx_heap)
	while length > 1:
		(talk1, id1) = heappop(mx_heap)
		(talk2, id2) = heappop(mx_heap)
		talk3 = -1
		if mx_heap:
			(talk3, id3) = heappop(mx_heap)
			heappush(mx_heap, (talk3, id3))
		num_of_talks = abs(talk2 - talk3) + 1
		for _ in range(num_of_talks):
			output.append([id2, id1])
		remaining = abs(talk1) - num_of_talks
		if remaining != 0:
			heappush(mx_heap, (-remaining, id1))
		remaining = abs(talk2) - num_of_talks
		if remaining != 0:
			heappush(mx_heap, (-remaining, id2))
		length = len(mx_heap)
	print(len(output))
	for op in output:
		print(*op)


import sys
import heapq

def rall():
	return sys.stdin.readlines()

def rl():
	return sys.stdin.readline().strip()

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something=''):
	sys.stdout.write(str(something) + '\n')

def pra(array):
	sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
	return array
NT = int(rl())
for ti in range(NT):
	_ = rl()
	array = list(map(int, rl().split(' ')))
	MaxQ = []
	for i in range(len(array)):
		if array[i] > 0:
			heapq.heappush(MaxQ, [-array[i], -(i + 1)])
	talks = []
	while len(MaxQ) > 1:
		big1 = heapq.heappop(MaxQ)
		big2 = heapq.heappop(MaxQ)
		(a, ai) = (-big1[0], -big1[1])
		(b, bi) = (-big2[0], -big2[1])
		k = max(b // 2, 1)
		pair = sorted([ai, bi])
		talks.extend([pair] * k)
		a -= k
		b -= k
		if a > 0:
			heapq.heappush(MaxQ, [-a, -ai])
		if b > 0:
			heapq.heappush(MaxQ, [-b, -bi])
	pr(len(talks))
	for pair in talks:
		pra(pair)


import sys
from math import factorial, gcd
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest, nsmallest
from itertools import groupby, accumulate
from copy import deepcopy
MOD = 10 ** 9 + 7
INF = float('inf')
input = sys.stdin.readline
rl = lambda : list(map(int, input().split()))
rs = lambda : sys.stdin.readline().strip()
for _ in range(int(input())):
	n = int(input())
	A = rl()
	count = 0
	heap = [(-a, i) for (i, a) in enumerate(A) if a != 0]
	heapify(heap)
	res = []
	while len(heap) > 1:
		count += 1
		(x, y) = (heappop(heap), heappop(heap))
		res.append((x[1] + 1, y[1] + 1))
		if x[0] + 1 != 0:
			heappush(heap, (x[0] + 1, x[1]))
		if y[0] + 1 != 0:
			heappush(heap, (y[0] + 1, y[1]))
	print(count)
	for i in range(count):
		print(*res[i])


import heapq
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = []
	for i in range(n):
		if l[i] > 0:
			x.append([-l[i], i])
	heapq.heapify(x)
	a = 0
	ans = []
	while len(x) > 1:
		xx = heapq.heappop(x)
		yy = heapq.heappop(x)
		a += 1
		xx[0] += 1
		yy[0] += 1
		ans.append([xx[1] + 1, yy[1] + 1])
		if xx[0] < 0:
			heapq.heappush(x, xx)
		if yy[0] < 0:
			heapq.heappush(x, yy)
	print(a)
	for i in ans:
		print(*i)


import heapq
test = eval(input())
for i in range(test):
	n = eval(input())
	numbers = list(map(int, input().split()))
	heap = []
	ans = []
	for i in range(len(numbers)):
		heapq.heappush(heap, (-numbers[i], i + 1))
	while True:
		(x, i) = heapq.heappop(heap)
		(y, j) = heapq.heappop(heap)
		if x == 0 or y == 0:
			break
		ans.append([i, j])
		heapq.heappush(heap, (x + 1, i))
		heapq.heappush(heap, (y + 1, j))
	print(len(ans))
	for i in ans:
		print(*i)


import sys
import io, os
input = sys.stdin.buffer.readline
t = int(input())
import heapq
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	q = []
	for (i, a) in enumerate(A):
		if a:
			q.append((-a, i))
	heapq.heapify(q)
	ans = []
	while len(q) >= 2:
		(a, i) = heapq.heappop(q)
		(b, j) = heapq.heappop(q)
		(a, b) = (-a, -b)
		ans.append((i + 1, j + 1))
		a -= 1
		b -= 1
		if a:
			heapq.heappush(q, (-a, i))
		if b:
			heapq.heappush(q, (-b, j))
	print(len(ans))
	for i in range(len(ans)):
		print(*ans[i])


from heapq import heappush, heappop
for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	h = []
	z = 0
	ans = 0
	talks = []
	for i in s:
		if i != 0:
			heappush(h, (-i, z))
		z += 1
	while len(h) > 1:
		(a, x) = heappop(h)
		(b, y) = heappop(h)
		if a != -1:
			heappush(h, (a + 1, x))
		if b != -1:
			heappush(h, (b + 1, y))
		talks += [[x + 1, y + 1]]
		ans += 1
	print(ans)
	for i in talks:
		print(*i)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import heapq as hp
import bisect as bs
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	v = []
	for i in range(n):
		if l[i] == 0:
			continue
		v.append([-l[i], i + 1])
	v.sort()
	ans = []
	hp.heapify(v)
	while len(v) >= 2:
		x = hp.heappop(v)
		y = hp.heappop(v)
		x[0] += 1
		y[0] += 1
		ans.append([x[1], y[1]])
		if x[0] < 0:
			hp.heappush(v, x)
		if y[0] < 0:
			hp.heappush(v, y)
	print(len(ans))
	for i in ans:
		print(*i)


import heapq
t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = []
	q = []
	for i in range(n):
		heapq.heappush(q, (-a[i], i + 1))
	while q:
		(value1, node1) = heapq.heappop(q)
		(value2, node2) = heapq.heappop(q)
		if value1 == 0 or value2 == 0:
			break
		value1 *= -1
		value2 *= -1
		ans.append([node1, node2])
		heapq.heappush(q, (-(value1 - 1), node1))
		heapq.heappush(q, (-(value2 - 1), node2))
	m = len(ans)
	print(m)
	for i in range(m):
		print(*ans[i])


from collections import defaultdict
from sys import stdin, stdout
(input, print) = (stdin.readline, stdout.write)
t = int(input())
for tt in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0 for _ in range(max(a) + 10)]
	d = defaultdict(set)
	for i in range(n):
		if a[i] == 0:
			continue
		d[a[i]].add(i)
		b[a[i]] += 1
	l = 0
	r = len(b) - 1
	ops = []
	while l < r:
		while b[l] == 0 and l < r:
			l += 1
		while b[r] == 0 and r > l:
			r -= 1
		min_l = l
		if l <= r:
			if l < r:
				total_people = min(b[l], b[r])
			else:
				total_people = b[l] // 2
			if b[l] == 0 or b[r] == 0 or r == 0 or (l == 0):
				break
			for i in range(total_people):
				lef = d[l].pop()
				ref = d[r].pop()
				ops.append(f'{lef + 1} {ref + 1}')
				b[l] -= 1
				b[r] -= 1
				if l - 1 > 0:
					d[l - 1].add(lef)
					b[l - 1] += 1
				if r - 1 > 0:
					d[r - 1].add(ref)
					b[r - 1] += 1
				min_l = min(min_l, l - 1)
			l = min_l
		else:
			break
	print(str(len(ops)))
	print('\n')
	for i in range(len(ops)):
		print(ops[i])
		if i == len(ops) - 1 and tt == t - 1:
			break
		print('\n')


import heapq
for ik in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = [(-a[i], i) for i in range(n)]
	heapq.heapify(a)
	ans = []
	while True:
		q = heapq.heappop(a)
		q1 = heapq.heappop(a)
		if q[0] == 0 or q1[0] == 0:
			break
		ans.append((q[1] + 1, q1[1] + 1))
		heapq.heappush(a, (q[0] + 1, q[1]))
		heapq.heappush(a, (q1[0] + 1, q1[1]))
	print(len(ans))
	for i in ans:
		print(*i)


import math
from collections import Counter, defaultdict
from functools import lru_cache
from queue import PriorityQueue
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(i) for i in input().split()]
	q = PriorityQueue()
	for i in range(n):
		if arr[i] > 0:
			q.put((-arr[i], i))
	count = 0
	ans = []
	while not q.empty():
		(temp1, ind1) = q.get()
		temp1 *= -1
		if not q.empty():
			(temp2, ind2) = q.get()
			temp2 *= -1
			count += 1
			ans.append([ind1 + 1, ind2 + 1])
			if temp1 - 1 > 0:
				q.put((-(temp1 - 1), ind1))
			if temp2 - 1 > 0:
				q.put((-(temp2 - 1), ind2))
		else:
			break
	print(count)
	for i in ans:
		(l, r) = i
		print(l, r)


import heapq
for case in range(int(input())):
	ans = 0
	n = int(input())
	soci = list(map(int, input().split()))
	soci_maxheap = [(-i, i, idx + 1) for (idx, i) in enumerate(soci)]
	heapq.heapify(soci_maxheap)
	out = []
	while True:
		maxi = heapq.heappop(soci_maxheap)
		maxi_2 = heapq.heappop(soci_maxheap)
		if maxi_2[0] == 0:
			break
		out.append((maxi[2], maxi_2[2]))
		after = (maxi[0] + 1, maxi[1], maxi[2])
		after_2 = (maxi_2[0] + 1, maxi_2[1], maxi_2[2])
		ans += 1
		heapq.heappush(soci_maxheap, after)
		heapq.heappush(soci_maxheap, after_2)
	print(ans)
	for i in out:
		print(*i)


from heapq import heapify, heappop, heappush
for _ in range(int(input())):
	s = int(input())
	r = list(map(int, input().split()))
	m = []
	result = []
	count = 0
	for (i, e) in enumerate(r):
		if e != 0:
			m.append((-1 * e, i + 1))
	heapify(m)
	while len(m) > 1:
		(x, y) = heappop(m)
		x = -x
		(a, b) = heappop(m)
		a = -a
		count += a
		result.append((y, b))
		x -= 1
		a -= 1
		if x > 0:
			heappush(m, (-x, y))
		if a > 0:
			heappush(m, (-a, b))
	print(len(result))
	for (x, y) in result:
		print(x, y)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	people = list(map(int, input().split()))
	h = []
	for i in range(n):
		if people[i] != 0:
			heapq.heappush(h, [-people[i], i])
	ans = []
	while len(h) > 1:
		a = heapq.heappop(h)
		b = heapq.heappop(h)
		a[0] = a[0] + 1
		b[0] = b[0] + 1
		if a[0] != 0:
			heapq.heappush(h, a)
		if b[0] != 0:
			heapq.heappush(h, b)
		ans.append([a[1] + 1, b[1] + 1])
	print(len(ans))
	for r in ans:
		print(*r)


from heapq import heappush, heappop

def solve(nums, N):
	h = []
	for (i, num) in enumerate(nums):
		if num == 0:
			continue
		heappush(h, (-num, i + 1))
	(k, pairs) = (0, [])
	while len(h) >= 2:
		(n1, i1) = heappop(h)
		(n2, i2) = heappop(h)
		pairs.append((i1, i2))
		k += 1
		if n1 < -1:
			heappush(h, (n1 + 1, i1))
		if n2 < -1:
			heappush(h, (n2 + 1, i2))
	return (k, pairs)

def main():
	from sys import stdin
	T = int(stdin.readline().strip())
	for _ in range(T):
		N = int(stdin.readline().strip())
		nums = list(map(int, stdin.readline().strip().split()))
		(k, pairs) = solve(nums, N)
		print(k)
		for (i, j) in pairs:
			print(f'{i} {j}')
main()


import heapq as hp
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	mx = []
	for i in range(n):
		if ar[i] > 0:
			hp.heappush(mx, [-ar[i], i])
	ans = 0
	temp = []
	while len(mx) > 1:
		l = hp.heappop(mx)
		r = hp.heappop(mx)
		l[0] = l[0] * -1
		r[0] = r[0] * -1
		temp.append([l[1] + 1, r[1] + 1])
		l[0] -= 1
		r[0] -= 1
		ans += 1
		if l[0] > 0:
			hp.heappush(mx, [-l[0], l[1]])
		if r[0] > 0:
			hp.heappush(mx, [-r[0], r[1]])
	print(ans)
	for i in temp:
		print(*i)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	(ans, ans_list) = (0, [])
	q = []
	for i in range(n):
		if a[i] == 0:
			continue
		idx = i + 1
		heapq.heappush(q, [-a[i], idx])
	while True:
		if len(q) in [0, 1]:
			break
		(x, y) = (heapq.heappop(q), heapq.heappop(q))
		x[0] += 1
		y[0] += 1
		ans += 1
		ans_list.append((x[1], y[1]))
		if x[0]:
			heapq.heappush(q, x)
		if y[0]:
			heapq.heappush(q, y)
	print(ans)
	for answer in ans_list:
		print(*answer)


from heapq import heappush, heappop
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	heap = []
	for i in range(n):
		if arr[i] > 0:
			heappush(heap, (-arr[i], i + 1))
	ans = []
	while len(heap) >= 2:
		(s1, p1) = heappop(heap)
		(s2, p2) = heappop(heap)
		ans.append([p1, p2])
		s1 += 1
		s2 += 1
		if s1 < 0:
			heappush(heap, (s1, p1))
		if s2 < 0:
			heappush(heap, (s2, p2))
	print(len(ans))
	for p in ans:
		print(p[0], p[1])


import sys, heapq
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	d = [(-int(i), idx + 1) for (idx, i) in enumerate(sys.stdin.readline().split()) if int(i) != 0]
	heapq.heapify(d)
	r = []
	while len(d) > 1:
		((n1, i1), (n2, i2)) = (heapq.heappop(d), heapq.heappop(d))
		n1 = -n1
		n2 = -n2
		while n2 > 0 and (not d or n2 >= -d[0][0]):
			r.append('%d %d\n' % (i1, i2))
			n1 -= 1
			n2 -= 1
		if n2 > 0:
			heapq.heappush(d, (-n2, i2))
		if n1 > 0:
			heapq.heappush(d, (-n1, i1))
	sys.stdout.write('%d\n%s' % (len(r), ''.join(r)))


from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations, accumulate, zip_longest, combinations_with_replacement
from math import gcd, floor, inf, log2, sqrt, log10, factorial
from bisect import bisect_right, bisect_left, insort_left
from statistics import mode
from string import ascii_lowercase, ascii_uppercase
from heapq import heapify, heappop, heappush, heappushpop, heapreplace, nlargest, nsmallest, merge
from copy import deepcopy
from random import shuffle
cases = int(input())
for _ in range(cases):
	num = int(input())
	arr = list(map(int, input().split()))
	new_arr = [(-n, i) for (i, n) in enumerate(arr, start=1)]
	heapify(new_arr)
	ans = []
	while len(new_arr) > 1:
		(n1, p1) = heappop(new_arr)
		(n2, p2) = heappop(new_arr)
		if n1 == 0 or n2 == 0:
			break
		ans.append([p1, p2])
		n1 += 1
		n2 += 1
		if n1 != 0:
			heappush(new_arr, (n1, p1))
		if n2 != 0:
			heappush(new_arr, (n2, p2))
	print(len(ans))
	for (i, j) in ans:
		print(i, j)


inf = float('inf')
import sys
import pprint
import logging
from logging import getLogger
import array

def input():
	return sys.stdin.readline().rstrip('\r\n')

def maps():
	return [int(i) for i in input().split()]
logging.basicConfig(format='%(message)s', level=logging.WARNING)
logger = getLogger(__name__)
logger.setLevel(logging.INFO)

def debug(msg, *args):
	logger.info(f'{msg}={pprint.pformat(args)}')
from heapq import heapify, heappop, heappush
for _ in range(*maps()):
	(n,) = maps()
	a = [*maps()]
	h = []
	for (i, j) in enumerate(a):
		h.append((-j, i + 1))
	heapify(h)
	ans = []
	while h:
		(x, i) = heappop(h)
		(y, ii) = heappop(h)
		if x == 0 or y == 0:
			break
		heappush(h, (x + 1, i))
		heappush(h, (y + 1, ii))
		ans.append((i, ii))
	print(len(ans))
	[print(*i) for i in ans]


import heapq
sr = lambda : input()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
inf = 10 ** 18
mod = 998244353
test = ir()
for t in range(test):
	n = ir()
	a = lr()
	h = []
	ans = []
	for (ind, num) in enumerate(a):
		if num > 0:
			heapq.heappush(h, (-num, ind + 1))
	while len(h) > 1:
		(num1, ind1) = heapq.heappop(h)
		(num2, ind2) = heapq.heappop(h)
		ans.append([ind1, ind2])
		num1 += 1
		num2 += 1
		if num1 < 0:
			heapq.heappush(h, (num1, ind1))
		if num2 < 0:
			heapq.heappush(h, (num2, ind2))
	print(len(ans))
	for l in ans:
		print(*l, sep=' ')


from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import math
import heapq
import string
import random
(MIN, MAX, MOD) = (-1061109567, 1061109567, 1000000007)
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())
for _ in range(N()):
	n = N()
	a = RLL()
	pq = [[-x, i] for (i, x) in enumerate(a)]
	heapq.heapify(pq)
	res = []
	while len(pq) > 1:
		x = heapq.heappop(pq)
		y = heapq.heappop(pq)
		if x[0] == 0 or y[0] == 0:
			break
		res.append([x[1] + 1, y[1] + 1])
		if x[0] != -1:
			heapq.heappush(pq, [x[0] + 1, x[1]])
		if y[0] != -1:
			heapq.heappush(pq, [y[0] + 1, y[1]])
	print(len(res))
	for i in res:
		print(*i)


import sys
import math
import heapq
from collections import defaultdict as dd
from collections import OrderedDict as od
from collections import deque
from itertools import permutations as pp
from itertools import combinations as cc
from sys import stdin
from functools import cmp_to_key as ctk
from functools import lru_cache
from bisect import bisect_left as bs_l
from bisect import bisect_right as bs_r
input = sys.stdin.readline
mod = 10 ** 9 + 7
sys.setrecursionlimit(10 ** 5)

def comparator(x, y):
	if x[0] == y[0]:
		return x[1] - y[1]
	return True

def ncr(n, r):
	if n < r:
		return 0
	return fc[n] * pow(fc[r] * fc[n - r] % mod, mod - 2, mod) % mod
T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	hp = []
	for i in range(n):
		if arr[i] > 0:
			heapq.heappush(hp, [-arr[i], i])
	ll = []
	while len(hp) > 1:
		(l1, l2) = (heapq.heappop(hp), heapq.heappop(hp))
		(x, y) = (l1[0], l2[0])
		if x < 0 and y < 0:
			ll.append([l1[1] + 1, l2[1] + 1])
			heapq.heappush(hp, [x + 1, l1[1]])
			heapq.heappush(hp, [y + 1, l2[1]])
		else:
			break
	print(len(ll))
	for i in ll:
		print(*i)


import math
from collections import defaultdict
from heapq import heapify, heappush, heappop
t = int(input())
while t:
	t -= 1
	n = int(input())
	lis = list(map(int, input().split()))
	out = []
	for i in range(0, n):
		out.append([0 - lis[i], i + 1])
	heapify(out)
	flag = True
	s = 0
	ans = []
	while flag:
		a = heappop(out)
		b = heappop(out)
		if a[0] == 0 or b[0] == 0:
			break
		ans.append([a[1], b[1]])
		s = s + 1
		a[0] = 0 - (abs(a[0]) - 1)
		b[0] = 0 - (abs(b[0]) - 1)
		heappush(out, a)
		heappush(out, b)
	print(s)
	for i in ans:
		print(f'{i[0]} {i[1]}')


from heapq import heapify, heappop, heappush
for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [(-x, i + 1) for (i, x) in enumerate(arr) if x > 0]
	heapify(arr)
	count = 0
	ans = []
	while len(arr) > 1:
		(a, b) = (heappop(arr), heappop(arr))
		diff = abs(a[0] - b[0])
		count += 1
		ans.append(str(b[1]) + ' ' + str(a[1]))
		if a[0] + 1 != 0:
			heappush(arr, (a[0] + 1, a[1]))
		if b[0] + 1 != 0:
			heappush(arr, (b[0] + 1, b[1]))
	print(count)
	for i in ans:
		print(i)


from sys import stdin
from heapq import heappush, heappop, heapify
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	b = list(map(int, stdin.readline().split()))
	a = []
	k = []
	p = 0
	for i in range(n):
		a.append([-b[i], i])
	a.sort()
	heapify(a)
	while len(a) > 1:
		b = heappop(a)
		c = heappop(a)
		if max(b[0], c[0]) != 0:
			p += 1
			b[0] += 1
			c[0] += 1
			k.append([min(b[1], c[1]) + 1, max(b[1], c[1]) + 1])
			heappush(a, b)
			heappush(a, c)
		else:
			break
	print(p)
	for i in k:
		print(*i)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	h = []
	for i in range(n):
		h.append((-a[i], i))
	heapq.heapify(h)
	meetings = []
	while True:
		a = heapq.heappop(h)
		b = heapq.heappop(h)
		if b[0] == 0:
			break
		meetings.append((a[1] + 1, b[1] + 1))
		a = (a[0] + 1, a[1])
		b = (b[0] + 1, b[1])
		heapq.heappush(h, a)
		heapq.heappush(h, b)
	print(len(meetings))
	for (i, j) in meetings:
		print(i, j)


from sys import stdin
import heapq
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = [[-a[i], i + 1] for i in range(n) if a[i] != 0]
	heapq.heapify(a)
	ans = []
	while len(a) >= 2:
		biggest = heapq.heappop(a)
		second_biggest = heapq.heappop(a)
		ans.append((biggest[1], second_biggest[1]))
		biggest[0] += 1
		if biggest[0] != 0:
			heapq.heappush(a, biggest)
		second_biggest[0] += 1
		if second_biggest[0] != 0:
			heapq.heappush(a, second_biggest)
	print(len(ans))
	for v in ans:
		print(*v)


import heapq
t = int(input())
for _ in range(t):
	pq = []
	_ = input()
	a = [int(x) for x in input().split()]
	for (i, e) in enumerate(a):
		if not e:
			continue
		heapq.heappush(pq, (-e, i))
	ans = []
	while len(pq) > 1:
		(man1, pos1) = heapq.heappop(pq)
		man1 = -man1
		(man2, pos2) = heapq.heappop(pq)
		man2 = -man2
		men = [pos1 + 1, pos2 + 1]
		men.sort()
		men = tuple(men)
		ans.append('%d %d' % men)
		man1 -= 1
		man2 -= 1
		if man1:
			heapq.heappush(pq, (-man1, pos1))
		if man2:
			heapq.heappush(pq, (-man2, pos2))
	print(len(ans))
	if ans:
		print('\n'.join(ans))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict, Counter, deque
from math import sqrt, log10, log, floor, factorial, gcd
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations
import sys, io, os
input = sys.stdin.readline
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
inf = float('inf')
mod = 10 ** 9 + 7

def yn(a):
	print('YES' if a else 'NO')
ceil = lambda a, b: (a + b - 1) // b

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))
t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	s = SortedList([[l[i], i + 1] for i in range(n) if l[i]])
	out = []
	while len(s) > 1:
		(val1, ind1) = s.pop()
		(val2, ind2) = s.pop()
		out.append([ind1, ind2])
		val1 -= 1
		val2 -= 1
		if val1:
			s.add([val1, ind1])
		if val2:
			s.add([val2, ind2])
	print(len(out))
	for i in out:
		print(*i)


def solution_d(l):
	lidx = [(_, i + 1) for (i, _) in enumerate(l)]
	lidx.sort()
	l.sort()
	if len(l) <= 1:
		return (0, [])
	if len(l) == 2:
		return (l[0], [[1, 2] for _ in range(l[0])])
	i = 0
	cnt = 0
	result = []
	diff = sum(l) - 2 * l[-1]
	while diff > 0:
		while l[i] == 0:
			i += 1
		maxminus = min((diff + 1) // 2, l[i])
		l[i] -= maxminus
		l[i + 1] -= maxminus
		result += [[lidx[i][1], lidx[i + 1][1]] for _ in range(maxminus)]
		cnt += maxminus
		diff -= 2 * maxminus
	while i < len(l) - 1:
		cnt += l[i]
		result += [[lidx[i][1], lidx[len(l) - 1][1]] for _ in range(l[i])]
		i += 1
	return (cnt, result)
t = int(input())
for _ in range(t):
	n = int(input())
	ri = input()
	l = [int(_) for _ in ri.split(' ')]
	(cnt, pairs) = solution_d(l)
	print(cnt)
	for (x, y) in pairs:
		print(x, y)


import heapq
t = int(input())
while t:
	t -= 1
	n = int(input())
	soc = list(map(int, input().split()))
	temp = []
	to_ret = []
	cnt = 0
	for (i, v) in enumerate(soc):
		if v != 0:
			heapq.heappush(temp, [-v, i + 1])
	while len(temp) >= 2:
		cur_a = heapq.heappop(temp)
		cur_b = heapq.heappop(temp)
		(soc_a, idx_a) = (cur_a[0], cur_a[1])
		(soc_b, idx_b) = (cur_b[0], cur_b[1])
		to_ret.append([min(idx_a, idx_b), max(idx_a, idx_b)])
		soc_a += 1
		soc_b += 1
		if soc_a != 0:
			heapq.heappush(temp, [soc_a, idx_a])
		if soc_b != 0:
			heapq.heappush(temp, [soc_b, idx_b])
		cnt += 1
	print(cnt)
	for ret in to_ret:
		print(*ret)


import heapq

def solve():
	n = int(input())
	h = []
	for (i, v) in enumerate(input().split(), 1):
		v = int(v)
		if v == 0:
			continue
		heapq.heappush(h, (-v, i))
	ans = []
	while len(h) > 1:
		(a, i) = heapq.heappop(h)
		(b, j) = heapq.heappop(h)
		ans.append((i, j))
		if a < -1:
			heapq.heappush(h, (a + 1, i))
		if b < -1:
			heapq.heappush(h, (b + 1, j))
	print(len(ans))
	for line in ans:
		print(*line)
for _ in range(int(input())):
	solve()


import sys
from sys import stdin, stdout
from math import ceil, floor, sqrt, log, log2, log10
from collections import Counter
import heapq
int_max = float('inf')
int_min = -float('inf')
ANS = []
input = stdin.readline

def solve():
	pass
t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	summ = sum(arr)
	overhead = -1
	for i in range(n):
		if arr[i] * 2 > summ:
			overhead = i
			break
	if overhead == -1:
		k = summ // 2
		a = []
		b = []
		for i in range(n):
			for j in range(arr[i]):
				if len(a) >= k:
					b.append(i + 1)
				else:
					a.append(i + 1)
	else:
		k = summ - arr[overhead]
		a = []
		b = []
		for i in range(n):
			if i == overhead:
				continue
			else:
				for j in range(arr[i]):
					a.append(i + 1)
					b.append(overhead + 1)
	ANS.append(str(k))
	for i in range(len(a)):
		ANS.append(str(a[i]) + ' ' + str(b[i]))
print('\n'.join(ANS))


from sys import stdin
from heapq import heappush, heappop
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(int, stdin.readline().split()))
	ans = []
	h = []
	for i in range(n):
		if arr[i] > 0:
			heappush(h, [-1 * arr[i], i])
	while h:
		a = heappop(h)
		t = -1 * a[0]
		while h and t > 0:
			b = heappop(h)
			b[0] += 1
			t -= 1
			ans.append([a[1] + 1, b[1] + 1])
			if b[0] < 0:
				heappush(h, b)
		if t > 0:
			break
	print(len(ans))
	for a in ans:
		print(*a)


from heapq import heapify, heappop, heappush
import os, sys
input = lambda : sys.stdin.buffer.readline().rstrip(b'\r\n')

def dbp(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)
a_str = lambda : input().decode('ascii')
get_int_list = lambda : [int(x) for x in input().split()]
join_str = lambda l, ch=' ': ch.join((str(x) for x in l))

def do_thing():
	n = int(input())
	hq = [(-a, idx + 1) for (idx, a) in enumerate(get_int_list())]
	heapify(hq)
	ans = []
	while len(hq) >= 2:
		(s1, p1) = heappop(hq)
		if s1 >= 0:
			continue
		(s2, p2) = heappop(hq)
		if s2 >= 0:
			heappush(hq, (s1, p1))
			continue
		ans.append(join_str((p1, p2)))
		if s1 + 1 < 0:
			heappush(hq, (s1 + 1, p1))
		if s2 + 1 < 0:
			heappush(hq, (s2 + 1, p2))
	if ans:
		return str(len(ans)) + '\n' + join_str(ans, '\n')
	else:
		return 0
maxcc = 1
maxcc = int(input())
for cc in range(maxcc):
	print(do_thing())


from heapq import heappush, heappop

def solve(n, a):
	h = []
	for i in range(n):
		heappush(h, (-1 * a[i], i))
	res = []
	while len(h) >= 2:
		(val1, index1) = heappop(h)
		val1 *= -1
		(val2, index2) = heappop(h)
		val2 *= -1
		if val1 <= 0 or val2 <= 0:
			break
		val1 -= 1
		val2 -= 1
		if val1 > 0:
			heappush(h, (-1 * val1, index1))
		if val2 > 0:
			heappush(h, (-1 * val2, index2))
		res.append(sorted((index1 + 1, index2 + 1)))
	return res
t = int(input().strip())
inputs = []
for _ in range(t):
	n = int(input().strip())
	ls = list(map(int, input().strip().split()))
	inputs.append((n, ls))
for (n, ls) in inputs:
	res = solve(n, ls)
	print(len(res))
	for s in res:
		print(*s)


import heapq
for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	h = []
	for i in range(a):
		heapq.heappush(h, [-b[i], i + 1])
	li = []
	while 1:
		c = heapq.heappop(h)
		d = heapq.heappop(h)
		if d[0] == 0:
			break
		li += [[c[1], d[1]]]
		c[0] += 1
		d[0] += 1
		heapq.heappush(h, c)
		heapq.heappush(h, d)
	print(len(li))
	for j in li:
		print(*sorted(j))


from heapq import heappush, heappop
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	q = []
	x = 1
	ans = []
	for c in A:
		if c != 0:
			heappush(q, (-c, x))
		x += 1
	while len(q) > 1:
		(c, d) = heappop(q)
		(e, f) = heappop(q)
		if c + 1 != 0:
			heappush(q, (c + 1, d))
		if e + 1 != 0:
			heappush(q, (e + 1, f))
		ans.append([d, f])
	print(len(ans))
	for i in ans:
		print(*i)


from heapq import heappush, heappop

def gs():
	return input()

def gi():
	return int(input())

def gl():
	return list(map(int, input().split()))

def gt():
	return map(int, input().split())
t = gi()
for _ in range(t):
	n = gi()
	array = gl()
	heap = []
	for i in range(n):
		if array[i]:
			heappush(heap, (-array[i], i + 1))
	talks = []
	while len(heap) > 1:
		(one, i) = heappop(heap)
		(two, j) = heappop(heap)
		one += 1
		two += 1
		talks.append([i, j])
		if one < 0:
			heappush(heap, (one, i))
		if two < 0:
			heappush(heap, (two, j))
	print(len(talks))
	for talk in talks:
		print(*talk)


import sys
from collections import Counter
from itertools import chain
from itertools import permutations
import math
import heapq
num_cases = int(sys.stdin.readline().strip())
for case in range(1, num_cases + 1):
	N = int(sys.stdin.readline().strip())
	A = list(map(int, sys.stdin.readline().split()))
	new_A = []
	for (index, value) in enumerate(A):
		new_A.append([-value, index + 1])
	heapq.heapify(new_A)
	count = 0
	ans = []
	while 1:
		alpha = heapq.heappop(new_A)
		beta = heapq.heappop(new_A)
		if beta[0] == 0:
			break
		else:
			count += 1
			ans.append([alpha[1], beta[1]])
			heapq.heappush(new_A, [alpha[0] + 1, alpha[1]])
			heapq.heappush(new_A, [beta[0] + 1, beta[1]])
	print(count)
	for answer in ans:
		print(*answer)


import heapq
for _ in range(int(input())):
	n = int(input())
	nums = list(map(int, input().split(' ')))
	times = 0
	max_heap = []
	ans = []
	for i in range(n):
		heapq.heappush(max_heap, (-1 * nums[i], i + 1))
	while len(max_heap) > 1:
		(a, b) = heapq.heappop(max_heap)
		(c, d) = heapq.heappop(max_heap)
		if a == 0 or c == 0:
			break
		a *= -1
		c *= -1
		a -= 1
		c -= 1
		times += 1
		ans.append([d, b])
		heapq.heappush(max_heap, (-1 * a, b))
		heapq.heappush(max_heap, (-1 * c, d))
	print(times)
	for (i, j) in ans:
		print(i, j)


import queue
t = int(input())
for i in range(t):
	ans = []
	n = int(input())
	talk = list(map(int, input().split()))
	q = queue.PriorityQueue()
	for i in range(len(talk)):
		if talk[i] != 0:
			q.put([-1 * talk[i], i])
	while len(q.queue) > 1:
		a = q.get()
		b = q.get()
		a[0] += 1
		b[0] += 1
		ans.append([a[1] + 1, b[1] + 1])
		if a[0] != 0:
			q.put(a)
		if b[0] != 0:
			q.put(b)
	print(len(ans))
	for i in ans:
		print(*i)


import sys
import bisect
from heapq import heappop, heappush
input = sys.stdin.readline
_ = 1
_ = int(input())
while _ > 0:
	_ -= 1
	n = int(input())
	l = [int(x) for x in input().split()]
	l1 = []
	ans = []
	for x in range(n):
		if l[x] > 0:
			heappush(l1, (-l[x], x + 1))
	while len(l1) > 1:
		(ele1, r1) = heappop(l1)
		(ele2, r2) = heappop(l1)
		ans.append((r1, r2))
		ele1 *= -1
		ele1 -= 1
		ele2 *= -1
		ele2 -= 1
		if ele1 > 0:
			heappush(l1, (-ele1, r1))
		if ele2 > 0:
			heappush(l1, (-ele2, r2))
	print(len(ans))
	for x in ans:
		print(f'{x[0]} {x[1]}')


from heapq import heapify, heappop, heappush

class Solution:

	def productiveMeetings(self, n, ais):
		pq = []
		for (i, ai) in enumerate(ais):
			if ai != 0:
				heappush(pq, (-ai, i + 1))
		vals = []
		while len(pq) > 1:
			(ai1, idx1) = heappop(pq)
			(ai2, idx2) = heappop(pq)
			vals.append((idx1, idx2))
			ai1 += 1
			ai2 += 1
			if ai1 != 0:
				heappush(pq, (ai1, idx1))
			if ai2 != 0:
				heappush(pq, (ai2, idx2))
		print(len(vals))
		for ps in vals:
			print(*ps)
sol = Solution()
t = int(input())
for _ in range(t):
	n = int(input())
	ais = list(map(int, input().split()))
	sol.productiveMeetings(n, ais)


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	arr = []
	for i in range(n):
		if ar[i] == 0:
			pass
		else:
			arr.append([-ar[i], i + 1])
	ans = 0
	li = []
	import heapq
	heapq.heapify(arr)
	while len(arr) > 1:
		fst = heapq.heappop(arr)
		snd = heapq.heappop(arr)
		ans += -1
		if fst[0] + 1:
			heapq.heappush(arr, [fst[0] + 1, fst[1]])
		if snd[0] + 1:
			heapq.heappush(arr, [snd[0] + 1, snd[1]])
		li.append(str(snd[1]) + ' ' + str(fst[1]))
	print(-ans)
	for x in li:
		print(x)


import math
import heapq
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
for _ in range(I()):
	n = I()
	l = L()
	ans = []
	for i in range(n):
		l[i] = [-l[i], i]
	heapq.heapify(l)
	while True:
		b = heapq.heappop(l)
		a = heapq.heappop(l)
		if a[0] < 0 and b[0] < 0:
			ans.append([a[1] + 1, b[1] + 1])
		else:
			break
		b[0] += 1
		a[0] += 1
		heapq.heappush(l, a)
		heapq.heappush(l, b)
	print(len(ans))
	for i in ans:
		print(*i)


import sys
from heapq import heappop, heappush, heapify
input = sys.stdin.readline

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	for i in range(n):
		b.append([a[i], i + 1])
	a = sorted(b, reverse=True)
	ans = []
	heap = []
	(first, second) = (a[0], a[1])
	heapify(heap)
	for i in range(2, n):
		a[i][0] = a[i][0] * -1
		heappush(heap, a[i])
	if n == 2:
		for _ in range(a[1][0]):
			ans.append([1, 2])
	else:
		while first[0] > 0 and second[0] > 0:
			x = heappop(heap)
			if second[0] == x[0] * -1:
				talks = 1
			else:
				talks = second[0] + x[0]
			for _ in range(talks):
				ans.append([first[1], second[1]])
			first[0] -= talks
			second[0] -= talks
			first[0] = first[0] * -1
			second[0] = second[0] * -1
			heappush(heap, x)
			heappush(heap, first)
			heappush(heap, second)
			first = heappop(heap)
			second = heappop(heap)
			first[0] = first[0] * -1
			second[0] = second[0] * -1
	print(len(ans))
	for i in ans:
		print(i[0], i[1])
for _t in range(int(input())):
	main()


import sys
input = sys.stdin.readline
import heapq
for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	h = []
	people = 0
	for i in range(n):
		if a[i] > 0:
			people += 1
			heapq.heappush(h, (-a[i], i))
	total = 0
	meetings = []
	while people > 1:
		(x, i) = heapq.heappop(h)
		(y, j) = heapq.heappop(h)
		x += 1
		y += 1
		total += 1
		meetings.append(str(i + 1) + ' ' + str(j + 1))
		if x < 0:
			heapq.heappush(h, (x, i))
		if y < 0:
			heapq.heappush(h, (y, j))
		if x == 0:
			people -= 1
		if y == 0:
			people -= 1
	print(total)
	if total > 0:
		print('\n'.join(meetings))


import heapq
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	heap = [[-ar[i], i] for i in range(n)]
	heapq.heapify(heap)
	ans = 0
	pair = []
	while True:
		x1 = heapq.heappop(heap)
		x2 = heapq.heappop(heap)
		if x1[0] == 0 or x2[0] == 0:
			break
		else:
			ans += 1
			pair.append([x1[1] + 1, x2[1] + 1])
			heapq.heappush(heap, [x1[0] + 1, x1[1]])
			heapq.heappush(heap, [x2[0] + 1, x2[1]])
	print(ans)
	for i in pair:
		print(*i)


import heapq
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = []
	he = []
	for i in range(n):
		if a[i] > 0:
			heapq.heappush(he, (-a[i], i + 1))
	while len(he) > 1:
		(x, i1) = heapq.heappop(he)
		(y, i2) = heapq.heappop(he)
		ans.append([i1, i2])
		x += 1
		y += 1
		if x < 0:
			heapq.heappush(he, (x, i1))
		if y < 0:
			heapq.heappush(he, (y, i2))
	if len(ans) >= 1:
		print(len(ans))
		for i in ans:
			print(*i)
	else:
		print(0)


import heapq as hp
t = int(input())
for i in range(t):
	answers = [[] for i in range(2)]
	n = int(input())
	arr = [int(el) for el in input().split()]
	heap = []
	for i in range(n):
		if arr[i] > 0:
			heap.append((-1 * arr[i], i + 1))
	hp.heapify(heap)
	while len(heap) >= 2:
		person1 = hp.heappop(heap)
		person2 = hp.heappop(heap)
		answers[0].append(person1[1])
		answers[1].append(person2[1])
		if person1[0] <= -2:
			hp.heappush(heap, (person1[0] + 1, person1[1]))
		if person2[0] <= -2:
			hp.heappush(heap, (person2[0] + 1, person2[1]))
	print(len(answers[0]))
	for (i, ele) in enumerate(answers[0]):
		print(answers[1][i], answers[0][i])


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from heapq import heappush, heappop, heapify
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr_ = [[-int(x), i + 1] for (i, x) in enumerate(input().split())]
	arr = []
	for (i, j) in arr_:
		if i != 0:
			heappush(arr, (i, j))
	res = []
	while len(arr) > 1:
		(a, ai) = heappop(arr)
		(b, bi) = heappop(arr)
		a *= -1
		b *= -1
		a -= 1
		b -= 1
		res.append((ai, bi))
		if a:
			heappush(arr, (-a, ai))
		if b:
			heappush(arr, (-b, bi))
	sys.stdout.write(f'{len(res)}\n')
	for (i, j) in res:
		sys.stdout.write(f'{i} {j}\n')


import os
import sys
import math
import collections
import functools
import itertools
import heapq
import bisect
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcde'
MOD = 10 ** 9 + 7
EPS = 1e-06
INF = 10 ** 18

def Ceil(a, b):
	return a // b + int(a % b > 0)

def INT():
	return int(input())

def STR():
	return input()

def INTS():
	return tuple(map(int, input().split()))

def ARRINT():
	return [int(i) for i in input().split()]

def ARRSTR():
	return [i for i in input().split()]

def solve(n, a):
	q = []
	for i in range(n):
		if a[i] != 0:
			heapq.heappush(q, (-a[i], i + 1))
	res = []
	while len(q) > 1:
		(f, f_id) = heapq.heappop(q)
		(s, s_id) = heapq.heappop(q)
		res += [[min(f_id, s_id), max(f_id, s_id)]]
		if f + 1 != 0:
			heapq.heappush(q, (f + 1, f_id))
		if s + 1 != 0:
			heapq.heappush(q, (s + 1, s_id))
	print(len(res))
	for r in res:
		print(*r)
for t in range(INT()):
	n = INT()
	a = ARRINT()
	solve(n, a)


import heapq
for i in range(int(input())):
	a = int(input())
	b = [int(x) for x in input().split()]
	k = []
	for i in range(len(b)):
		k.append([-1 * b[i], i])
	heapq.heapify(k)
	steps = []
	while len(k) >= 2:
		index1 = heapq.heappop(k)
		index2 = heapq.heappop(k)
		if index2[0] <= -1:
			x = min(index2[1] + 1, index1[1] + 1)
			y = max(index2[1] + 1, index1[1] + 1)
			steps.append([x, y])
			index2[0] += 1
			index1[0] += 1
			heapq.heappush(k, index2)
			heapq.heappush(k, index1)
	print(len(steps))
	for j in steps:
		print(j[0], j[1])


import heapq
from queue import PriorityQueue as PQ
n = int(input())
for i in range(n):
	pq = PQ()
	ans = 0
	length = int(input())
	data = list(map(int, input().split()))
	for j in range(length):
		if data[j] != 0:
			pq.put((-data[j], j + 1))
	ans = []
	count = 0
	while pq.qsize() > 1:
		a = pq.get()
		b = pq.get()
		count += 1
		ans.append((a[1], b[1]))
		if b[0] != -1:
			pq.put((b[0] + 1, b[1]))
		if a[0] != -1:
			pq.put((a[0] + 1, a[1]))
	print(count)
	for i in ans:
		print(i[0], i[1])


import sys
import heapq
readline = sys.stdin.readline
from heapq import heappush, heappop, heapify, heappushpop, _heappop_max, _heapify_max

def _heappush_max(heap, item):
	heap.append(item)
	heapq._siftdown_max(heap, 0, len(heap) - 1)

def _heappushpop_max(heap, item):
	if heap and item < heap[0]:
		(item, heap[0]) = (heap[0], item)
		heapq._siftup_max(heap, 0)
	return item
T = int(readline())
for _ in range(T):
	N = int(readline())
	hq = []
	A = list(map(int, readline().split()))
	for i in range(N):
		if A[i]:
			hq.append((A[i], i + 1))
	_heapify_max(hq)
	ans_lst = []
	while len(hq) >= 2:
		(a0, i0) = _heappop_max(hq)
		(a1, i1) = _heappop_max(hq)
		ans_lst.append((i0, i1))
		a0 -= 1
		a1 -= 1
		if a0:
			_heappush_max(hq, (a0, i0))
		if a1:
			_heappush_max(hq, (a1, i1))
	print(len(ans_lst))
	for ans in ans_lst:
		print(*ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter, defaultdict
import math
import heapq
import bisect

def nck(n, k):
	res = 1
	for i in range(1, k + 1):
		res = res * (n - i + 1) // i
	return res
import heapq
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	h = []
	ans = []
	for i in range(n):
		if arr[i] > 0:
			heapq.heappush(h, [-1 * arr[i], i + 1])
	pre_ind = -1
	pre_val = 0
	while h:
		(val, ind) = heapq.heappop(h)
		val *= -1
		if pre_val == 0:
			if h:
				(val1, ind1) = heapq.heappop(h)
				val1 *= -1
				ans.append([ind, ind1])
				val -= 1
				val1 -= 1
				pre_val = val
				pre_ind = ind
				if val1 > 0:
					heapq.heappush(h, [-1 * val1, ind1])
			else:
				break
		else:
			ans.append([pre_ind, ind])
			val -= 1
			pre_val -= 1
			if val > 0:
				heapq.heappush(h, [-1 * val, ind])
	print(len(ans))
	for i in ans:
		print(*i)


from heapq import heappop, heappush
t = int(input())
for _ in range(t):
	n = int(input())
	soc = list(map(int, input().split()))
	heap = []
	for i in range(len(soc)):
		if soc[i] > 0:
			heappush(heap, (-soc[i], i + 1))
	sol = []
	while len(heap) > 1:
		(s1, i1) = heappop(heap)
		(s2, i2) = heappop(heap)
		sol.append([i1, i2])
		s1 += 1
		s2 += 1
		if s1 < 0:
			heappush(heap, (s1, i1))
		if s2 < 0:
			heappush(heap, (s2, i2))
	print(len(sol))
	for (i, j) in sol:
		print(i, j)


import sys
from math import sqrt, ceil, floor, gcd
from collections import Counter
import heapq
input = lambda : sys.stdin.readline()

def int_arr():
	return list(map(int, input().split()))

def str_arr():
	return list(map(str, input().split()))

def get_str():
	return map(str, input().split())

def get_int():
	return map(int, input().split())

def get_flo():
	return map(float, input().split())

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 1000000007

def solve(n, arr):
	out = []
	for i in range(n):
		if arr[i] > 0:
			out.append([-arr[i], i])
	heapq.heapify(out)
	c = 0
	ans = []
	while len(out) > 1:
		x = heapq.heappop(out)
		y = heapq.heappop(out)
		c += 1
		x[0] += 1
		y[0] += 1
		ans.append([x[1] + 1, y[1] + 1])
		if x[0] < 0:
			heapq.heappush(out, x)
		if y[0] < 0:
			heapq.heappush(out, y)
	print(c)
	for i in ans:
		print(*i)
for _ in range(int(input())):
	n = int(input())
	arr = int_arr()
	solve(n, arr)


import sys
import heapq
input = sys.stdin.readline
t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	h = []
	x = []
	for i in range(n):
		if a[i] > 0:
			heapq.heappush(h, (-a[i], i + 1))
	while len(h) > 1:
		(f, fi) = heapq.heappop(h)
		(s, si) = heapq.heappop(h)
		ans += 1
		f += 1
		s += 1
		x.append([fi, si])
		if f < 0:
			heapq.heappush(h, (f, fi))
		if s < 0:
			heapq.heappush(h, (s, si))
	print(ans)
	if ans:
		for i in x:
			print(*i)
	t -= 1


import heapq
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	q = []
	for i in range(n):
		heapq.heappush(q, [-a[i], i + 1])
	res = []
	while True:
		(high, low) = (heapq.heappop(q), heapq.heappop(q))
		if not low[0]:
			break
		res.append([high[1], low[1]])
		high[0] += 1
		low[0] += 1
		heapq.heappush(q, high)
		heapq.heappush(q, low)
	print(len(res))
	for k in res:
		print(*k)


import sys
input = sys.stdin.readline
inp = lambda : int(input().strip())
minp = lambda : list(map(int, input().strip().split()))
out = lambda x, lst='\n': sys.stdout.write(x + lst)

def parent(x):
	return (x - 1) // 2

def left(x):
	return x * 2 + 1

def right(x):
	return x * 2 + 2

class MaxHeap:

	def __init__(self, a) -> None:
		self.n = 0
		self.heap = []
		for (idx, x) in enumerate(a):
			if x:
				self.push((x, idx))

	def cmp(self, x, y):
		return self.heap[x][0] > self.heap[y][0]

	def swap(self, x, y):
		(self.heap[x], self.heap[y]) = (self.heap[y], self.heap[x])

	def top(self):
		return self.heap[0]

	def pop(self):
		assert self.n != 0, 'heap is empty - nothing to be popped'
		self.swap(0, self.n - 1)
		self.n -= 1
		rt = 0
		while left(rt) < self.n:
			L = left(rt)
			R = right(rt)
			mx = rt
			if L < self.n and self.cmp(L, mx):
				mx = L
			if R < self.n and self.cmp(R, mx):
				mx = R
			if mx == rt:
				break
			self.swap(rt, mx)
			(rt, mx) = (mx, rt)

	def push(self, item):
		if self.n < len(self.heap):
			self.heap[self.n] = item
		else:
			self.heap.append(item)
		self.n += 1
		rt = self.n - 1
		while rt:
			if not self.cmp(parent(rt), rt):
				self.swap(parent(rt), rt)
				rt = parent(rt)
			else:
				break

	def size(self):
		return self.n

def solve() -> str:
	(_, a) = (inp(), minp())
	pq = MaxHeap(a)
	res = []
	while pq.size() > 2:
		(x, idx) = pq.top()
		pq.pop()
		(y, idy) = pq.top()
		pq.pop()
		(z, _) = pq.top()
		while y >= z:
			res.append(f'{idx + 1} {idy + 1}')
			x -= 1
			y -= 1
		if x:
			pq.push((x, idx))
		if y:
			pq.push((y, idy))
	if pq.size() == 2:
		(x, idx) = pq.top()
		pq.pop()
		(y, idy) = pq.top()
		pq.pop()
		while y >= 1:
			res.append(f'{idx + 1} {idy + 1}')
			x -= 1
			y -= 1
	return f'{len(res)}\n' + '\n'.join(res)

def main() -> None:
	testCases = 1
	testCases = inp()
	for testCase in range(1, testCases + 1):
		out(solve())
main()


from heapq import heappush, heappop

def mgc(a, n):
	mp = {}
	h = []
	tot = 0
	ans = []
	for i in range(n):
		heappush(h, [-a[i], i + 1])
	while True:
		first_person = heappop(h)
		second_person = heappop(h)
		if second_person[0] == 0:
			break
		first_person[0] += 1
		second_person[0] += 1
		ans.append([first_person[1], second_person[1]])
		heappush(h, first_person)
		heappush(h, second_person)
	print(len(ans))
	for i in ans:
		print(*i)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	mgc(a, n)


from heapq import heappush, heappop, heapify
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	heap = [(-arr[i], i) for i in range(n)]
	heapify(heap)
	ans = []
	while True:
		(a, x1) = heappop(heap)
		(b, x2) = heappop(heap)
		if a == 0 or b == 0:
			break
		ans.append([x1 + 1, x2 + 1])
		heappush(heap, (a + 1, x1))
		heappush(heap, (b + 1, x2))
	print(len(ans))
	for each in ans:
		print(*each)


import itertools
import math
import sys
import queue
import itertools
from heapq import heappop, heappush
import random

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	h = []
	for i in range(n):
		heappush(h, (-a[i], i))
	res = []
	while len(h) >= 2:
		(f, s) = (heappop(h), heappop(h))
		(f, fi, s, si) = (-f[0], f[1], -s[0], s[1])
		if f > 0 and s > 0:
			res.append([fi, si])
			heappush(h, (-f + 1, fi))
			heappush(h, (-s + 1, si))
	sys.stdout.write(str(len(res)) + '\n')
	for i in res:
		sys.stdout.write(str(i[0] + 1) + ' ' + str(i[1] + 1) + '\n')
multi_test = 1
if multi_test:
	t = int(sys.stdin.readline())
	for _ in range(t):
		solve()
else:
	solve()


import heapq
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	heap = []
	ans = []
	x = 1
	for element in arr:
		if element != 0:
			heapq.heappush(heap, (-element, x))
		x += 1
	while len(heap) > 1:
		(first, id1) = heapq.heappop(heap)
		(second, id2) = heapq.heappop(heap)
		if first + 1 != 0:
			heapq.heappush(heap, (first + 1, id1))
		if second + 1 != 0:
			heapq.heappush(heap, (second + 1, id2))
		ans.append((id1, id2))
	print(len(ans))
	for (i, j) in ans:
		print(i, j)


import io, os
import heapq
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = []
	heap = []
	for i in range(n):
		heapq.heappush(heap, [-arr[i], i + 1])
	while len(heap) > 1:
		p1 = heapq.heappop(heap)
		p2 = heapq.heappop(heap)
		if p1[0] == 0 or p2[0] == 0:
			break
		p1[0] += 1
		p2[0] += 1
		ans.append([p1[1], p2[1]])
		if p1[0] < 0:
			heapq.heappush(heap, p1)
		if p2[0] < 0:
			heapq.heappush(heap, p2)
	print(len(ans))
	for ele in ans:
		print(min(ele), max(ele))
T = int(input())
t = 1
while t <= T:
	main(t)
	t += 1


from heapq import heappop, heappush
import sys
input = sys.stdin.readline
inp = sys.stdin.readline

def input():
	return inp().strip()

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def solve():
	n = ii()
	arr = li()
	queue = []
	for i in range(1, n + 1):
		if arr[i - 1] != 0:
			heappush(queue, (-1 * arr[i - 1], i))
	ans = []
	count = 0
	while queue:
		if len(queue) > 1:
			(fp, f) = heappop(queue)
			(sp, s) = heappop(queue)
			count += 1
			ans.append((f, s))
			if fp + 1 != 0:
				heappush(queue, (fp + 1, f))
			if sp + 1 != 0:
				heappush(queue, (sp + 1, s))
		else:
			break
	print(count)
	for (x, y) in ans:
		print(x, y)

def main():
	t = ii()
	for _ in range(t):
		solve()
main()


from collections import defaultdict, deque, Counter
from functools import lru_cache
from heapq import heappush, heappop
from bisect import bisect_right, bisect_left

def inpNum():
	return int(input())

def inpStr():
	return input()

def inpSepNum():
	return map(int, input().split())

def inpNumList():
	return list(map(int, input().split()))

def inpChList():
	return list(input().split())

def finder(n, nums):
	maxH = []
	ans = 0
	res = []
	for i in range(n):
		if nums[i] != 0:
			heappush(maxH, (-nums[i], i + 1))
	while len(maxH) > 1:
		mx1 = heappop(maxH)
		mx2 = heappop(maxH)
		newmx1 = mx1[0] + 1
		newmx2 = mx2[0] + 1
		ans += 1
		if newmx2:
			heappush(maxH, (newmx2, mx2[1]))
		if newmx1:
			heappush(maxH, (newmx1, mx1[1]))
		res.append([mx2[1], mx1[1]])
	return (ans, res)
tc = inpNum()
for _ in range(tc):
	n = inpNum()
	nums = inpNumList()
	(answer, result) = finder(n, nums)
	if not answer:
		print(0)
	else:
		print(answer)
		for r in sorted(result):
			print(*r)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
for _ in range(inp()):
	n = inp()
	a = inpl()
	q = []
	for (i, x) in enumerate(a):
		if x == 0:
			continue
		heappush(q, (-x, i))
	res = []
	while q:
		(ax, ai) = heappop(q)
		if not q:
			break
		(bx, bi) = heappop(q)
		res.append((ai + 1, bi + 1))
		ax += 1
		bx += 1
		if ax:
			heappush(q, (ax, ai))
		if bx:
			heappush(q, (bx, bi))
	print(len(res))
	for x in res:
		print(*x)


import heapq

class pers:

	def __init__(self, s, i):
		self.s = s
		self.i = i

	def __lt__(self, other):
		return self.s > other.s

	def __repr__(self):
		return '(%d %d)' % (self.s, self.i)
t = int(input())
for case in range(t):
	n = int(input())
	a = [int(x) for x in input().split(' ')]
	soc = [pers(a[i], i + 1) for i in range(n)]
	soc = [x for x in soc if x.s > 0]
	heapq.heapify(soc)
	pairs = []
	while len(soc) > 1:
		first = heapq.heappop(soc)
		second = heapq.heappop(soc)
		pairs.append((first.i, second.i))
		first.s -= 1
		second.s -= 1
		if second.s > 0:
			heapq.heappush(soc, second)
		if first.s > 0:
			heapq.heappush(soc, first)
	print(len(pairs))
	for p in pairs:
		print('%d %d' % p)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict, deque
from math import ceil, floor, sqrt, log2, gcd, pi
from heapq import heappush, heappop
from bisect import bisect_left, bisect
from functools import reduce
import sys
abc = 'abcdefghijklmnopqrstuvwxyz'
ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	heap = []
	cnt = 0
	ans = []
	tot = 0
	for i in range(n):
		if a[i] > 0:
			heappush(heap, (-a[i], i + 1))
			tot += 1
	while tot > 1:
		(a1, id1) = heappop(heap)
		(a2, id2) = heappop(heap)
		a1 = -a1
		a2 = -a2
		mini = min(a1, a2)
		if mini == 0:
			break
		cnt += 1
		ans += [[id1, id2]] * 1
		a1 -= 1
		a2 -= 1
		tot -= 2
		if a1 > 0:
			heappush(heap, (-a1, id1))
			tot += 1
		if a2 > 0:
			heappush(heap, (-a2, id2))
			tot += 1
	print(cnt)
	for i in ans:
		print(*i)


import sys
N = int(200000.0 + 5)
sys.setrecursionlimit(N)

def charming():
	n = int(input())
	a = list(map(int, input().split()))
	a = list(zip(a, range(1, n + 1)))
	a.sort(key=lambda x: x[0], reverse=True)
	res = list()
	id = 0
	for i in range(1, n):
		while a[i][0] > 0 and a[id][0] > 0:
			res += [[a[i][1], a[id][1]]]
			a[id] = (a[id][0] - 1, a[id][1])
			a[i] = (a[i][0] - 1, a[i][1])
		if a[id][0] <= 0:
			id = i
	if a[id][0] > 0:
		siz = len(res)
		for i in range(siz):
			if a[id][0] <= 0:
				break
			if res[i][0] != a[id][1] and res[i][1] != a[id][1]:
				a[id] = (a[id][0] - 1, a[id][1])
				tmp = res[i][0]
				res[i][0] = a[id][1]
				if a[id][0] > 0:
					res.append([tmp, a[id][1]])
					a[id] = (a[id][0] - 1, a[id][1])
	print(len(res))
	for (i, j) in res:
		print(i, j)
for t in range(int(input())):
	charming()


import heapq
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	q = []
	for i in range(n):
		q.append((-a[i], i + 1))
	heapq.heapify(q)
	ans = 0
	l = []
	while True:
		(x1, y1) = heapq.heappop(q)
		(x2, y2) = heapq.heappop(q)
		x1 = -x1
		x2 = -x2
		if x1 == 0 or x2 == 0:
			break
		ans += 1
		l.append([y1, y2])
		heapq.heappush(q, (-max(x1 - 1, 0), y1))
		heapq.heappush(q, (-max(x2 - 1, 0), y2))
	print(ans)
	for (x, y) in l:
		print(x, y)


import sys
import getpass
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict, deque
input = sys.stdin.readline
M9 = 10 ** 9 + 7
(yes, no) = ('YES', 'NO')
MAXINT = sys.maxsize
OFFLINE_TEST = getpass.getuser() == 'hkmac'

def log(*args):
	if OFFLINE_TEST:
		print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)

def solve(*args):
	if OFFLINE_TEST:
		log('----- solving ------')
		log(*args)
		log('----- ------- ------')
	return solve_(*args)

def read_matrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def read_strings(rows):
	return [input().strip() for _ in range(rows)]

def minus_one(arr):
	return [x - 1 for x in arr]

def minus_one_matrix(mrr):
	return [[x - 1 for x in row] for row in mrr]

def solve_(arr):
	arr = [(-x, i) for (i, x) in enumerate(arr) if x > 0]
	arr.sort()
	res = []
	while len(arr) > 1:
		(x, i) = heapq.heappop(arr)
		(y, j) = heapq.heappop(arr)
		x += 1
		y += 1
		if x < 0:
			heapq.heappush(arr, (x, i))
		if y < 0:
			heapq.heappush(arr, (y, j))
		res.append(f'{i + 1} {j + 1}')
	return res
for case_num in range(int(input())):
	k = int(input())
	arr = list(map(int, input().split()))
	res = solve(arr)
	print(len(res))
	res = '\n'.join((str(x) for x in res))
	print(res)


from heapq import heappush, heappop
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	heap = []
	for i in range(n):
		if arr[i] > 0:
			heappush(heap, (-arr[i], i + 1))
	ans = []
	while len(heap) >= 2:
		(s1, p1) = heappop(heap)
		(s2, p2) = heappop(heap)
		ans.append([p1, p2])
		s1 += 1
		s2 += 1
		if s1 < 0:
			heappush(heap, (s1, p1))
		if s2 < 0:
			heappush(heap, (s2, p2))
	sys.stdout.write(str(len(ans)) + '\n')
	for p in ans:
		sys.stdout.write(str(p[0]) + ' ' + str(p[1]) + '\n')


import collections
import os
import sys
from io import BytesIO, IOBase
from bisect import bisect_left
from math import gcd, log, ceil
from collections import Counter
from pprint import pprint
import heapq
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve():
	n = input()
	arr = list(map(int, input().split()))
	hq = []
	for (i, ele) in enumerate(arr):
		heapq.heappush(hq, (-ele, i))
	steps = []
	j = 0
	while len(hq) >= 2 and j < 10:
		(a, ai) = heapq.heappop(hq)
		(b, bi) = heapq.heappop(hq)
		if b == 0:
			break
		if a + 1 != 0:
			heapq.heappush(hq, (a + 1, ai))
		if b + 1 != 0:
			heapq.heappush(hq, (b + 1, bi))
		steps.append((ai + 1, bi + 1))
	print(len(steps))
	for (i, j) in steps:
		print(i, j)
for _ in range(int(input())):
	solve()


import os, sys
from io import BytesIO, IOBase
from heapq import *

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda dtype: [dtype(x) for x in input().split()]
inp_2d = lambda dtype, n: [dtype(input()) for _ in range(n)]
inp_2ds = lambda dtype, n: [inp(dtype) for _ in range(n)]
inp_enu = lambda dtype: [(-x, i + 1) for (i, x) in enumerate(inp(dtype))]
inp_enus = lambda dtype, n: [[i] + inp(dtype) for i in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
for _ in range(int(input())):
	(n, a, ans) = (int(input()), inp_enu(int), [])
	heapify(a)
	while len(a) > 1:
		(mi1, ix1) = heappop(a)
		(mi2, ix2) = heappop(a)
		if 0 in (mi1, mi2):
			if mi1:
				heappush(a, (mi1, ix1))
			elif mi2:
				heappush(a, (mi2, ix2))
		else:
			ans.append(f'{ix1} {ix2}')
			heappush(a, (mi1 + 1, ix1))
			heappush(a, (mi2 + 1, ix2))
	print(len(ans))
	print(*ans, sep='\n')


from sys import stdin
import bisect

def read_input():
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	return (n, a)

def reverse_insort(a, x, lo=0, hi=None):
	if lo < 0:
		raise ValueError('lo must be non-negative')
	if hi is None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if x > a[mid]:
			hi = mid
		else:
			lo = mid + 1
	return lo - 1

def solve(n, a):
	answer = []
	if n == 2:
		for _ in range(min(a)):
			answer.append((1, 2))
		return answer
	a = sorted([[ai, i] for (i, ai) in enumerate(a, 1)], reverse=True)
	while a[1][0] != 0:
		j = reverse_insort(a, [a[1][0], 0], lo=1)
		i = reverse_insort(a, [a[0][0], 0], hi=j)
		answer.append((a[i][1], a[j][1]))
		a[i][0] -= 1
		a[j][0] -= 1
	return answer

def main():
	t = int(stdin.readline())
	for _ in range(t):
		(n, a) = read_input()
		answer = solve(n, a)
		print(len(answer))
		for (i, j) in answer:
			print(i, j)
main()


import collections
from math import ceil, gcd, sqrt, log
from heapq import *
INF = float('inf')
mod = 1000000007

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	maxHeap = []
	for i in range(n):
		if A[i] > 0:
			k = [-A[i], i]
			heappush(maxHeap, k)
	count = 0
	ans = []
	while len(maxHeap) > 1:
		x = heappop(maxHeap)
		y = heappop(maxHeap)
		net = [x[1] + 1, y[1] + 1]
		ans.append(net)
		count += 1
		k1 = -x[0]
		k2 = -y[0]
		if k1 - 1 > 0:
			net = [-(k1 - 1), x[1]]
			heappush(maxHeap, net)
		if k2 - 1 > 0:
			net = [-(k2 - 1), y[1]]
			heappush(maxHeap, net)
	print(count)
	for i in ans:
		print(*i)
t = int(input())
while t != 0:
	solve()
	t -= 1


import heapq
numCases = int(input())
for _ in range(numCases):
	n = int(input())
	invertedSocial = []
	temp = [int(x) for x in input().split()]
	for ind in range(len(temp)):
		if temp[ind] != 0:
			heapq.heappush(invertedSocial, (-temp[ind], ind + 1))
	numMeetings = 0
	meetings = []
	heapq.heapify(invertedSocial)
	while True:
		if len(invertedSocial) < 2:
			break
		(v1, p1) = heapq.heappop(invertedSocial)
		(v2, p2) = heapq.heappop(invertedSocial)
		v1 = -v1
		v2 = -v2
		meetings.append(str(p1) + ' ' + str(p2))
		numMeetings += 1
		if v1 - 1 > 0:
			heapq.heappush(invertedSocial, (-v1 + 1, p1))
		if v2 - 1 > 0:
			heapq.heappush(invertedSocial, (-v2 + 1, p2))
	print(numMeetings)
	for each in meetings:
		print(each)


import heapq
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	t = []
	for i in range(n):
		t.append([-l[i], i])
	t.sort(reverse=True)
	heapq.heapify(t)
	ans = 0
	out = []
	while len(t) > 1:
		a = heapq.heappop(t)
		b = heapq.heappop(t)
		a[0] *= -1
		b[0] *= -1
		if a[0] > 0 and b[0] > 0:
			a[0] -= 1
			b[0] -= 1
			if a[0]:
				a[0] *= -1
				heapq.heappush(t, a)
			if b[0] != 0:
				b[0] *= -1
				heapq.heappush(t, b)
			out.append((a[1] + 1, b[1] + 1))
			ans += 1
	print(ans)
	for i in out:
		print(*i)


from heapq import heapify, heappush, heappop, heappushpop
from collections import defaultdict as dd, deque as dq, Counter as C
from math import factorial as f, ceil, gcd, sqrt, log
from bisect import bisect_left as bl, bisect_right as br
from itertools import combinations as c, permutations as p
from math import factorial as f, ceil, gcd, sqrt, log
from sys import stdin
input = stdin.readline
mp = lambda : map(int, input().split())
it = lambda : int(input())
ls = lambda : list(input().strip().split())
mt = lambda r: [ls() for _ in range(r)]
lcm = lambda a, b: a * b // gcd(a, b)

def fibo_n(n):
	return ((1 + sqrt(5)) / 2) ** n / sqrt(5)
for _ in range(it()):
	a = it()
	b = list(mp())
	for i in range(a):
		b[i] *= -1
	k = list(zip(b, range(1, a + 1)))
	heapify(k)
	ans = []
	while True:
		(fst, pos1) = heappop(k)
		(scd, pos2) = heappop(k)
		if fst != 0 and scd != 0:
			fst += 1
			scd += 1
			ans.append([pos1, pos2])
			heappush(k, (fst, pos1))
			heappush(k, (scd, pos2))
		else:
			break
	print(len(ans))
	for i in ans:
		print(*i)


import heapq

class pers:

	def __init__(self, s, i):
		self.s = s
		self.i = i

	def __lt__(self, next):
		return self.s > next.s

	def __repr__(self):
		return '(%d, %d)' % (self.s, self.i)
T = int(input())
for x in range(T):
	n = int(input())
	s = [int(x) for x in input().split(' ')]
	ss = [pers(s[i], i + 1) for i in range(n)]
	ss = [x for x in ss if x.s > 0]
	heapq.heapify(ss)
	pairs = []
	while len(ss) > 1:
		first = heapq.heappop(ss)
		second = heapq.heappop(ss)
		pairs.append((first.i, second.i))
		first.s -= 1
		second.s -= 1
		if first.s > 0:
			heapq.heappush(ss, first)
		if second.s > 0:
			heapq.heappush(ss, second)
	print(len(pairs))
	for p in pairs:
		print('%d %d' % p)


import os
import sys
from io import BytesIO, IOBase
_str = str
str = lambda x=b'': x if type(x) is bytes else _str(x).encode()
BUFSIZE = 8192

def istick(l, i, j, k, n, m):
	(lx, rx) = (j, j)
	t = 0
	for y in range(i, -1, -1):
		if lx >= 0 and rx < m and (l[y][lx] == l[y][rx] == '*'):
			t += 1
			lx -= 1
			rx += 1
		else:
			break
	if t > k:
		return t
	return False

def marktick(d, i, j, x, n, m):
	l = d
	(lx, rx) = (j, j)
	t = 0
	for y in range(i, i - x, -1):
		if lx >= 0 and rx < m:
			l[y][lx] = '*'
			l[y][rx] = '*'
			lx -= 1
			rx += 1

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		from heapq import heapify, heappush, heappop
		ans1 = []
		for i in range(n):
			if a[i] != 0:
				ans1.append([-a[i], i])
		a = ans1
		heapify(a)
		ans = []
		while len(a) > 1:
			x = heappop(a)
			y = heappop(a)
			ans.append([x[1] + 1, y[1] + 1])
			x[0] += 1
			y[0] += 1
			if x[0] != 0:
				heappush(a, x)
			if y[0] != 0:
				heappush(a, y)
		print(len(ans))
		for i in ans:
			print(*i)
main()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')


from __future__ import division, print_function
import heapq
import os
import copy
import sys
from io import BytesIO, IOBase
from typing import Counter, DefaultDict, Deque
import bisect
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def main():
	t = int(input())
	while t:
		n = int(input())
		a = list(map(int, input().split()))
		b = []
		ans = []
		out = 0
		for i in range(n):
			if a[i] > 0:
				heapq.heappush(b, (-a[i], i))
		while len(b) >= 2:
			(x, xi) = heapq.heappop(b)
			(y, yi) = heapq.heappop(b)
			(x, y) = (-x, -y)
			x -= 1
			y -= 1
			out += 1
			ans.append((xi + 1, yi + 1))
			if x > 0:
				heapq.heappush(b, (-x, xi))
			if y > 0:
				heapq.heappush(b, (-y, yi))
		print(out)
		for i in ans:
			print(*i)
		t -= 1
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from heapq import heappop, heappush
for t in range(int(input())):
	n = int(input())
	soc = tuple(map(lambda x: -int(x), input().rstrip().split()))
	arr = list(map(list, zip(soc, range(1, n + 1))))
	ops = []
	arr.sort()
	while arr and arr[-1][0] == 0:
		arr.pop()
	while len(arr) > 1:
		(a, b) = (heappop(arr), heappop(arr))
		ops.append(sorted([a[1], b[1]]))
		a[0] += 1
		if a[0]:
			heappush(arr, a)
		b[0] += 1
		if b[0]:
			heappush(arr, b)
	print(len(ops))
	for tup in ops:
		print(tup[0], tup[1])


from math import *
from collections import *
from functools import *
from bisect import *
from itertools import *
from heapq import *
import sys
inf = float('inf')
ninf = -float('inf')
ip = input
alphal = 'abcdefghijklmnopqrstuvwxyz'
alphau = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
mod = 10 ** 9 + 7

def ipl():
	return list(map(int, ip().split()))

def ipn():
	return int(ip())

def ipf():
	return float(ip())

def solve():
	n = ipn()
	a = ipl()
	if n == 1:
		print(0)
		return
	heap = []
	for i in range(n):
		if a[i] != 0:
			heappush(heap, (-a[i], i + 1))
		a[i] = [a[i], i + 1]
	r = []
	c = set()
	while len(heap) > 1:
		(e1, i) = heappop(heap)
		(e2, j) = heappop(heap)
		e1 += 1
		e2 += 1
		r.append((i, j))
		if e1 < 0:
			heappush(heap, (e1, i))
		if e2 < 0:
			heappush(heap, (e2, j))
		i += 1
	print(len(r))
	for (i, j) in r:
		print(i, j)
t = ipn()
for _ in range(t):
	solve()


from heapq import heapify, heappop, heappush
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	heap = [(-el, i + 1) for (i, el) in enumerate(arr)]
	heapify(heap)
	res = []
	while len(heap) > 1:
		(start, ind) = heappop(heap)
		(end, pos) = heappop(heap)
		if end == 0:
			heappush(heap, (start, ind))
		else:
			res.append([ind, pos])
			start += 1
			end += 1
			heappush(heap, (start, ind))
			heappush(heap, (end, pos))
	print(len(res))
	for r in res:
		print(*r)


import heapq
for _ in range(int(input())):
	n = int(input())
	li = []
	j = 1
	for i in input().split():
		heapq.heappush(li, [-int(i), j])
		j += 1
	res = []
	while len(li) >= 2:
		x = heapq.heappop(li)
		y = heapq.heappop(li)
		if y[0] == 0:
			heapq.heappush(li, x)
		else:
			res.append([x[1], y[1]])
			x[0] += 1
			y[0] += 1
			heapq.heappush(li, x)
			heapq.heappush(li, y)
	print(len(res))
	for r in res:
		print(*r)


from heapq import heappop, heappush

def cf_744D(numbers: list):
	heap = []
	for (index, num) in enumerate(numbers, start=1):
		if num:
			heappush(heap, (-num, index))
	answer = []
	while len(heap) > 1:
		(a, x) = heappop(heap)
		(b, y) = heappop(heap)
		answer.append((x, y))
		if a + 1:
			heappush(heap, (a + 1, x))
		if b + 1:
			heappush(heap, (b + 1, y))
	return answer
for _ in range(int(input())):
	input()
	answer = cf_744D(list(map(int, input().split())))
	print(len(answer))
	for a in answer:
		print(*a)


from collections import defaultdict, Counter, deque
from math import sqrt, log10, log, floor, factorial, gcd
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations
import sys, io, os
input = sys.stdin.readline
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
inf = float('inf')
mod = 10 ** 9 + 7

def yn(a):
	print('YES' if a else 'NO')
ceil = lambda a, b: (a + b - 1) // b

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))
t = int(input())
for i in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	s = SortedList([[l[i], i + 1] for i in range(n) if l[i]])
	out = []
	while len(s) > 1:
		(val1, ind1) = s.pop()
		(val2, ind2) = s.pop()
		out.append([ind1, ind2])
		val1 -= 1
		val2 -= 1
		if val1:
			s.add([val1, ind1])
		if val2:
			s.add([val2, ind2])
	print(len(out))
	for i in out:
		print(*i)


from sys import *
input = stdin.readline
import threading
from bisect import *
import math
from collections import *
import operator
from heapq import *
from itertools import *
inf = 10 ** 19
mod = 10 ** 9 + 9
MOD = 998244353
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
drow = [0, 1, 0, -1]
dcol = [-1, 0, 1, 0]
setrecursionlimit(200005)

def asc(c):
	return ord(c) - 97 + 1
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = (-1 * a[i], i + 1)
	heapify(a)
	ans = []
	while len(a) > 1:
		temp1 = heappop(a)
		temp2 = heappop(a)
		if temp1[0] == 0:
			continue
		elif temp2[0] == 0:
			heappush(a, temp1)
		else:
			ans.append((temp1[1], temp2[1]))
			heappush(a, ((abs(temp1[0]) - 1) * -1, temp1[1]))
			heappush(a, ((abs(temp2[0]) - 1) * -1, temp2[1]))
	print(len(ans))
	for i in ans:
		print(*i)


def bisect_right(nums, val):
	(lo, hi) = (0, len(nums) - 1)
	while lo < hi:
		mid = lo + (hi - lo) // 2
		if nums[mid][0] > val:
			hi = mid
		else:
			lo = mid + 1
	return lo
cases = int(input())
for _ in range(cases):
	n = int(input())
	nums = [[int(x), i + 1] for (i, x) in enumerate(input().split())]
	nums.sort()
	left = 0
	right = n - 1
	pairs = []
	while left < right:
		if left < right and nums[left][0] == 0:
			left += 1
		else:
			nums[left][0] -= 1
			nums[right][0] -= 1
			pairs.append([nums[left][1], nums[right][1]])
			if nums[right][0] < nums[right - 1][0]:
				index = bisect_right(nums, nums[right][0])
				if nums[index][0] == nums[right][0]:
					(nums[right], nums[index + 1]) = (nums[index + 1], nums[right])
				else:
					(nums[right], nums[index]) = (nums[index], nums[right])
	print(len(pairs))
	for (i, j) in pairs:
		print(i, j)


import heapq
from sys import stdin
t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	a = [[-a[i], i + 1] for i in range(n) if a[i] != 0]
	meetings = []
	heapq.heapify(a)
	while len(a) >= 2:
		(largest, largestidx) = heapq.heappop(a)
		(second, secondidx) = heapq.heappop(a)
		meetings.append((largestidx, secondidx))
		largest += 1
		second += 1
		if largest != 0:
			heapq.heappush(a, [largest, largestidx])
		if second != 0:
			heapq.heappush(a, [second, secondidx])
	print(len(meetings))
	for (i, j) in meetings:
		print(i, j)


import heapq
t = int(input())
for _ in range(t):
	n = int(input())
	peeps = list(map(int, input().split()))
	ans = []
	a = [(-i, index) for (index, i) in enumerate(peeps, start=1) if i]
	heapq.heapify(a)
	while len(a) > 1:
		(p1, i1) = heapq.heappop(a)
		(p2, i2) = heapq.heappop(a)
		p1 += 1
		p2 += 1
		ans.append([i1, i2])
		if p1:
			heapq.heappush(a, (p1, i1))
		if p2:
			heapq.heappush(a, (p2, i2))
	print(len(ans))
	for i in ans:
		print(*i)