Dual (Hard Version)
Popskyy & tiasu - Dual⠀The only difference between the two versions of this problem is the constraint on the maximum number of operations. You can make hacks only if all versions of the problem are solved.You are given an array $$$a_1, a_2,\dots, a_n$$$ of integers (positive, negative or $$$0$$$). You can perform multiple operations on the array (possibly $$$0$$$ operations).In one operation, you choose $$$i, j$$$ ($$$1 \leq i, j \leq n$$$, they can be equal) and set $$$a_i := a_i + a_j$$$ (i.e., add $$$a_j$$$ to $$$a_i$$$).Make the array non-decreasing (i.e., $$$a_i \leq a_{i+1}$$$ for $$$1 \leq i \leq n-1$$$) in at most $$$31$$$ operations. You do not need to minimize the number of operations.
In the first test case, by adding $$$a_1 = 2$$$ to $$$a_2$$$, we get the array $$$[2, 3]$$$ which is non-decreasing.In the second test case, the array changes as:   $$$[1, 2, -10, 3]$$$  $$$[1, 2, -10, 6]$$$  $$$[1, 2, -10, 12]$$$  $$$[1, 2, 2, 12]$$$ In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.The first line contains a single integer $$$n$$$ ($$$1 \le n \le 20$$$) — the length of the array.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-20 \le a_i \le 20$$$) — the array before performing the operations.
For each test case, output your operations in the following format.The first line should contain an integer $$$k$$$ ($$$0 \le k \le 31$$$) — the number of operations.The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$) — the corresponding operation consists in adding $$$a_j$$$ to $$$a_i$$$.After all the operations, the array $$$a_1, a_2,\dots, a_n$$$ must be non-decreasing.
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    b = []
    m = max(a)
    x = min(a)
    dem = 0
    d = 0
    e = 0
    posd = []
    pose = []
    k = a.index(m)
    c = a.index(x)
    if m == x:
        print('0')
    elif m == 0:
        for i in range(n - 2, -1, -1):
            a[i] = a[i] + a[i + 1]
            dem += 1
            b.append(i + 1)
            b.append(i + 2)
    elif x == 0:
        for i in range(1, n):
            a[i] = a[i] + a[i - 1]
            dem += 1
            b.append(i + 1)
            b.append(i)
    else:
        for i in range(n):
            if a[i] >= 0:
                d += 1
                posd.append(i)
            else:
                e += 1
                pose.append(i)
        if d >= e:
            if d == 20:
                for i in range(1, n):
                    a[i] = a[i] + a[i - 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i)
            elif 13 <= d < 20:
                while True:
                    m = m * 2
                    dem += 1
                    b.append(k + 1)
                    b.append(k + 1)
                    if m >= 20:
                        a[c] = m
                        break
                for i in pose:
                    a[i] = a[i] + m
                    dem += 1
                    b.append(i + 1)
                    b.append(k + 1)
                for i in range(1, n):
                    a[i] = a[i] + a[i - 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i)
            elif abs(m) >= abs(x):
                for i in pose:
                    a[i] = a[i] + m
                    dem += 1
                    b.append(i + 1)
                    b.append(k + 1)
                for i in range(1, n):
                    a[i] = a[i] + a[i - 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i)
            else:
                for i in posd:
                    a[i] = a[i] + x
                    dem += 1
                    b.append(i + 1)
                    b.append(c + 1)
                for i in range(n - 2, -1, -1):
                    a[i] = a[i] + a[i + 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i + 2)
        elif e == 20:
            for i in range(n - 2, -1, -1):
                a[i] = a[i] + a[i + 1]
                dem += 1
                b.append(i + 1)
                b.append(i + 2)
        elif 13 <= e < 20:
            while True:
                x = x * 2
                dem += 1
                b.append(c + 1)
                b.append(c + 1)
                if x <= -20:
                    a[c] = x
                    break
            for i in posd:
                a[i] = a[i] + x
                dem += 1
                b.append(i + 1)
                b.append(c + 1)
            for i in range(n - 2, -1, -1):
                a[i] = a[i] + a[i + 1]
                dem += 1
                b.append(i + 1)
                b.append(i + 2)
        elif abs(x) >= abs(m):
            for i in posd:
                a[i] = a[i] + x
                dem += 1
                b.append(i + 1)
                b.append(c + 1)
            for i in range(n - 2, -1, -1):
                a[i] = a[i] + a[i + 1]
                dem += 1
                b.append(i + 1)
                b.append(i + 2)
        else:
            for i in pose:
                a[i] = a[i] + m
                dem += 1
                b.append(i + 1)
                b.append(k + 1)
            for i in range(1, n):
                a[i] = a[i] + a[i - 1]
                dem += 1
                b.append(i + 1)
                b.append(i)
    if dem > 0:
        print(dem)
        while len(b) > 0:
            print(b[0], end=' ')
            print(b[1])
            b.pop(0)
            b.pop(0)
t = int(input())
while t > 0:
    t -= 1
    solve()
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    b = []
    m = max(a)
    x = min(a)
    dem = 0
    d = 0
    e = 0
    posd = []
    pose = []
    k = a.index(m)
    c = a.index(x)
    if m == x:
        print('0')
    elif m == 0:
        for i in range(n - 2, -1, -1):
            a[i] = a[i] + a[i + 1]
            dem += 1
            b.append(i + 1)
            b.append(i + 2)
    elif x == 0:
        for i in range(1, n):
            a[i] = a[i] + a[i - 1]
            dem += 1
            b.append(i + 1)
            b.append(i)
    else:
        for i in range(n):
            if a[i] >= 0:
                d += 1
                posd.append(i)
            else:
                e += 1
                pose.append(i)
        if d >= e:
            if d == 20:
                for i in range(1, n):
                    a[i] = a[i] + a[i - 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i)
            elif 13 <= d < 20:
                while True:
                    m = m * 2
                    dem += 1
                    b.append(k + 1)
                    b.append(k + 1)
                    if m >= 20:
                        a[c] = m
                        break
                for i in pose:
                    a[i] = a[i] + m
                    dem += 1
                    b.append(i + 1)
                    b.append(k + 1)
                for i in range(1, n):
                    a[i] = a[i] + a[i - 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i)
            elif abs(m) >= abs(x):
                for i in pose:
                    a[i] = a[i] + m
                    dem += 1
                    b.append(i + 1)
                    b.append(k + 1)
                for i in range(1, n):
                    a[i] = a[i] + a[i - 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i)
            else:
                for i in posd:
                    a[i] = a[i] + x
                    dem += 1
                    b.append(i + 1)
                    b.append(c + 1)
                for i in range(n - 2, -1, -1):
                    a[i] = a[i] + a[i + 1]
                    dem += 1
                    b.append(i + 1)
                    b.append(i + 2)
        elif e == 20:
            for i in range(n - 2, -1, -1):
                a[i] = a[i] + a[i + 1]
                dem += 1
                b.append(i + 1)
                b.append(i + 2)
        elif 13 <= e < 20:
            while True:
                x = x * 2
                dem += 1
                b.append(c + 1)
                b.append(c + 1)
                if x <= -20:
                    a[c] = x
                    break
            for i in posd:
                a[i] = a[i] + x
                dem += 1
                b.append(i + 1)
                b.append(c + 1)
            for i in range(n - 2, -1, -1):
                a[i] = a[i] + a[i + 1]
                dem += 1
                b.append(i + 1)
                b.append(i + 2)
        elif abs(x) >= abs(m):
            for i in posd:
                a[i] = a[i] + x
                dem += 1
                b.append(i + 1)
                b.append(c + 1)
            for i in range(n - 2, -1, -1):
                a[i] = a[i] + a[i + 1]
                dem += 1
                b.append(i + 1)
                b.append(i + 2)
        else:
            for i in pose:
                a[i] = a[i] + m
                dem += 1
                b.append(i + 1)
                b.append(k + 1)
            for i in range(1, n):
                a[i] = a[i] + a[i - 1]
                dem += 1
                b.append(i + 1)
                b.append(i)
    if dem > 0:
        print(dem)
        while len(b) > 0:
            print(b[0], end=' ')
            print(b[1])
            b.pop(0)
            b.pop(0)
t = int(input())
while t > 0:
    t -= 1
    solve()
for s in [*open(0)][2::2]:
    (*a,) = map(int, s.split())
    n = len(a)
    r = []
    s = 1
    for f in (min, max):
        if (m := f(a)) * (s := (-s)) > 0:
            i = a.index(m) + 1
            j = 0
            z = []
            for x in a:
                while (x + m) * s < 0:
                    m *= 2
                    z += (i, i)
                z += ((j := (j + 1)), i) * (x * s < 0)
            p = 2 * (n - 1) * [0]
            p[::2] = range(2, n + 1)
            p[1::2] = range(1, n)
            z += p[::s]
            r += ((len(z) // 2, *z),)
    print(*min(r or [[0]]))