#P13958. [ICPC 2023 Nanjing R] 谜题:问号

[ICPC 2023 Nanjing R] 谜题:问号

Description

XingHui is a puzzle master. Today, she is playing a puzzle called Filling with Question Marks\textit{Filling with Question Marks}. The puzzle contains a grid of nn rows and nn columns, and a handful of question mark\textit{question mark} pieces (QM pieces). A QM piece occupies 44 cells, as shown in the following figure.

:::align{center}

A QM piece (It looks like a question mark, I suppose?) :::

QM pieces must be placed entirely inside the grid and pieces can be rotated by a multiple of 9090 degrees and/or flipped. More precisely, there are 88 types of QM pieces, as shown below.

:::align{center} :::

Any two QM pieces cannot occupy the same cell. The goal of the puzzle is to find a way to place the maximum number of QM pieces on the n×nn \times n grid.

XingHui wonders if you could solve the puzzle successfully.

Input Format

There are multiple test cases. The first line of the input contains a single integer TT indicating the number of test cases. For each test case:

The first and only line contains one integer nn (1n2×1031 \leq n \leq 2 \times 10^3) indicating the size of the grid.

It is guaranteed that the sum of n2n^2 of all test cases will not exceed 5×1065 \times 10^6.

Output Format

For each test case:

First output one line containing one integer indicating the maximum number of QM pieces placed on the grid.

Then output nn lines. Each of these lines contains nn integers separated by a space. The jj-th integer of the ii-th line ai,ja_{i,j} indicates that the cell on the ii-th row and jj-th column belongs to the ai,ja_{i,j}-th QM piece. If ai,ja_{i,j} is 00, it indicates that the corresponding cell is empty and does not belong to any QM piece.

If there are multiple solutions, output any of them.

Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!

2
3
4
2
0 1 1
2 2 1
2 1 2
4
1 1 2 2
1 2 3 2
4 1 4 3
4 4 3 3

Hint

The sample test cases are shown below.

:::align{center}

:::