#P1716C. Robot in a Hallway

    ID: 7810 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>data structuresdpgreedyimplementationternary search*2000

Robot in a Hallway

No submission language available for this problem.

Description

There is a grid, consisting of 22 rows and mm columns. The rows are numbered from 11 to 22 from top to bottom. The columns are numbered from 11 to mm from left to right.

The robot starts in a cell (1,1)(1, 1). In one second, it can perform either of two actions:

  • move into a cell adjacent by a side: up, right, down or left;
  • remain in the same cell.

The robot is not allowed to move outside the grid.

Initially, all cells, except for the cell (1,1)(1, 1), are locked. Each cell (i,j)(i, j) contains a value ai,ja_{i,j} — the moment that this cell gets unlocked. The robot can only move into a cell (i,j)(i, j) if at least ai,ja_{i,j} seconds have passed before the move.

The robot should visit all cells without entering any cell twice or more (cell (1,1)(1, 1) is considered entered at the start). It can finish in any cell.

What is the fastest the robot can achieve that?

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains a single integer mm (2m21052 \le m \le 2 \cdot 10^5) — the number of columns of the grid.

The ii-th of the next 22 lines contains mm integers ai,1,ai,2,,ai,ma_{i,1}, a_{i,2}, \dots, a_{i,m} (0ai,j1090 \le a_{i,j} \le 10^9) — the moment of time each cell gets unlocked. a1,1=0a_{1,1} = 0. If ai,j=0a_{i,j} = 0, then cell (i,j)(i, j) is unlocked from the start.

The sum of mm over all testcases doesn't exceed 21052 \cdot 10^5.

For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains a single integer mm (2m21052 \le m \le 2 \cdot 10^5) — the number of columns of the grid.

The ii-th of the next 22 lines contains mm integers ai,1,ai,2,,ai,ma_{i,1}, a_{i,2}, \dots, a_{i,m} (0ai,j1090 \le a_{i,j} \le 10^9) — the moment of time each cell gets unlocked. a1,1=0a_{1,1} = 0. If ai,j=0a_{i,j} = 0, then cell (i,j)(i, j) is unlocked from the start.

The sum of mm over all testcases doesn't exceed 21052 \cdot 10^5.

Output

For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.

Samples

Sample Input 1

<div class="test-example-line test-example-line-even test-example-line-0">4</div><div class="test-example-line test-example-line-odd test-example-line-1">3</div><div class="test-example-line test-example-line-odd test-example-line-1">0 0 1</div><div class="test-example-line test-example-line-odd test-example-line-1">4 3 2</div><div class="test-example-line test-example-line-even test-example-line-2">5</div><div class="test-example-line test-example-line-even test-example-line-2">0 4 8 12 16</div><div class="test-example-line test-example-line-even test-example-line-2">2 6 10 14 18</div><div class="test-example-line test-example-line-odd test-example-line-3">4</div><div class="test-example-line test-example-line-odd test-example-line-3">0 10 10 10</div><div class="test-example-line test-example-line-odd test-example-line-3">10 10 10 10</div><div class="test-example-line test-example-line-even test-example-line-4">2</div><div class="test-example-line test-example-line-even test-example-line-4">0 0</div><div class="test-example-line test-example-line-even test-example-line-4">0 0</div><div class="test-example-line test-example-line-even test-example-line-4"></div>

Sample Output 1

5
19
17
3