#arc174b. B - Bought Review

B - Bought Review

Score: 300300 points

问题陈述

TT 个测试案例解决以下问题。

在美食评论网站 EatCocoder 上,您可以用从 1155 的整数星级来评价餐厅。 最初,由厨师 B 管理的餐厅有 AiA_iii 星级的评论。(1i5)(1 \le i \le 5) 厨师可以向 EatCocoder 管理层支付 PiP_i 日元的贿赂,以获得额外的一条 ii 星级评论。(1i5)(1 \le i \le 5)

通过贿赂总共增加 kk 条评论后,将总共有 A1+A2+A3+A4+A5+kA_1+A_2+A_3+A_4+A_5+k 条评论。 厨师 B 希望这些评论的平均评分至少为 3 星级。确定实现此目的所需的最少贿赂总额。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

Solve the following problem for TT test cases.

On the gourmet review site EatCocoder, you can review restaurants with an integer number of stars from 11 to 55.
Initially, the restaurant managed by Chef B has AiA_i reviews with ii stars. (1i5)(1 \le i \le 5)
The chef can pay a bribe of PiP_i yen to the EatCocoder administration to have one additional ii-star review. (1i5)(1 \le i \le 5)

After adding a total of kk reviews by bribery, there will be A1+A2+A3+A4+A5+kA_1+A_2+A_3+A_4+A_5+k reviews in total.
Chef B wants the average rating of these reviews to be at least 33 stars. Determine the minimum total amount of bribery required to achieve this.

Constraints

  • All input values are integers.
  • 1T1041 \le T \le 10^4
  • 0Ai1080 \le A_i \le 10^8
  • 1A1+A2+A3+A4+A51 \le A_1+A_2+A_3+A_4+A_5
  • 1Pi1081 \le P_i \le 10^8

Input

The input is given from Standard Input in the following format:

TT

Case1\rm{Case}_1

Case2\rm{Case}_2

\vdots

Case\rm{Case}T_T

Here, Case\rm{Case}i_i represents the ii-th test case.

Each test case is given in the following format:

A1A_1 A2A_2 A3A_3 A4A_4 A5A_5

P1P_1 P2P_2 P3P_3 P4P_4 P5P_5

Output

Print TT lines in total.
The ii-th line should contain the answer to the ii-th test case as an integer.

Sample Input 1

6
1 0 1 0 0
1 2 3 4 5
0 2 2 0 0
1 1 1 1 5
0 1 2 0 0
1 1 1 5 3
1 1 1 0 0
1 1 1 1 1
0 0 0 0 1
1 1 1 1 1
100000000 100000000 100000000 0 0
100000000 100000000 100000000 100000000 100000000

Sample Output 1

5
2
3
2
0
15000000000000000

This input contains six test cases.

  • For the first test case, you can, for example, do the following to have an average rating of at least 33 stars with a bribe of 55 yen, which is the minimum possible amount.
    • Initially, there are 1,0,1,0,01,0,1,0,0 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively.
    • Pay a bribe of P5=5P_5 = 5 yen to add one 55-star review.
    • As a result, there are 1,0,1,0,11,0,1,0,1 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively, averaging 33 stars.
  • For the second test case, you can, for example, do the following to have an average rating of at least 33 stars with a bribe of 22 yen, which is the minimum possible amount.
    • Initially, there are 0,2,2,0,00,2,2,0,0 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively.
    • Pay a bribe of P4×2=2P_4 \times 2 = 2 yen to add two 44-star reviews.
    • As a result, there are 0,2,2,2,00,2,2,2,0 reviews of with 1,2,3,4,51,2,3,4,5 stars, respectively, averaging 33 stars.
  • For the third test case, you can, for example, do the following to have an average rating of at least 33 stars with a bribe of 33 yen, which is the minimum possible amount.
    • Initially, there are 0,1,2,0,00,1,2,0,0 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively.
    • Pay a bribe of P5=3P_5 = 3 yen to add one 55-star review.
    • As a result, there are 0,1,2,0,10,1,2,0,1 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively, averaging 3.253.25 stars.
  • For the fourth test case, you can, for example, do the following to have an average rating of at least 33 stars with a bribe of 22 yen, which is the minimum possible amount.
    • Initially, there are 1,1,1,0,01,1,1,0,0 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively.
    • Pay a bribe of P4=1P_4 = 1 yen to add one 44-star review.
    • Pay a bribe of P5=1P_5 = 1 yen to add one 55-star review.
    • As a result, there are 1,1,1,1,11,1,1,1,1 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively, averaging 33 stars.
  • For the fifth test case, you can, for example, do the following to have an average rating of at least 33 stars with a bribe of 00 yen, which is the minimum possible amount.
    • Initially, there are 0,0,0,0,10,0,0,0,1 reviews with 1,2,3,4,51,2,3,4,5 stars, respectively.
    • Since the average is already 55, which is not less than 33, give no bribe.
  • For the sixth test case, note that the answer may not fit into a 3232-bit signed integer.