Chef loves to play with arrays by himself. Today, he has an array A consisting of N distinct integers. He wants to perform the following operation on his array A.

- Select a pair of adjacent integers and remove the larger one of these two. This decreases the array size by 1. Cost of this operation will be equal to the smaller of them.

Find out minimum sum of costs of operations needed to convert the array into a single element.

-----Input-----
First line of input contains a single integer T denoting the number of test cases. First line of each test case starts with an integer N denoting the size of the array A. Next line of input contains N space separated integers, where the ith integer denotes the value Ai.

-----Output-----
For each test case, print the minimum cost required for the transformation.

-----Constraints-----
- 1 ≤ T ≤ 10 
- 2 ≤ N ≤ 50000 
- 1 ≤ Ai ≤ 105 

-----Subtasks-----
- Subtask 1 : 2 ≤ N ≤ 15 : 35 pts 
- Subtask 2 : 2 ≤ N ≤ 100 : 25 pts 
- Subtask 3 : 2 ≤ N ≤ 50000 : 40 pts 

-----Example-----
Input
2
2
3 4
3
4 2 5

Output
3
4

-----Explanation-----Test 1 :  Chef will make only 1 move: pick up both the elements (that is, 3 and 4), remove the larger one (4), incurring a cost equal to the smaller one (3).

Difficulty: EASY
Tags: Constructive algorithms

Ad-hoc

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	list_a = list(map(int, input().split()))
	if n == 2:
		print(min(list_a))
	else:
		print(min(list_a) * (n - 1))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split(' ')))
	print(min(a) * (len(a) - 1))


tests = int(input())
for i in range(tests):
	length = int(input())
	arr = list(map(int, input().split()))
	minimum = min(arr)
	print(minimum * (length - 1))


a = int(input())
for i in range(0, a):
	n = int(input())
	x = list(map(int, input().split()))
	print(min(x) * (n - 1))


g = int(input())
for h in range(g):
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	a.sort()
	s = set(a)
	ans = a[0] * (len(s) - 1)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	mi = min(l)
	print(mi * (len(l) - 1))


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	print(min(A) * (N - 1))


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	print(m * (len(l) - 1))


t = int(input())
for case in range(t):
	n = int(input())
	resource = input().split()
	resource = list(map(int, resource))
	minnum = int(min(resource))
	print(minnum * (n - 1))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(min(a) * (n - 1))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(min(arr) * (n - 1))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	m = min(l)
	print(m * (n - 1))


N = int(input())
for i in range(N):
	a = int(input())
	li = [int(x) for x in input().split()]
	a = min(li)
	print(a * (len(li) - 1))


for i in range(int(input())):
	n1 = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	print(m * (n1 - 1))


t = int(input())
for i in range(t):
	n = int(input())
	li = list(map(int, input().split()))
	cost = min(li) * (n - 1)
	print(cost)


for i in range(int(input())):
	n = int(input()) - 1
	arr = list(map(int, input().split()))
	mini = 100000
	for j in range(0, n):
		mini = min(mini, arr[j])
	k = mini * n
	print(k)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(a[0] * (n - 1))


t = int(input())
for w in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	cost = min(l)
	length = len(l)
	print(cost * (length - 1))


p = int(input())
for i in range(p):
	q = int(input())
	p = list(map(int, input().split()))
	p.sort()
	print(p[0] * (q - 1))


for _ in range(int(input())):
	a = int(input())
	k = list(map(int, input().split()))
	b = min(k)
	print(b * (a - 1))


t = int(input())
for ni in range(t):
	n = int(input())
	l = [int(i) for i in input().split()]
	a = min(l)
	print((n - 1) * a)


for _ in range(int(input())):
	a = int(input())
	l = list(map(int, input().split()))
	l.sort()
	print(l[0] * (a - 1))


c = int(input())
for _ in range(c):
	d = int(input())
	n = input().split()
	a = []
	for i in n:
		a.append(int(i))
	b = min(a)
	l = d - 1
	print(b * l)


t = int(input())
while t:
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(min(arr) * (len(arr) - 1))
	t = t - 1


t = int(input())
for i in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	print(min(lst) * (n - 1))


from math import *
for t in range(int(input())):
	n = int(input())
	numberlist = list(map(int, input().split()))
	numberlist.sort()
	print(numberlist[0] * (len(numberlist) - 1))


t = int(input())
for i in range(t):
	a = int(input())
	b = list(map(int, input().split()))
	c = min(b)
	print((a - 1) * c)


T = int(input())
for i in range(T):
	N = int(input())
	L = list(map(int, input().split(' ')))
	suma = min(L) * (len(L) - 1)
	print(suma)


try:
	T = int(input())
	for i in range(T):
		N = int(input())
		a = sorted(list(map(int, input().split())), reverse=True)[:N]
		c = a[len(a) - 1]
		a.remove(a[0])
		b = len(a) * c
		print(b)
except:
	pass


import re

class arrSol:

	def __init__(self):
		self.N = 0
		self.arr = 0

	def updateN(self, N):
		self.N = N

	def populateArr(self):
		self.arr = input()
		self.arr = re.split(' ', self.arr)

	def findCost(self):
		min = 100001
		for i in range(self.N):
			if int(self.arr[i]) < min:
				min = int(self.arr[i])
		return min * (self.N - 1)
T = int(input())
for i in range(T):
	myObj = arrSol()
	myObj.updateN(int(input()))
	myObj.populateArr()
	print(int(myObj.findCost()))
	del myObj


for _ in range(int(input())):
	elements = int(input())
	in_elements = list(map(int, input().split()))
	in_elements.sort()
	answer = in_elements[0] * (len(in_elements) - 1)
	print(answer)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(min(*a) * (n - 1))


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	minc = min(a)
	cost = minc * (n - 1)
	print(cost)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = 0
	while True:
		if len(a) == 1:
			break
		else:
			a.remove(a[1])
			s = s + a[0]
	print(s)


t = input()
for c in range(0, int(t)):
	n = int(input())
	a = input().split()
	arr = []
	for x in a:
		arr.append(int(x))
	arr.sort()
	print(arr[0] * (n - 1))


a = int(input(''))
for i in range(a):
	x = int(input(''))
	t = [int(x) for x in input('').split()]
	if x == 2:
		print(min(t))
	else:
		print(min(t) * (x - 1))


t = int(input())
for i in range(t):
	size = int(input())
	arr = input().split()
	num_arr = [int(string) for string in arr]
	count = 0
	temp = 99999999
	for num in num_arr:
		if temp > num:
			temp = num
	count = (size - 1) * temp
	print(count)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	print((n - 1) * min(a))


test = int(input())
for i in range(test):
	n = int(input())
	a = list(map(int, input().split()))
	a = sorted(a)
	print(a[0] * (n - 1))


try:
	test_case = int(input())
	for i in range(test_case):
		user = int(input())
		array = sorted(list(map(int, input().split())), reverse=True)[:user]
		c = array[len(array) - 1]
		array.remove(array[0])
		b = len(array) * c
		print(b)
except:
	pass


t = int(input())
for i in range(t):
	n = int(input())
	array = list(map(int, input().split()))
	minEle = min(array)
	print(minEle * (n - 1))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().lstrip().split()))
	arr.sort()
	q = min(arr)
	print(q * (n - 1))


class Solution:

	def solve(self, arr):
		solutions = 0
		return solutions

	def input_num_arr(self):
		return [int(num) for num in input().split(' ')]

	def input_arr(self):
		return input().split(' ')

	def input_str(self):
		return input().strip()

	def input_num(self):
		return int(input())
solver = Solution()
for _ in range(int(input())):
	n = solver.input_num()
	arr = solver.input_num_arr()
	print((n - 1) * min(arr))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	print(arr[0] * (n - 1))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	s = 0
	while len(a) != 1:
		a.remove(a[1])
		s = s + a[0]
	print(s)


t = int(input())
while t != 0:
	t = t - 1
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = min(a)
	n = n - 1
	print(ans * n)


def minmax(inp):
	mini = min(inp)
	length = len(inp) - 1
	output = mini * length
	print(output)
x = int(input())
for _ in range(x):
	y = int(input())
	inp = list(map(int, input().split()))
	minmax(inp)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l.sort()
	print(l[0] * (n - 1))


for i in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	print(min(lis) * (n - 1))


T = int(input())
for _ in range(0, T):
	n = int(input())
	a = list(map(int, input().split()))
	b = min(a)
	cost = b * (len(a) - 1)
	print(cost)


def minim(a, b):
	if a >= b:
		return b
	else:
		return a
for i in range(int(input())):
	n = int(input())
	l = input().split()
	lst = []
	for i in range(n - 1, -1, -1):
		lst.append(int(l[i]))
	print(min(lst) * (n - 1))


for _ in range(int(input())):
	n = int(input())
	arr = [int(j) for j in input().split()]
	arr = sorted(arr)
	print(arr[0] * (n - 1))


T = int(input())
for _ in range(T):
	N = int(input())
	p = [int(_) for _ in input().split()]
	min_p = min(p)
	print(min_p * (len(p) - 1))


t = int(input())
for tc in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	min = l[0]
	for i in l:
		if i < min:
			min = i
	print(min * (n - 1))


n = int(input())
for t in range(n):
	u = int(input())
	a = [int(t) for t in input().split()]
	print(min(a) * (u - 1))


def output(inp):
	l = len(inp) - 1
	m = min(inp)
	print(l * m)
n = int(input())
for _ in range(n):
	a = int(input())
	inp = list(map(int, input().split()))
	output(inp)


for i in range(int(input())):
	n = int(input()) - 1
	a = list(map(int, input().split()))
	b = min(a)
	print(b * n)


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	a.sort()
	b = a[0]
	while len(a) != 1:
		x += b
		a.remove(a[1])
	print(x)


T = int(input())
for i in range(T):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = min(arr) * (n - 1)
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	m = a[0]
	ans = (len(a) - 1) * m
	print(ans)


for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split(' ')))
	c = min(x)
	print(c * (n - 1))


T = int(input())
for _ in range(T):
	n = int(input())
	x = list(map(int, input().split()))
	print(min(x) * (n - 1))


for i in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	A.sort()
	s = 0
	while len(A) > 1:
		x = min(A[0], A[1])
		y = max(A[0], A[1])
		s = s + x
		A.remove(y)
	print(s)


t = int(input())
for i in range(t):
	N = int(input())
	X = input()
	L = X.split()
	L1 = []
	for i in L:
		L1.append(int(i))
	print(min(L1) * (N - 1))


def inte(e):
	return int(e)
for x in range(int(input())):
	size = input()
	nums = input()
	nums = nums.split()
	cost = 0
	nums.sort(key=inte)
	while len(nums) != 1:
		if int(nums[0]) >= int(nums[1]):
			cost += int(nums[1])
			nums.pop(0)
		else:
			cost += int(nums[0])
			nums.pop(1)
	print(cost)


def solve():
	n = int(input())
	l1 = [int(i) for i in input().split()]
	lent = n - 1
	return min(l1) * lent
t = int(input())
for i in range(t):
	print(solve())