Janmansh is at the fruit market to buy fruits for Chingari. There is an infinite supply of three different kinds of fruits with prices A, B and C. 

He needs to buy a total of X fruits having at least 2 different kinds of fruits. What is the least amount of money he can spend to buy fruits?

------ Input Format ------ 

- The first line of the input contains a single integer T - the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains four space separated integers X, A, B, C - the number of fruits to buy and the prices of the three different types of fruits respectively.

------ Output Format ------ 

For each test case, output the least amount of money he can spend to buy fruits.

------ Constraints ------ 

$1 ≤T ≤10^{5}$
$2 ≤X ≤1000$
$1 ≤A, B, C ≤100$

----- Sample Input 1 ------ 
2
2 1 1 1
3 4 3 2

----- Sample Output 1 ------ 
2
7

----- explanation 1 ------ 
Test case-1: He can buy any two fruits of different kinds for a total price of $2$.

Test case-2: He can buy $1$ fruit of price $3$ and $2$ fruits of price $2$ for a total price of $7$.

Difficulty: EASY
Tags: Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
for i in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	m = min(a, b, c)
	mx = max(a, b, c)
	print((x - 1) * m + (a + b + c - m - mx))


T = int(input())
for i in range(T):
	(X, A, B, C) = map(int, input().split())
	minCost = min(A, B, C)
	Sum = 0
	for i in range(1, X):
		Sum += minCost
	if A == minCost:
		remaining = [B, C]
	elif B == minCost:
		remaining = [A, C]
	else:
		remaining = [A, B]
	Sum += min(remaining)
	print(Sum)


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


class Solution:

	def solve(self, x, a, b, c):
		current = x - 1
		s = sorted([a, b, c])
		ans = current * s[0]
		ans += s[1]
		return ans

class Driver:

	def __init__(self):
		self._solver = Solution()

	def drive(self):
		test_cases = int(input().strip())
		for _ in range(test_cases):
			(x, a, b, c) = map(int, input().strip().split(' '))
			ans = self._solver.solve(x, a, b, c)
			print(ans)
Driver().drive()


for _ in range(int(input())):
	N = list(map(int, input().split()))
	X = N[0]
	N[0] = 0
	N.sort()
	ma = N[2]
	mi = N[1]
	price = 0
	price = mi * (X - 1) + ma
	print(price)


t = int(input())
for i in range(t):
	(x, a, b, c) = map(int, input().split())
	f = min(a, b, c)
	e = max(a, b, c)
	print((x - 1) * f + (a + b + c - e - f))


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


t = int(input())
for i in range(t):
	(x, a, b, c) = map(int, input().split())
	p = min(a, b, c)
	q = max(a, b, c)
	print((x - 1) * p + (a + b + c - p - q))


T = int(input())
for _ in range(T):
	(X, A, B, C) = map(int, input().split())
	(min_p, max_p) = (min(A, B, C), max(A, B, C))
	print((X - 1) * min_p + (A + B + C - min_p - max_p))


def minimum(a, b, c):
	li = list()
	li.append(a)
	li.append(b)
	li.append(c)
	li.sort()
	return (li[0], li[1])
for _ in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	(mini, sec_mini) = minimum(a, b, c)
	print(sec_mini + (x - 1) * mini)


n = int(input())
while n > 0:
	(a, b, c, d) = map(int, input().split())
	l = [b, c, d]
	l.sort()
	print((a - 1) * l[0] + l[1])
	n = n - 1


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	e = []
	e.append(b)
	e.append(c)
	e.append(d)
	e.sort()
	if a == 2:
		print(e[0] + e[1])
	elif a > 2:
		print((a - 1) * e[0] + e[1])


t = int(input())
for t in range(t):
	fruits = list(map(int, input().split()))
	num = fruits[0]
	fruits.pop(0)
	fruits.sort()
	smallest = fruits[0]
	second_smallest = fruits[1]
	for x in range(num - 1):
		second_smallest += smallest
	print(second_smallest)


for i in range(int(input())):
	(X, A, B, C) = map(int, input().split())
	l = []
	l.append(A)
	l.append(B)
	l.append(C)
	l.sort()
	print((X - 1) * l[0] + l[1])


for i in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	m1 = min(a, b, c)
	m2 = max(a, b, c)
	print((x - 1) * m1 + (a + b + c - m1 - m2))


n = int(input())

def secondmin(arr):
	minimum = min(arr)
	arr.remove(minimum)
	return min(arr)
for _ in range(n):
	arr = list(map(int, input().split()))
	x = arr[0]
	m = min(arr[1:len(arr) + 1])
	sm = secondmin(arr[1:len(arr) + 1])
	if x == 2:
		print(m + sm)
	else:
		print(m * (x - 1) + sm)


t = int(input())
for _ in range(t):
	(x, a, b, c) = map(int, input().split())
	mn = min(a, b, c)
	mx = max(a, b, c)
	print((x - 1) * mn + (a + b + c - mn - mx))


t = int(input())
for _ in range(t):
	ls = list(map(int, input().split()))
	pr = ls[1:]
	pr.sort()
	print(pr[0] * (ls[0] - 1) + pr[1])


for _ in range(int(input())):
	ls = list(map(int, input().rstrip().split()))
	ls2 = ls[1:]
	ls2.sort()
	print((ls[0] - 1) * ls2[0] + ls2[1])


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


for _ in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	l = [a, b, c]
	s = sorted(l)
	if x > 0 and x <= 2:
		print(s[0] + s[1])
	elif x > 2:
		print(s[0] * (x - 1) + s[1])


t = int(input())
for i in range(t):
	(x, a, b, c) = map(int, input().split())
	lst = []
	lst.append(a)
	lst.append(b)
	lst.append(c)
	lst.sort()
	print((x - 1) * lst[0] + lst[1])


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


n = int(input())
for _ in range(n):
	(x, a, b, c) = map(int, input().split())
	l1 = [a, b, c]
	l1.sort()
	m = (x - 1) * l1[0] + 1 * l1[1]
	print(m)


for i in range(int(input())):
	(total_fruit, fruit_a, fruit_b, fruit_c) = map(int, input().split())
	lis = [fruit_c, fruit_b, fruit_a]
	lis.sort()
	print(lis[0] * (total_fruit - 1) + lis[1])


t = int(input())
for _ in range(t):
	(x, a, b, c) = list(map(int, input().split()))
	s = [a, b, c]
	p1 = min(s)
	s.remove(p1)
	p2 = min(s)
	res = p2 + (x - 1) * p1
	print(res)


T = int(input())
for i in range(T):
	(X, A, B, C) = map(int, input().split())
	Minimum = min(A, B, C)
	Maximum = max(A, B, C)
	print((X - 1) * Minimum + (A + B + C - Minimum - Maximum))


for t in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	ab = []
	ab.append(b)
	ab.append(c)
	ab.append(d)
	ab.sort()
	print((a - 1) * ab[0] + ab[1])


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


for i in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	m = max(b, c, d)
	n = min(b, c, d)
	s = b + c + d
	print(n * (a - 1) + (s - m - n))


for i in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	k = max(a, b, c)
	j = min(a, b, c)
	print(j * (x - 1) + (a + b + c - k - j))


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


for _ in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	t1 = max(a, b, c)
	t2 = min(a, b, c)
	s = a + b + c
	t3 = s - t1 - t2
	print((x - 1) * min(t2, t3) + max(t2, t3))


for i in range(int(input())):
	(x, a, b, c) = map(int, input().split())
	min1 = min(a, b, c)
	max1 = max(a, b, c)
	print(min1 * (x - 1) + (a + b + c - min1 - max1))


t = int(input())
for i in range(t):
	(x, a, b, c) = map(int, input().split())
	min_ = min(a, b, c)
	max_ = max(a, b, c)
	print((x - 1) * min_ + (a + b + c - min_ - max_))


t = int(input())
while t > 0:
	(x, a, b, c) = map(int, input().split())
	lst = [a, b, c]
	lst.sort()
	(a, b, c) = (lst[0], lst[1], lst[2])
	print(b + (x - 1) * a)
	t -= 1


t = int(input())
for _ in range(t):
	(x, a, b, c) = map(int, input().split())
	array = [a, b, c]
	array.sort()
	print((x - 1) * array[0] + array[1])


for i in range(int(input())):
	c = [int(i) for i in input().split(' ')]
	x = c[0]
	amt = c[1:]
	amt.sort()
	print(amt[0] * (x - 1) + amt[1])


t = int(input())
for i in range(t):
	(x, a, b, c) = map(int, input().split())
	list1 = [a, b, c]
	y = min(list1)
	list1.sort()
	z = list1[-2]
	print(y * (x - 1) + z)


for _ in range(int(input())):
	list1 = list(map(int, input().split()))
	maxi = max(list1[1], list1[2], list1[3])
	mini = min(list1[1], list1[2], list1[3])
	list1 = list(list1)
	list2 = [list1[1], list1[2], list1[3]]
	mini_count = list2.count(mini)
	maxi_count = list2.count(maxi)
	second_minimum = 0
	for i in list1:
		if i != mini and i != maxi:
			second_minimum = i
		else:
			pass
	l = list1[0] - 1
	condition = [list1[1] != list1[2], list1[2] != list1[3], list1[3] != list1[1]]
	if all(condition):
		print(l * mini + second_minimum)
	elif any(condition):
		if mini_count > 1:
			print(list1[0] * mini)
		else:
			print(l * mini + maxi)
	else:
		print(list1[0] * mini)


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


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