WOBURN CHALLENGE 1995
PEG contra PCS

Problem 3: Smallest Latin Square
(ICPSC 1981 #2)

INPUT FILE: test3.in
OUTPUT FILE: test3.out

A square arrangment of numbers such as

1 2 3 4 5
2 1 4 5 3
3 4 5 1 2
4 5 2 3 1
5 3 1 2 4

is a Latin Square because each whole number 1, 2, 3, 4 and 5 appears exactly once in each row and column of the square.

Of all the possible 5x5 Latin squares, the one above is the smallest in the following sense: if the digits are strung together (in rows from top to bottom) the resulting integer,

12345 21453 34512 45231 53124

is the smallest one possible.

INPUT
On the first line is M, an integer indicating the number of test cases.
On each of the next M lines is an integer N between 2 and 9.

OUTPUT
Output the smallest NxN latin square for each test case.

Sample Input File

1
5

Output for Sample Input

1 2 3 4 5
2 1 4 5 3
3 4 5 1 2
4 5 2 3 1
5 3 1 2 4
Downloader failed! Response object 006~ASP 0159~Buffering Off~Buffering must be on.