Baghini wants Gifts!

3.8

20 votes
Very-Easy
Problem

It's Valentines Day! Bagh went to watch Deadpool with his girlfriend, Baghini. Somehow Baghini got furious by watching the movie. To calm her down, Bagh has to buy gifts for her now. There are two types of gifts that Baghini wants from Bagh: one is black and the other is white. To make her happy, Bagh has to buy B number of black gifts and W number of white gifts.

The cost of each black gift is X units. The cost of every white gift is Y units. The cost of converting each black gift into white gift or vice versa is Z units. Help Bagh by deducing the minimum amount he needs to spend on Baghini's gifts.

Input Format

The first line will contain an integer T which will be the number of test cases. There will be T pairs of lines. The first line of each test case will contain the values of integers B and W. Another line of each test case will contain the values of integers X, Y, and Z.

Constraints

1≤T≤10 0≤X,Y,Z, B,W≤10^9

Output Format

T lines, each containing an integer: the minimum amount of units Bagh needs to spend on gifts.

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Sample Case #01: There is no benefit to converting the white gifts into black or the black gifts into white, so Bagh will have to buy each gift for 1 unit. So cost of buying all gifts will be: 10∗1+10∗1=20.

Sample Case #02: Again, we can't decrease the cost of black or white gifts by converting colors. We will buy gifts at their original price. So cost of buying all gifts will be: 5∗2+9∗3=10+27=37.

Sample Case #03: We will buy white gifts at their original price, 1. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 1+1=2. So cost of buying all gifts will be: 3∗2+6∗1=12.

Sample Case #04: Similarly, we will buy white gifts at their original price, 2. For black gifts, we will first buy white one and color them to black, so that their cost will be reduced to 2+1=3. So cost of buying all gifts will be: 7∗3+7∗2=35.

Sample Case #05: We will buy black gifts at their original price, 1. For white gifts, we will first black gifts worth 1 unit and color them to white with another 2 units, so cost for white gifts is reduced to 3 units. So cost of buying all gifts will be: 3∗1+3∗3=3+9=12.

Editor Image

?