Under construction

Still being built. Things may change

Battlefield (Small)

Time limit5sMemory limit512 MB

Summary
Permute your three troop types against fixed enemy counts to maximize the total pairwise battle score.
Level

Medium4 of 10

Topics
Brute force, Math
Solved
No attempts yet

Problem

After the Third World War almost all 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 across a battlefield.

General Kim Yushin commands NN generals. Among them AA carry armament 1, BB carry armament 2, and CC carry armament 3. They stand on a hill in one long row, side by side, looking at the enemy camp. General Kim Yushin was about to give the order to attack.

The enemy general also has NN generals. Among them DD carry armament 4, EE carry armament 5, and FF carry armament 6. They stand on the opposite hill in one long row and prepare to hold off the attack.

Just before giving the order, General Kim Yushin realized something. If the battle starts now, the generals meet in the formation they already stand in, so each pair fights one on one. If he changes the order of his row and puts an armament that suits the enemy armament in front of it, he gets a better battle result.

You are given the integers P14P_{14}, P15P_{15}, P16P_{16}, P24P_{24}, P25P_{25}, P26P_{26}, P34P_{34}, P35P_{35}, P36P_{36}. They describe the battle result when generals with the given armaments meet on the battlefield. For example, P14P_{14} is the battle result when a general of General Kim Yushin carrying armament 1 meets a general of the enemy carrying armament 4. A positive integer means a result in favor of General Kim Yushin.

The total battle result is the sum of the NN individual battle results. You assist General Kim Yushin and want to maximize that sum by rearranging his generals. Compute this maximum value.

Input

The first line contains the number of test cases TT.
Each test case is given as four lines in this format.

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

Constraints

  • All numbers are integers.
  • T≤1000T \le 1000
  • 1≤N≤1001 \le N \le 100
  • 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 case number starting from 1 and yy is the largest battle result General 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
    1
    2 1 1 0 1 1 0
    10 9 0
    10 0 0
    0 0 0
    
    Expected output
    Case #1: 19