Under construction

Still being built. Things may change

Battlefield (Large)

Time limit5sMemory limit512 MB

Summary
You reorder your three weapon groups against the fixed enemy line to maximize the summed duel scores.
Level

Medium7 of 10

Topics
Brute force, Math
Solved
No attempts yet

Problem

After the Third World War most modern technology was gone and only a few people survived. Humanity rebuilt slowly and started a new history. Thousands of years later, in a new medieval age, General Kim Yushin faced an enemy general on the battlefield.

Kim Yushin commands NN officers. AA of them carry weapon 1, BB carry weapon 2, and CC carry weapon 3. They stand side by side in one long line on a hill, looking at the enemy camp.

The enemy general also commands NN officers. DD of them carry weapon 4, EE carry weapon 5, and FF carry weapon 6. They form one line on the opposite hill and prepare to hold off the attack.

Once the battle starts, the two lines face each other in the order they stand, and each pair fights one on one. Before giving the order to attack, Kim Yushin can rearrange his own line in any order he likes.

You are given the integers P14P_{14}, P15P_{15}, P16P_{16}, P24P_{24}, P25P_{25}, P26P_{26}, P34P_{34}, P35P_{35}, P36P_{36}. PijP_{ij} is the result of the duel when an officer of Kim Yushin carrying weapon ii meets an enemy officer carrying weapon jj. A positive value means a result in Kim Yushin's favor.

The result of the whole battle is the sum of the results of the single duels. Find the largest total battle result Kim Yushin can reach by rearranging his line.

Input

The first line contains the number of test cases TT. Each test case is given on the following four lines.

N A B C D E F
P14 P15 P16
P24 P25 P26
P34 P35 P36

Constraints

  • All values are integers.
  • 1≤T≤10001 \le T \le 1000
  • 1≤N≤10131 \le N \le 10^{13}
  • 0≤A,B,C,D,E,F≤N0 \le A, B, C, D, E, F \le N
  • A+B+C=NA + B + C = N
  • D+E+F=ND + E + F = N
  • −100000≤P14,P15,P16,P24,P25,P26,P34,P35,P36≤100000-100000 \le P_{14}, P_{15}, P_{16}, P_{24}, P_{25}, P_{26}, P_{34}, P_{35}, P_{36} \le 100000

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the largest total battle result Kim Yushin can reach.

Examples2

  1. Example 1

    Input
    2
    1 1 0 0 1 0 0
    1 2 3
    -4 -5 -6
    -7 -8 9
    3 1 1 1 1 1 1
    3 1 4
    1 5 9
    2 6 5
    
    Expected output
    Case #1: 1
    Case #2: 18
    
  2. Example 2

    Input
    3
    1 0 0 1 0 0 1
    0 0 0
    0 0 0
    0 0 -5
    4 4 0 0 0 0 4
    -100000 -100000 -100000
    7 7 7
    7 7 7
    5 2 2 1 1 3 1
    0 0 0
    0 0 0
    0 0 0
    
    Expected output
    Case #1: -5
    Case #2: -400000
    Case #3: 0