Under construction

Still being built. Things may change

City Tour

Time limit2sMemory limit512 MB

Summary
Find the minimum and maximum time of a tour from city 1 that visits every city once and uses each transportation means exactly once.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Albert plans to visit NN cities during his vacation. For convenience, the cities are numbered 1 to NN. The NN cities are connected by various transportation means such as trains, airplanes, cruise ships, and buses. There are also NN different transportation means, numbered 1 to NN for convenience.

Dk,i,jD_{k,i,j} is the time it takes to travel from city ii to city jj using the kk-th transportation means. If this value is 0, the means cannot be used to travel from ii to jj. It is always true that Dk,i,j=Dk,j,iD_{k,i,j} = D_{k,j,i}.

For example, let N=3N = 3, D1,⋅,⋅=[[0,1,3],[1,0,4],[3,4,0]]D_{1,\cdot,\cdot} = [[0, 1, 3], [1, 0, 4], [3, 4, 0]], D2,⋅,⋅=[[0,2,2],[2,0,4],[2,4,0]]D_{2,\cdot,\cdot} = [[0, 2, 2], [2, 0, 4], [2, 4, 0]], and D3,⋅,⋅=[[0,3,8],[3,0,4],[8,4,0]]D_{3,\cdot,\cdot} = [[0, 3, 8], [3, 0, 4], [8, 4, 0]].

  • D1,1,2=1D_{1,1,2} = 1, so traveling from city 1 to city 2 by the 1st means takes 1 unit of time.
  • D2,3,1=2D_{2,3,1} = 2, so traveling from city 3 to city 1 by the 2nd means takes 2 units of time.
  • D3,2,3=4D_{3,2,3} = 4, so traveling from city 2 to city 3 by the 3rd means takes 4 units of time.

While looking into various transportation options and schedules, Albert wanted to know the shortest and longest times for a route that starts at city 1, uses each transportation means exactly once, visits each city exactly once, and then returns to city 1.

In the example above, consider the route city 1 -> city 2 -> city 3 -> city 1.

  • Using the means in the order 1, 2, 3 takes D1,1,2+D2,2,3+D3,3,1=1+4+8=13D_{1,1,2} + D_{2,2,3} + D_{3,3,1} = 1 + 4 + 8 = 13 units of time.
  • Using the means in the order 1, 3, 2 takes D1,1,2+D3,2,3+D2,3,1=1+4+2=7D_{1,1,2} + D_{3,2,3} + D_{2,3,1} = 1 + 4 + 2 = 7 units of time. This is the shortest route in this example.
  • Using the means in the order 2, 1, 3 takes D2,1,2+D1,2,3+D3,3,1=2+4+8=14D_{2,1,2} + D_{1,2,3} + D_{3,3,1} = 2 + 4 + 8 = 14 units of time. This is the longest route in this example.
  • Other orders also let Albert use each means once, visit each city once, and return.

For this example, the answers are 7 and 14. Given NN and the 3D array DD, find the minimum and maximum times for such a tour.

Input

The first line contains the number of test cases TT.

Each test case starts with a line containing NN. The next N2N^2 lines each contain NN integers separated by spaces. The first NN lines give D1,⋅,⋅D_{1,\cdot,\cdot}, where the first of these lines is for i=1i = 1, the next is for i=2i = 2, and so on. The next NN lines give the times for the 2nd means, and this continues in order up to the NN-th means.

Output

For each test case, print the minimum and maximum time on one line, separated by a space. If no tour satisfies the conditions, print "0 0".

Constraints

  • 1≤T≤101 \le T \le 10
  • 2≤N≤82 \le N \le 8
  • For all 1≤i,j,k≤N1 \le i, j, k \le N: 0≤Dk,i,j≤1060 \le D_{k,i,j} \le 10^6
  • For all 1≤i,j,k≤N1 \le i, j, k \le N: Dk,i,j=Dk,j,iD_{k,i,j} = D_{k,j,i} is guaranteed.
  • For all 1≤i,k≤N1 \le i, k \le N: Dk,i,i=0D_{k,i,i} = 0 is guaranteed.

Examples1

  1. Example 1

    Input
    4
    2
    0 9
    9 0
    0 3
    3 0
    2
    0 0
    0 0
    0 1
    1 0
    3
    0 1 3
    1 0 4
    3 4 0
    0 2 2
    2 0 4
    2 4 0
    0 3 8
    3 0 4
    8 4 0
    4
    0 1 2 3
    1 0 4 5
    2 4 0 6
    3 5 6 0
    0 2 3 5
    2 0 7 11
    3 7 0 13
    5 11 13 0
    0 1 3 3
    1 0 7 0
    3 7 0 0
    3 0 0 0
    0 0 3 5
    0 0 7 9
    3 7 0 1
    5 9 1 0
    
    Expected output
    12 12
    0 0
    7 14
    10 26