oz-and-his-game-board-1

Today Oz is playing with his game-board. He has N coins of type-1 and M coins of type-2. Also the game-board has N squares of type-1 and M squares of type-2. In this game Oz must place one coin into each square. After placing all coins Oz will get a score based on his coin placement strategy . 

His score calculation is like this : If type-1 square contains a type-1 coin then he will get A points, if type-2 square contains a type-2 coin then he will get B points and in all other cases, he will get C points.
Oz's total game score will be sum of scores of all squares. Help Oz to earn maximum game points with his game-board.

Input :

The first line contains the number of test cases T . 

Each test case consists of five space separated integers - N, M, A, B and C. 

Output :

For each test case output maximum game points that Oz can earn.

Constraints :

1 ≤  T ≤ 100

1 ≤ N, M ≤ 10^9

-10^4  ≤ A, B, C ≤ 10^4

SAMPLE INPUT
2
3 4 500 800 600
1 5 10 -20 -30

SAMPLE OUTPUT
4700
-90

Explanation

For first sample :

With optimal strategy Oz will place 3 coins of type-1 into 3 squares of type-1 getting score =500+500+500 = 1500
and he will place 4 coins of type-2 into 4 squares of type-2 getting score = 800+800+800+800 = 3200
so total score will be 1500+3200 =4700
He cannot get better then this in any other coin placement strategy.

Source: hackerearth | Difficulty: EASY
Tags: Mathematics

Ad-hoc


Solutions:
'''
# Read input from stdin and provide input before running code'''

t = input()
p=[]
#print 'Hi, %s.' % t
for i in range(int(t)):
	x = input()
	p = x.split(" ");
	n = int(p[0]);
	m = int(p[1]);
	a = int(p[2]);
	b = int(p[3]);
	c = int(p[4]);
	sum1 = n*a+m*b;
	if(n>m):
		sum2 = 2*m*c+(n-m)*a;
	else:
		sum2 = 2*n*c+(m-n)*b;
	if(sum1>sum2):
		print(sum1)
	else:
		print(sum2)


diff=0
def ABS(a):
	if(a<0):return -a
	return a
	
def MIN(one,two):
	if(one < two):		
		return one
	return two
	
test=eval(input())
for t in range(0,test):
	N,M,A,B,C=list(map(int,input().split()))
	first=N*A + M*B
	second= MIN(N,M)
	if(N>M):
		diff=A *ABS(N-M)
	else: diff=B * ABS(N-M)
	second=2*C*second + diff
	if(first>second):print(first)
	else :print(second)


for cases in range(int(input())):
	n,m,a,b,c = list(map(int, input().split()))
	ma = n*a + m*b
	if n >= m:
		ma = max(ma, 2*(m*c) + (n-m)*a)
	else:
		ma = max(ma, 2*(n*c) + (m-n)*b)
	print(ma)


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

import sys
t=input()
t=int(t)
while(t):
	t=t-1
	num=list(map(int, sys.stdin.readline().split(' ')))
	n0=num[0]
	n1=num[1]
	n2=num[2]
	n3=num[3]
	n4=num[4]
	s=n0*n2+n1*n3
	i=min(n0,n1)
	n0_s=(n2*(n0-i))+(n3*(n1-i))+2*i*n4		
	if(n0_s>s):
		s=n0_s
	print(s)


t = int(input())
while(t != 0):
	t = t - 1
	n,m,a,b,c = [int(x) for x in input().split()]
	if((2*c) > (a + b)):
		print ((n*a + m*b + (2*c - a - b)*min(n,m)))
	else:
		print((n*a + m*b))


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
import sys
t=input()
t=int(t)
while(t):
	t=t-1
	numbers=list(map(int, sys.stdin.readline().split(' ')))
	n=numbers[0]
	m=numbers[1]
	a=numbers[2]
	b=numbers[3]
	c=numbers[4]
	sc=n*a+m*b
	i=min(n,m)
	n_sc=(a*(n-i)) + (b*(m-i)) + 2*i*c
		
	if( n_sc> sc):
		sc=n_sc
	'''
	for i in range(min(n,m)+1):
		n_sc=(a*(n-i)) + (b*(m-i)) + 2*i*c
		#print n_sc
		if( n_sc> sc):
			sc=n_sc
	'''
	print(sc)


t=eval(input());
while(t):
	t=t-1
	suma=0
	x=input().split()
        n=int(x[0])
        m=int(x[1])
        a=int(x[2])
        b=int(x[3])
        c=int(x[4])
        if(n>m):
            suma=suma+((n-m)*a)
            #print suma
            if(2*c>a+b):
    		suma=suma+(c*2*m)
    		#print suma
            else:
    		suma=suma+((a+b)*m)
    		#print suma
        else:
            suma=suma+((m-n)*b)
            #print suma
            if(2*c>a+b):
    		suma=suma+(c*2*n)
    		#print suma
            else:
    		suma=suma+((a+b)*n)
    		#print suma
        print(suma)
        #print '\n'


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
t = int(input())
for i in range(0,t):
	s = input().split(" ")
	t1 = int(s[0])
	t2 = int(s[1])
	
	a = int(s[2])
	b = int(s[3])
	c = int(s[4])
	
	st1  = t1 * a + t2 * b
	
	mn = t1
	mtype = 1
	
	dif = t2 -t1
	if(t2 <t1):
		mn = t2
		dif = t1 - t2
		mtype = 2
	
	st2 = mn*c*2
	if(mtype==2):
		st2 += dif * a
	else:
		st2 += dif*b
	if  st2 > st1:
		print(st2)
	else:
		print(st1) 
	 


no_of_test_cases = int(input())

for _ in range(no_of_test_cases):
	inp = input().split()
	N,M,A,B,C = int(inp[0]),int(inp[1]),int(inp[2]),int(inp[3]),int(inp[4])
	print(max(N*A+M*B,min(N,M)*C*2+abs(N-M)*(A if (N>M) else B)))


no_of_test_cases = int(input())

for _ in range(no_of_test_cases):
	inp = input().split()
	N,M,A,B,C = int(inp[0]),int(inp[1]),int(inp[2]),int(inp[3]),int(inp[4])
	inplace = N*A+M*B;
	outplace = min(N,M)*C*2
	if (N>M):
		outplace+=(N-M)*A
	else:
		outplace+=(M-N)*B
	print(max(inplace,outplace))


tc = input()
tc = int(tc)
t = 0
for t in range(tc):
	n,m,a,b,c = input().split()
	n = int(n)
	m = int(m)
	a = int(a)
	b = int(b)
	c = int(c)
	totalResult = 0
	minVal = min(n, m)
	if (a+b) > (2*c):
		totalResult = a + b
		totalResult = minVal * totalResult
	else:
		totalResult = 2 * c
		totalResult = minVal * totalResult
	
			
	if(n > m):
		totalResult = totalResult + ((n-m)*a)
	else:
		totalResult = totalResult + ((m-n)*b)
	print(totalResult)


		

	
		





for _ in range(int(input())):
	n,m,a,b,c=list(map(int,input().split()))
	if a+b<2*c:
		d=min(n,m)
		print(2*d*c + (n-d)*a +(m-d)*b)
	else:
		print(n*a + m*b)


def maxscore(nType1,nType2,nType1Score,nType2Score,nOtherTypeScore):
	if(nType1>nType2):
		nDiff = nType1 - nType2
		nMaxScore = (nType1+nType2 - nDiff)*nOtherTypeScore + nDiff*nType1Score
	else:
		nDiff = nType2 - nType1
		nMaxScore = (nType1+nType2 - nDiff)*nOtherTypeScore + nDiff*nType2Score
	return nMaxScore


nTestCases = int(eval(input()))
for i in range(0,nTestCases):
	nType1,nType2,nType1Score,nType2Score,nOtherTypeScore = input().split()
	nType1 = int(nType1)
	nType2 = int(nType2)
	nType1Score = int(nType1Score)
	nType2Score = int(nType2Score)
	nOtherTypeScore = int(nOtherTypeScore)
	
	if(nOtherTypeScore > nType1Score) and (nOtherTypeScore > nType2Score):
		nMaxScore = maxscore(nType1,nType2,nType1Score,nType2Score,nOtherTypeScore)
	else:
		nTemp1Score = nType1*nType1Score + nType2*nType2Score
		nTemp2Score = maxscore(nType1,nType2,nType1Score,nType2Score,nOtherTypeScore)
		if(nTemp1Score > nTemp2Score):
			nMaxScore = nTemp1Score
		else:
			nMaxScore = nTemp2Score
	print(nMaxScore)
		
		


T = int(input())
for t in range(T):
	x, y, a, b, c = [int(i) for i in input().split()]
	if (a >= b and a >= c) or (b>=a and b>=c):
		ans = x*a + y*b
		if x > y:
			ans1 = 2*y*c + (x-y)*a
		else:
			ans1 = 2*x*c + (y-x)*b
		if ans < ans1:
			ans = ans1
	elif c>= a and c >= b:
		if x > y:
			ans = 2*y*c + (x-y)*a
		else:
			ans = 2*x*c + (y-x)*b
	print(ans)
	
		


test_case = eval(input())
for test in range(test_case):
	line = input()
	value_list = line.split()
	N = int(value_list[0])
	M = int(value_list[1])
	A = int(value_list[2])
	B = int(value_list[3])
	C = int(value_list[4])

	sum_placed = N*A + M*B
	if N > M:
		sum_displaced = 2*M*C+ (N-M)*A
	elif M>N:
		sum_displaced = 2*N*C+ (M-N)*B
	else:
		sum_displaced = 2*M*C

	if sum_placed>sum_displaced:
		print(str(sum_placed))
	else:
		print(str(sum_displaced))


t=eval(input());
while(t>0):
	t -=1;
	r = input().split(" ");
	n,m,a,b,c = list(map(int,r));
	x1 = a*n + b*m;
	if(n>=m):
		x2 = 2*m*c+(n-m)*a;
	elif(m>n):
		x2 = 2*n*c+(m-n)*b;
	if(x1>=x2):
		print(x1);
	else:
		print(x2);


t=int(input())
while t!=0:
	t-=1
	n,m,a,b,c=list(map(int,input().split()))
	r1=(n*a)+(m*b)
	r2=0
	if n<=m:
		r2=((2*n)*c)+((m-n)*b) 
	elif m<n:
		r2=((2*m)*c)+((n-m)*a)    
	r=max(r1,r2)
	print(r)


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
print 'Hello World!'
'''
t=eval(input())
max = 0
while t>0:
	n=input()
	q = n.split(' ')
	n=int(q[0])
	m=int(q[1])
	a=int(q[2])
	b=int(q[3])
	c=int(q[4])
	r=a+b
	if n>m:
		dif=n-m
		if r>(2*c):
			max = m*r+dif*a
		else:
			max = 2*m*c+dif*a
		#max = ((a+b)>c?((2m*(a+b))+dif*a):((2m*(c))+dif*a))
	else:
		dif=m-n
		if r>(2*c):
			max = n*r+dif*b
		else:
			max = 2*n*c+dif*b
		#max = ((a+b)>c?((2m*(a+b))+dif*a):((2m*(c))+dif*a))
	t-=1
	print(max)


	


tc = int(input())

for i in range(tc):
	
	line = input()
	a = [int(j) for j in line.split()]
	
	if a[0] == a[1]:
		
		if (a[2]+a[3]) < (2 * a[4]):
			ans=int(2 * a[0] * a[4] )
		elif (2 * a[0] * a[4] )>((a[0] * a[2]) + (a[1] * a[3])):
			ans=int(2 * a[0] * a[4] )
		
		
	elif a[1]>a[0]:
		diff=a[1]-a[0]
		sum1=(2 * a[0] * a[4] ) + ( diff * a[3] )
		ans1= int(a[0]) * a[2]
		ans2 =int(a[1]) * a[3]
		sum2=int(ans1)+int(ans2)
		if sum1 > sum2:
			ans = sum1
		else:
			ans = sum2
	
	else:
		diff=a[0]-a[1]
		sum1=(2 * a[1] * a[4] ) + ( diff * a[2] )
	
		ans1= int(a[0]) * a[2]
		ans2 =int(a[1]) * a[3]
		sum2=int(ans1)+int(ans2)	
		if sum1 > sum2:
			ans = sum1
		else:
			ans = sum2
						
	print(ans)					
	





'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''



a = input()
ultimate_list=[]
reser_value = 0
final_value = []
for i in range(0,int(a)):
	value = input()
	value = value.split()
	value = [ int(i) for i in  value ]
	ultimate_list.append(value)
count = 0	

for i in ultimate_list:
	count = 0
	value = 0
	replace_value = 0
	replace_value2 = 0
	for j in i[0:2]:
		count = count + 1 
		if count == 1:
			value = value + int(j) * i[2]
		#replacement code
			if i[0] < i[1]:
					val = int(i[1]) - int(i[0])
					a = i[1] - int(val)
					final = (int(j) * i[4]) + (a * i[4]) + (int(val) * i[3])
			if i[0] > i [1]:
					val = int(i[0]) - int(i[1])
					a = i[0] - int(val)
					final = (int(a) * i[4]) + (i[1] * i[4]) + (int(val) * i[2])	
			if i[0] == i [1]:		
				    final = (i[0] * i[4]) + (i[1] * i[4])
		if count == 2:
			value = value + int(j) * i[3]
			#replacement count
			
	if final > value : 
		final_value.append(final)
	else:
		final_value.append(value)
			
for i in final_value:
	print(i)
	


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
tc=int(input())
while tc:
	n,m,a,b,c=list(map(int,input().split()))
	if 2*c >= a+b:
		if n<=m:
			ans = 2*n*c+(m-n)*b
		else:
			ans = 2*m*c+(n-m)*a
	else:
		ans = n*a+m*b
	print(ans)
	tc-=1


t=int(input())
for i in range(0,t):
	inp=input().split()
	n=int(inp[0])
	m=int(inp[1])
	a=int(inp[2])
	b=int(inp[3])
	c=int(inp[4])
	ans1=n*a+m*b
	ans2=min(n,m)*c*2
	if n>m:
		ans2+=a*(n-m)
	else:
		ans2+=b*(m-n)
	print(max(ans2,ans1))


T= int(input(""))
while(T):
	T-=1
	N,M,A,B,C= list(map(int,input().split()))
	if (A+B<2*C):
		if(N>=M):print((N-M)*A+M*C*2)
		else:print((M-N)*B+N*C*2)
	else:print(N*A+M*B)



t=eval(input()) 
for i in range(t):
	n,m,a,b,c = list(map(int, input().split()))
	ans=n*a+m*b
	if n>m:
		t=2*m*c+(n-m)*a
		ans=max(ans,t)
	else:
		t=2*n*c+(m-n)*b
		ans=max(ans,t)
	print(ans)
	


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''



a = input()
ultimate_list=[]
reser_value = 0
final_value = []
for i in range(0,int(a)):
	value = input()
	value = value.split()
	value = [ int(i) for i in  value ]
	ultimate_list.append(value)
count = 0	

for i in ultimate_list:
	count = 0
	value = 0
	replace_value = 0
	replace_value2 = 0
	for j in i[0:2]:
		count = count + 1 
		if count == 1:
			value = value + int(j) * i[2]
		#replacement code
			if i[0] < i[1]:
					val = int(i[1]) - int(i[0])
					a = i[1] - int(val)
					final = (int(j) * i[4]) + (a * i[4]) + (int(val) * i[3])
			if i[0] > i [1]:
					val = int(i[0]) - int(i[1])
					a = i[0] - int(val)
					final = (int(a) * i[4]) + (i[1] * i[4]) + (int(val) * i[2])	
			if i[0] == i [1]:		
				    final = (i[0] * i[4]) + (i[1] * i[4])
		if count == 2:
			value = value + int(j) * i[3]
			#replacement count
			
	if final > value : 
		final_value.append(final)
	else:
		final_value.append(value)
			
for i in final_value:
	print(i)