An electronics shop sells red and blue lamps. A red lamp costs X rupees and a blue lamp costs Y rupees.

Chef is going to buy exactly N lamps from this shop. Find the minimum amount of money Chef needs to pay such that at least K of the lamps bought are red.

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

- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of a single line containing four space-separated integers N, K, X, Y.

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

For each test case, output on a new line the minimum amount of money Chef needs to pay in order to buy N lamps such that at least K of the lamps bought are red.

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

$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤10^{8}$
$0 ≤ K ≤ N$
$1 ≤ X, Y ≤10$

----- Sample Input 1 ------ 
4
2 2 5 1
4 1 3 1
3 0 4 7
5 2 3 4

----- Sample Output 1 ------ 
10
6
12
15

----- explanation 1 ------ 
Test case $1$: Chef buys $2$ red lamps with $2 \cdot 5 = 10$ rupees.

Test case $2$: Chef buys $1$ red lamp and $3$ blue lamps with $1 \cdot 3 + 3\cdot 1 = 6$ rupees.

Test case $3$: Chef buys $3$ red lamps with $3 \cdot 4 = 12$ rupees.

Difficulty: EASY
Tags: Mathematics

Starter code:

Solutions:
t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	rc = k * x
	bc = n - k
	print(rc + bc * min(x, y))


for i in range(int(input())):
	(n, k, x, y) = map(int, input().split())
	print(k * x + (n - k) * min(x, y))


t = int(input())
for _ in range(t):
	(n, k, x, y) = map(int, input().split())
	red_lamps = k * x
	other_lamps = (n - k) * min(x, y)
	print(red_lamps + other_lamps)


t = int(input())
for i in range(0, t):
	(n, k, x, y) = list(map(int, input().split()))
	sum = (n - k) * min(x, y) + k * x
	print(sum)


t = int(input())
for k in range(t):
	(n, k, x, y) = map(int, input().split())
	sum = (n - k) * min(x, y) + k * x
	print(int(sum))


t = int(input())
for _ in range(t):
	(total, num_red, red_cost, blue_cost) = map(int, input().split())
	total_cost = num_red * red_cost
	total_cost += min(red_cost, blue_cost) * (total - num_red)
	print(total_cost)


t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	s = min(x, y)
	p = x * k + (n - k) * s
	print(p)


t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	final = k * x
	j = n - k
	final += j * min(x, y)
	print(final)


for i in range(int(input())):
	(n, k, x, y) = map(int, input().split())
	if x > y:
		print(k * x + (n - k) * y)
	else:
		print(x * n)


for i in range(int(input())):
	(w, x, y, z) = map(int, input().split())
	a = min(w * y, (w - x) * z + x * y)
	print(a)


t = int(input())
while t > 0:
	(n, k, x, y) = map(int, input().split())
	if x > y:
		total_cost = k * x + (n - k) * y
	else:
		total_cost = n * x
	print(total_cost)
	t -= 1


t = int(input())
for i in range(t):
	sum = 0
	s = list(map(int, input().split()))
	if s[3] < s[2]:
		sum = s[1] * s[2] + (s[0] - s[1]) * s[3]
	else:
		sum = s[0] * s[2]
	print(sum)


t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	blue = (n - k) * y
	red = k * x
	if x > y:
		print(blue + red)
	else:
		print(n * x)


t = int(input())
s = 0
for i in range(t):
	(n, k, x, y) = map(int, input().split(' '))
	s = k * x
	n = n - k
	if x < y:
		s += n * x
	else:
		s += n * y
	print(s)
	s = 0


t = int(input())
for _ in range(t):
	(n, k, x, y) = map(int, input().split())
	if x > y:
		print(k * x + (n - k) * y)
	else:
		print(n * x)


t = int(input())
for i in range(t):
	(N, K, X, Y) = map(int, input().split())
	print(K * X + (N - K) * min(X, Y))


for i in range(int(input())):
	(a, b, c, d) = map(int, input('').split())
	if c > d:
		print(b * c + (a - b) * d)
	else:
		print(a * c)


t = int(input())
for i in range(t):
	(a, b, c, d) = map(int, input().split())
	if c > d:
		x = b * c
		y = (a - b) * d
		print(x + y)
	else:
		print(a * c)


repeat = int(input())
for item in range(repeat):
	(amount, RedLeast, RedCost, BlueCost) = map(int, input().split())
	convert = RedLeast * RedCost
	remainLamp = amount - RedLeast
	convert2 = remainLamp * RedCost
	convert3 = remainLamp * BlueCost
	final = min(convert2, convert3)
	a = final + convert
	print(a)


t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	if x > y:
		print(k * x + (n - k) * y)
	elif x <= y:
		print(x * n)


t = int(input())
for _ in range(t):
	(n, k, x, y) = list(map(int, input().split()))
	redcost = k * x
	other = (n - k) * y
	if x >= y:
		print(redcost + other)
	else:
		print(n * x)


t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	b = (n - k) * y
	r = k * x
	if x >= y:
		print(b + r)
	else:
		print(n * x)


for i in range(int(input())):
	(a, b, x, y) = map(int, input().split())
	if x <= y:
		print(x * a)
	else:
		z = b * x + (a - b) * y
		print(z)


T = int(input())
for i in range(T):
	(X, Y, Z, A) = map(int, input().split())
	if Z <= A:
		print(X * Z)
	else:
		print(Y * Z + (X - Y) * A)


T = int(input())
for i in range(T):
	(N, K, X, Y) = map(int, input().split())
	if N <= K:
		print(N * X)
	elif N > K:
		print(K * X + (N - K) * min(X, Y))


for _ in range(int(input())):
	(N, K, X, Y) = map(int, input().split())
	if N <= K:
		print(N * X)
	elif N > K:
		print(K * X + (N - K) * min(X, Y))


a = int(input())
for j in range(a):
	b = [int(x) for x in input().split()]
	if b[0] == b[1]:
		print(b[0] * b[2])
	else:
		c = b[1] * b[2]
		d = b[0] - b[1]
		if b[2] < b[3]:
			print(d * b[2] + c)
		elif b[2] > b[3]:
			print(d * b[3] + c)
		else:
			print(d * b[3] + c)


for t in range(int(input())):
	(n, k, x, y) = map(int, input().split())
	rl = k * x
	if x > y:
		bl = (n - k) * y
		print(rl + bl)
	else:
		print(n * x)


for _ in range(int(input())):
	(N, K, X, Y) = map(int, input().split())
	total = K * X
	case1 = (N - K) * X
	case2 = (N - K) * Y
	total = total + min(case2, case1)
	print(total)


for _ in range(int(input())):
	(n, k, x, y) = list(map(int, input().split()))
	c = k * x + (n - k) * min(x, y)
	print(c)


T = int(input())
for _ in range(T):
	(N, K, X, Y) = map(int, input().split())
	if K == 0:
		if X <= Y:
			red_lamp_cost = N * X
			print(red_lamp_cost)
		elif X > Y:
			blue_lamp_cost = N * Y
			print(blue_lamp_cost)
	elif K != 0:
		if X <= Y:
			red_lamp_cost = N * X
			print(red_lamp_cost)
		elif X > Y:
			red_lamp_cost = K * X
			blue_lamp_cost = (N - K) * Y
			print(red_lamp_cost + blue_lamp_cost)


for i in range(int(input())):
	(n, k, x, y) = map(int, input().split())
	d = n - k
	fr = x * k
	c1 = fr + d * x
	c2 = fr + d * y
	if c1 > c2:
		print(c2)
	else:
		print(c1)


t = int(input())
for i in range(t):
	(N, K, X, Y) = map(int, input().split())
	red_cost = K * X
	blue_cost = (N - K) * min(X, Y)
	if N == K:
		print(red_cost)
	else:
		print(red_cost + blue_cost)


t = int(input())
for i in range(t):
	(n, k, x, y) = map(int, input().split())
	if n == k:
		print(k * x)
	elif n > k and k != 0:
		h = k * x + (n - k) * x
		u = k * x + (n - k) * y
		print(min(h, u))
	elif k == 0:
		b = n * x
		c = n * y
		print(min(b, c))


for _ in range(int(input())):
	(N, K, X, Y) = map(int, input().split())
	if K * X + (N - K) * X > K * X + (N - K) * Y:
		print(K * X + (N - K) * Y)
	else:
		print(K * X + (N - K) * X)


for t in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	if c < d:
		print(a * c)
	else:
		v = abs(a - b)
		v1 = b * c
		v1 = v1 + v * d
		print(v1)