Hanh lives in a shared apartment. There are $n$ people (including Hanh) living there, each has a private fridge. 

$n$ fridges are secured by several steel chains. Each steel chain connects two different fridges and is protected by a digital lock. The owner of a fridge knows passcodes of all chains connected to it. A fridge can be open only if all chains connected to it are unlocked. For example, if a fridge has no chains connected to it at all, then any of $n$ people can open it.

 [Image] For exampe, in the picture there are $n=4$ people and $5$ chains. The first person knows passcodes of two chains: $1-4$ and $1-2$. The fridge $1$ can be open by its owner (the person $1$), also two people $2$ and $4$ (acting together) can open it. 

The weights of these fridges are $a_1, a_2, \ldots, a_n$. To make a steel chain connecting fridges $u$ and $v$, you have to pay $a_u + a_v$ dollars. Note that the landlord allows you to create multiple chains connecting the same pair of fridges. 

Hanh's apartment landlord asks you to create exactly $m$ steel chains so that all fridges are private. A fridge is private if and only if, among $n$ people living in the apartment, only the owner can open it (i.e. no other person acting alone can do it). In other words, the fridge $i$ is not private if there exists the person $j$ ($i \ne j$) that the person $j$ can open the fridge $i$.

For example, in the picture all the fridges are private. On the other hand, if there are $n=2$ fridges and only one chain (which connects them) then both fridges are not private (both fridges can be open not only by its owner but also by another person).

Of course, the landlord wants to minimize the total cost of all steel chains to fulfill his request. Determine whether there exists any way to make exactly $m$ chains, and if yes, output any solution that minimizes the total cost. 


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 10$). Then the descriptions of the test cases follow.

The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 1000$, $1 \le m \le n$) — the number of people living in Hanh's apartment and the number of steel chains that the landlord requires, respectively.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^4$) — weights of all fridges.


-----Output-----

For each test case:

  If there is no solution, print a single integer $-1$.  Otherwise, print a single integer $c$ — the minimum total cost. The $i$-th of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$), meaning that the $i$-th steel chain connects fridges $u_i$ and $v_i$. An arbitrary number of chains can be between a pair of fridges. 

If there are multiple answers, print any.


-----Example-----
Input
3
4 4
1 1 1 1
3 1
1 2 3
3 3
1 2 3

Output
8
1 2
4 3
3 2
4 1
-1
12
3 2
1 2
3 1

Difficulty: interview

Starter code:

Solutions:
t = int(input())

for _ in range(t):
	n, m = map(int, input().split())

	a = list(map(int, input().split()))

	if n <= 2:
		print(-1)
		continue
	if m < n:
		print(-1)
		continue

	cost = 0
	edges = []
	for i in range(n):
		edges.append((i+1, (i+1)%n + 1))
		cost += 2 * a[i]

	s = sorted(range(n), key=lambda i: a[i])

	for i in range(m-n):
		edges.append((s[0]+1, s[1]+1))
		cost += a[s[0]] + a[s[1]]

	print(cost)
	for u, v in edges:
		print(u, v)

for i in ' '*int(input()):
    n,m=map(int,input().split())
    L=list(map(int,input().split()))
    if n<3:
        print(-1)
        continue
    if n>m:
        print(-1)
        continue
    else:
        min1=min(L)
        index1=L.index(min1)
        LL=L[:]
        LL.pop(index1)
        min2=min(LL)
        index2=LL.index(min2)
        if index2>=index1:index2+=1
        print(sum(L)*2+(m-n)*(min1+min2))
        for i in range(n):
            if i<n-1:
                print(i+1,i+2)
            else:
                print(1,i+1)
        for i in range(m-n):
            print(index1+1,index2+1)

import sys
import math

# sys.stdin = open("in.txt")
for _ in range(int(input())):
    n, m = map(int, input().split())

    li = list(map(int, input().split()))
    if n == 2 or n > m:
        print(-1)
        continue
    if li[0] < li[1]:
        min1 = 0
        min2 = 1
    else:
        min1 = 1
        min2 = 0
    for i in range(2, n):
        if li[i] < li[min1]:
            min2 = min1
            min1 = i
        elif li[i] < li[min2]:
            min2 = i
    print(sum(li) * 2 + (m - n) * (li[min1] + li[min2]))
    for i in range(n):
        print(i + 1, (i + 1) % n + 1)
    for i in range(m - n):
        print(min1 + 1, min2 + 1)

import sys
def I():
    return sys.stdin.readline().rstrip()

for _ in range(int(I())):
    n, m = list(map(int, I().split()))
    a = list(map(int,I().split()))
    if n > m or n == 2:
        print( -1 )
    else:
        s = 2 * sum(a)
        b = sorted(a)
        i = a.index(b[0])
        a[ i ] = 1e5
        j = a.index(b[1])
        m -= n
        s += sum(b[:2]) * m
        print( s )
        for k in range(n):
            print( k + 1, (k + 1) % n + 1 )
        for k in range(m):
            print( i + 1, j + 1 )


def solve(n,m,costs):
    if n == 2:
        print(-1)
        return
    
    if m < n:
        print(-1)
        return

    cost = 0
    for i in range(n-1):
        cost += costs[i]+costs[i+1]

    #print(cost)
    cost += costs[0]+costs[-1]
    min_cost = float('inf')
    for i in range(n):
        for j in range(i+1,n):
            cost1 = costs[i]+costs[j]
            if cost < min_cost:
                min_cost = cost1
                min_pair = [i+1,j+1]

    m -= n
    cost += m*min_cost
    print(cost)
    for i in range(n-1):
        print(i+1,i+2)

    print(n,1)
    for i in range(m):
        print(min_pair[0],min_pair[1])
    

def main():
    t = int(input())
    for i in range(t):
        n,m = list(map(int,input().split()))
        costs = list(map(int,input().split()))
        solve(n,m,costs)  


main()


for _ in range(int(input())):
    n, m = list(map(int, input().split()))
    a = [*list(map(int, input().split()))]
    if m < n or (n == 2 and m == 2):
        print(-1)
    else:
        cost = x = y = 0
        mn = float('inf')
        for i in range(n):
            t = a[i] + a[i - 1]
            cost += t
            if t < mn:
                mn = min(mn, t)
                x, y = i + 1, n if i == 0 else i
        print(cost + (m - n) * mn)
        for i in range(n - 1):
            print(i + 1, (i + 2) % (n + 1))
        print(n, 1)
        for i in range(m - n):
            print(x, y)


for _ in range(int(input())):
    fr,ch=list(map(int,input().split()))
    arr=list(map(int,input().split()))
    if(ch<fr):
        print(-1)
    elif(fr==2):
        print(-1)
    else:
        x=[[arr[i],i+1] for i in range(fr)]
        x.sort()
        print(sum(arr)*2+(ch-fr)*(x[0][0]+x[1][0]))
        
        
        for i in range(fr):
            if(i==fr-1):
                print(1,fr)
            else:
                print(i+1,i+2)
        for i in range(fr,ch):
            print(x[0][1],x[1][1])
            


q = int(input())
for rwre in range(q):
	n, m = map(int,input().split())
	l = list(map(int,input().split()))
	if m < n:
		print(-1)
	else:
		ll = [[l[i], i+1] for i in range(n)]
		ll.sort()
		l.sort()
		suma = 0
		if n == 2:
			print(-1)
		else:
			for i in range(n):
				suma += (l[i-1] + l[i])
			s1 = ll[0][1]
			s2 = ll[1][1]
			print(suma + (m-n)*(ll[0][0]+ll[1][0]))
			for i in range(n-1):
				print(i + 1, i+2)
			print(n, 1)
			a = n
			while a < m:
				print(s1, s2)
				a += 1

for _ in range(int(input())):
	n,m=map(int,input().split())
	l=list(map(int,input().split()))
	if m<n or n==2:
		print("-1")
		continue
	L=[[l[i],i+1]for i in range(n)]
	L.sort()
	ans1=2*sum(l)
	ans1+=(L[0][0]+L[1][0])*(m-n)
	print(ans1)
	for i in range(n-1):
		print(i+1,i+2)
	print(1,n)
	for i in range(m-n):
		print(L[0][1],L[1][1])

t = int(input())
for i in range (t):
    n, m = list(map(int,input().split()))
    arr = list(map(int,input().split()))
    if arr[0] < arr[1]:
        min1 = arr[0]
        index1 = 1
        min2 = arr[1]
        index2 = 2
    else:
        min1 = arr[1]
        min2 = arr[0]
        index1 = 2
        index2 = 1
    for j in range (2, n):
        if arr[j] < min2:
            if arr[j] < min1:
                min1 = arr[j]
                index1 = j + 1
            else:
                min2 = arr[j]
                index2 = j + 1
    if m < n or n == 2:
        print(-1)
    else:
        ans = sum(arr) * 2 + (m - n) * (min1 + min2)
        print(ans)
        for j in range (1, n):
            print(j, j+1)
        print(1, n)
        for j in range (m - n):
            print(index1, index2)

T = int(input())

for t in range(T):
	n, m = map(int, input().split())

	A = list(map(int, input().split()))

	tot = sum(A)

	A = [(A[i], i + 1) for i in range(n)]

	A.sort()

	if m < n or n == 2:
		print(-1)

	else:
		cost = 2 * tot

		cost += (m - n) * (A[0][0] + A[1][0])

		print(cost)

		for i in range(n):
			print(i + 1, ((i + 1) % n) + 1)

		for i in range(m - n):
			print(A[0][1], A[1][1])

for _ in range(int(input())):
	n, m = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	if m < n or n == 2:
		print(-1)
	else:
		total = 0
		ans = []
		for i in range(n):
			total += arr[i] + arr[(i+1)%n]
			ans.append((i+1, ((i+1)%n)+1))
		for i in range(m-n):
			total += arr[0] + arr[1]
			ans.append((1, 2))
		print(total)
		for ele in ans:
			print(*ele)

t = int(input())
for i in range(t):
	n, m = list(map(int, input().split()))
	dop = list(map(int, input().split()))
	suma = sum(dop)
	mas = []
	for i in range(n):
		mas.append([dop[i], i])
	if m < n or n == 2:
		print(-1)
		continue
	mas.sort()
	print(suma * 2 + (mas[0][0] + mas[1][0]) * (m - n))
	for i in range(1, n):
		print(i, i + 1)
	print(n, 1)
	for i in range(m - n):
		print(mas[0][1] + 1, mas[1][1] + 1)


t=int(input())
for i in range(t):
    n,m=[int(x) for x in input().split(' ')]
    alist=[int(x) for x in input().split(' ')]
    blist=alist[:]
    if (n == 2) or (m < n):
        print('-1')
    else:
        alist.remove(min(alist))
        print(2*sum(blist)+(m-n)*(min(blist)+min(alist)))
        for i in range(n-1):
            print(i+1,i+2,sep=' ')
        print(n,1,sep=' ')
        a=blist.index(min(blist))
        b=alist.index(min(alist))
        if b >= a:
            for i in range(m-n):
                print(a+1,b+2,sep=' ')
        else:
            for i in range(m-n):
                print(b+1,a+1,sep=' ')
            
            


def run_testcase():
	[n, m] = [int(x) for x in input().split()]
	ns = [int(x) for x in input().split()]

	if n == 2:
		print(-1)
		return

	if m < n:
		print(-1)
		return


	sum_ns = sum(ns)

	cost = 2 * sum_ns

	rest_m = m - n
	sorted_ns = list(sorted(ns))
	cheapest_edge = sorted_ns[0] + sorted_ns[1]

	cost += cheapest_edge * rest_m

	edges = [None] * m

	edges[0] = (1, n)
	for i in range(1, n):
		edges[i] = (i, i+1)

	for i in range(rest_m):
		edges[n+i] = (sorted_ns[0], sorted_ns[1])

	print(cost)
	for edge in edges:
		print(edge[0], edge[1])



testcase_count = int(input())

for i in range(testcase_count):
	# print(str(run_testcase()))
	run_testcase()

import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    q = int(input())
    for _ in range(q):
        n, m = list(map(int,input().split()))
        a = list(map(int,input().split()))
        if n > m or n == 2:
            print(-1)
        else:
            ansv = 2*sum(a)
            ans = [[k,k+1] for k in range(1,n)]
            ans.append([n,1])
            b = sorted([[a[k],k+1] for k in range(n)])
            for k in range(m-n):
                ans.append([b[0][1],b[1][1]])
            print(ansv+(m-n)*(b[0][0]+b[1][0]))
            for e in ans:
                print(*e)

def __starting_point():
    main()

__starting_point()

t=int(input())
for _ in range(t):
    n,m=map(int,input().split())
    aa=list(map(int,input().split()))
    if m<n or n==1 or n==2:
        print(-1)
    else:
        a=[(aa[i],i) for i in range(n)]
        a=sorted(a)
        ans=sum(aa)*2
        m -= n
        ans += m*(a[0][0]+a[1][0])
        print(ans)
        for i in range(1,n):
            print(i,i+1)
        print(n,1)
        for i in range(m):
            print(a[0][1]+1,a[1][1]+1)

tc = int(input())
for ii in range(tc):
    n,m = list(map(int,input().split()))
    ar = list(map(int,input().split()))
    if m<n or n==2:
        print(-1)
        continue
    ans = 0
    mn1 = float('inf')
    for i in range(n):
        ans += ar[i]*2
        if(ar[i]<mn1):
            save1 = i
            mn1 = ar[i]
        
    m -= n
    mn2 = float('inf')
    for i in range(n):
        if (ar[i]<mn2) and (i!=save1):
            mn2 = ar[i]
            save2 = i

    ans += m*(mn1+mn2)

    print(ans)
    for i in range(m):
        print(save1+1,save2+1)
    for i in range(n):
        print(i+1, (i+1)%n + 1)

    


for _ in range(int(input())):
	n, m = map(int, input().split())
	l2 = list(map(int, input().split()))
	l = sorted(l2)
	if n == 2 or m < n:
		print(-1)
		continue
	ans = 2*sum(l)
	ans += (m-n)*(l[0]+l[1])
	print(ans)
	for i in range(1, n):
		print(i, i+1)
	print(n, 1)
	for i in range(m-n):
		print(l2.index(l[0])+1, l2.index(l[1])+1)

# stdin = open("testdata.txt", "r")
from sys import stdin

def input():
	return stdin.readline().strip()

t = int(input())

for _ in range(t):
	n, m = list(map(int, input().split()))
	weight = list(map(int, input().split()))
	# print(weight)
	edges = [(i, (i+1)%n) for i in range(n)]
	edges.sort(key = lambda x: (weight[x[0]]+weight[x[1]]))
	total = sum(weight[u]+ weight[v] for u, v in edges)
	# print(edges)
	if m < n:
		print(-1)
	elif n == 2:
		print(-1)
	else:
		u, v = edges[0]
		total += (weight[u] + weight[v])*(m-n)
		print(total)
		for i in range(n):
			print(edges[i][0]+1, edges[i][1]+1)
		for j in range(m-n):
			print(edges[0][0]+1, edges[0][1]+1)


import sys
import math
import itertools
import collections


def sieve(n):
    if n < 2: return list()
    prime = [True for _ in range(n + 1)]
    p = 3
    while p * p <= n:
        if prime[p]:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 2
    r = [2]
    for p in range(3, n + 1, 2):
        if prime[p]:
            r.append(p)
    return r
def divs(n, start=1):
    r = []
    for i in range(start, int(math.sqrt(n) + 1)):
        if (n % i == 0):
            if (n / i == i):
                r.append(i)
            else:
                r.extend([i, n // i])
    return r
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return list(map(int, input().split()))
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def wr(arr): return ''.join(map(str, arr))
def revn(n): return str(n)[::-1]
def prime(n):
    if n == 2: return True
    if n % 2 == 0 or n <= 1: return False
    sqr = int(math.sqrt(n)) + 1
    for d in range(3, sqr, 2):
        if n % d == 0: return False
    return True
def convn(number, base):
    newnumber = 0
    while number > 0:
        newnumber += number % base
        number //= base
    return newnumber


t = ii()
for _ in range(t):
    n, m = mi()
    a = li()
    if n > m or n == 2:
        print(-1)
    else:
        ans = 2 * sum(a)
        mina = min(a)
        posmin = a.index(min(a))
        b = a.copy()
        b.sort()
        posmin1 = a.index(b[1])
        if posmin == posmin1:
            posmin1 = a.index(b[1], posmin + 1)
        ans += (mina + b[1]) * (m - n)
        print(ans)
        for i in range(n - 1):
            print(i + 1, i + 2)
        print(n, 1)
        for i in range(m - n):
            print(posmin + 1, posmin1 + 1)