CF BUDDY
← Problems·

1783B · Matrix of Differences

1100 · constructive algorithms, math

Problem: For a square matrix of integers of size n×nn \times n, let's define its beauty as follows: for each pair of side-adjacent elements xx and yy, write out the number xy|x-y|, and then find the number of different numbers among them.

For example, for the matrix (1342)\begin{pmatrix} 1 & 3\\ 4 & 2 \end{pmatrix} the numbers we consider are 13=2|1-3|=2, 14=3|1-4|=3, 32=1|3-2|=1 and 42=2|4-2|=2; there are 33 different numbers among them (22, 33 and 11), which means that its beauty is equal to 33.

You are given an integer nn. You have to find a matrix of size n×nn \times n, where each integer from 11 to n2n^2 occurs exactly once, such that its beauty is the maximum possible among all such matrices.

Input Format: The first line contains a single integer tt (1t491 \le t \le 49) – the number of test cases.

The first (and only) line of each test case contains a single integer nn (2n502 \le n \le 50).

Output Format: For each test case, print nn rows of nn integers — a matrix of integers of size n×nn \times n, where each number from 11 to n2n^2 occurs exactly once, such that its beauty is the maximum possible among all such matrices. If there are multiple answers, print any of them.

Sample Cases

Case 1

Input

2
2
3

Output

1 3
4 2
1 3 4
9 2 7
5 8 6

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback