CF1816B.Grid Reconstruction

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Grid Reconstruction

Consider a 2×n2 \times n grid, where nn is an even integer. You may place the integers 1,2,,2n1, 2, \ldots, 2n on the grid, using each integer exactly once.

A path is a sequence of cells achieved by starting at (1,1)(1, 1), then repeatedly walking either downwards or to the right, and stopping when (2,n)(2, n) is reached. The path should not extend beyond the grid.

The cost of a path is the alternating sum of the numbers written on the cells in a path. That is, let the numbers written on the cells be a1,a2,,aka_1, a_2, \ldots, a_k (in the order that it is visited), the cost of the path is $a_1 - a_2 + a_3 - a_4 + \ldots = um_{i=1}^k a_i \cdot (-1)^{i+1}$.

Construct a way to place the integers 1,2,,2n1, 2, \ldots, 2n on the grid, such that the minimum cost over all paths from (1,1)(1, 1) to (2,n)(2, n) is maximized. If there are multiple such grids that result in the maximum value, output any of them.

Input

The first line contains a single integer tt (1t10001 \leq t \leq 1000) — the number of test cases. The description of test cases follows.

The first and the only line of each test case contains a single integer nn (2n1052 \leq n \leq 10^5, nn is even) — the number of the columns in the grid.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, output 22 lines, each containing nn integers — the desired grid. If there are multiple solutions, output any of them.

Note

In the first test case, there are only two paths from cell (1,1)(1, 1) to cell (2,2)(2, 2). Their costs are 31+4=63-1+4=6 and 32+4=53-2+4=5. Then the minimum cost is 55, which is the maximum possible value.

In the second test case, there are four paths from cell (1,1)(1, 1) to cell (2,4)(2, 4). Their costs are 81+53+7=168-1+5-3+7=16, 82+53+7=158-2+5-3+7=15, 82+63+7=168-2+6-3+7=16, and 82+64+7=158-2+6-4+7=15. Then the minimum value is 1515, which is the maximum possible value.

Samples

3
2
4
6
3 2
1 4
8 2 6 4
1 5 3 7
11 5 9 1 7 3
6 10 2 8 4 12

在线编程 IDE

建议全屏模式获得最佳体验