#include #include #include #include #include #include #include #include int squareSol[10][10][10]; int rectSol[8][7][7]; void initializeSolutions() { squareSol[2][0][0] = 0; squareSol[2][0][1] = 1; squareSol[2][1][0] = 1; squareSol[2][1][1] = 1; squareSol[3][0][0] = 1; squareSol[3][0][1] = 1; squareSol[3][0][2] = 0; squareSol[3][1][0] = 1; squareSol[3][1][1] = 0; squareSol[3][1][2] = 2; squareSol[3][2][0] = 0; squareSol[3][2][1] = 2; squareSol[3][2][2] = 2; squareSol[4][0][0] = 1; squareSol[4][0][1] = 1; squareSol[4][0][2] = 2; squareSol[4][0][3] = 2; squareSol[4][1][0] = 1; squareSol[4][1][1] = 3; squareSol[4][1][2] = 3; squareSol[4][1][3] = 2; squareSol[4][2][0] = 4; squareSol[4][2][1] = 3; squareSol[4][2][2] = 0; squareSol[4][2][3] = 5; squareSol[4][3][0] = 4; squareSol[4][3][1] = 4; squareSol[4][3][2] = 5; squareSol[4][3][3] = 5; squareSol[5][0][0] = 1; squareSol[5][0][1] = 1; squareSol[5][0][2] = 2; squareSol[5][0][3] = 3; squareSol[5][0][4] = 3; squareSol[5][1][0] = 1; squareSol[5][1][1] = 2; squareSol[5][1][2] = 2; squareSol[5][1][3] = 3; squareSol[5][1][4] = 4; squareSol[5][2][0] = 0; squareSol[5][2][1] = 5; squareSol[5][2][2] = 5; squareSol[5][2][3] = 4; squareSol[5][2][4] = 4; squareSol[5][3][0] = 6; squareSol[5][3][1] = 5; squareSol[5][3][2] = 7; squareSol[5][3][3] = 7; squareSol[5][3][4] = 8; squareSol[5][4][0] = 6; squareSol[5][4][1] = 6; squareSol[5][4][2] = 7; squareSol[5][4][3] = 8; squareSol[5][4][4] = 8; squareSol[6][0][0] = 1; squareSol[6][0][1] = 1; squareSol[6][0][2] = 2; squareSol[6][0][3] = 3; squareSol[6][0][4] = 3; squareSol[6][0][5] = 4; squareSol[6][1][0] = 1; squareSol[6][1][1] = 2; squareSol[6][1][2] = 2; squareSol[6][1][3] = 3; squareSol[6][1][4] = 4; squareSol[6][1][5] = 4; squareSol[6][2][0] = 5; squareSol[6][2][1] = 5; squareSol[6][2][2] = 6; squareSol[6][2][3] = 7; squareSol[6][2][4] = 7; squareSol[6][2][5] = 8; squareSol[6][3][0] = 5; squareSol[6][3][1] = 6; squareSol[6][3][2] = 6; squareSol[6][3][3] = 7; squareSol[6][3][4] = 8; squareSol[6][3][5] = 8; squareSol[6][4][0] = 9; squareSol[6][4][1] = 9; squareSol[6][4][2] = 10; squareSol[6][4][3] = 11; squareSol[6][4][4] = 11; squareSol[6][4][5] = 12; squareSol[6][5][0] = 9; squareSol[6][5][1] = 10; squareSol[6][5][2] = 10; squareSol[6][5][3] = 11; squareSol[6][5][4] = 12; squareSol[6][5][5] = 12; squareSol[7][0][0] = 1; squareSol[7][0][1] = 1; squareSol[7][0][2] = 2; squareSol[7][0][3] = 3; squareSol[7][0][4] = 3; squareSol[7][0][5] = 4; squareSol[7][0][6] = 0; squareSol[7][1][0] = 1; squareSol[7][1][1] = 2; squareSol[7][1][2] = 2; squareSol[7][1][3] = 3; squareSol[7][1][4] = 4; squareSol[7][1][5] = 4; squareSol[7][1][6] = 8; squareSol[7][2][0] = 5; squareSol[7][2][1] = 5; squareSol[7][2][2] = 6; squareSol[7][2][3] = 7; squareSol[7][2][4] = 7; squareSol[7][2][5] = 8; squareSol[7][2][6] = 8; squareSol[7][3][0] = 5; squareSol[7][3][1] = 6; squareSol[7][3][2] = 6; squareSol[7][3][3] = 7; squareSol[7][3][4] = 9; squareSol[7][3][5] = 9; squareSol[7][3][6] = 10; squareSol[7][4][0] = 11; squareSol[7][4][1] = 11; squareSol[7][4][2] = 13; squareSol[7][4][3] = 13; squareSol[7][4][4] = 9; squareSol[7][4][5] = 10; squareSol[7][4][6] = 10; squareSol[7][5][0] = 12; squareSol[7][5][1] = 11; squareSol[7][5][2] = 14; squareSol[7][5][3] = 13; squareSol[7][5][4] = 15; squareSol[7][5][5] = 16; squareSol[7][5][6] = 16; squareSol[7][6][0] = 12; squareSol[7][6][1] = 12; squareSol[7][6][2] = 14; squareSol[7][6][3] = 14; squareSol[7][6][4] = 15; squareSol[7][6][5] = 15; squareSol[7][6][6] = 16; rectSol[2][0][0] = 1; rectSol[2][0][1] = 1; rectSol[2][0][2] = 2; rectSol[2][0][3] = 3; rectSol[2][0][4] = 3; rectSol[2][0][5] = 4; rectSol[2][1][0] = 1; rectSol[2][1][1] = 2; rectSol[2][1][2] = 2; rectSol[2][1][3] = 3; rectSol[2][1][4] = 4; rectSol[2][1][5] = 4; rectSol[3][0][0] = 1; rectSol[3][0][1] = 1; rectSol[3][0][2] = 2; rectSol[3][0][3] = 2; rectSol[3][0][4] = 3; rectSol[3][0][5] = 3; rectSol[3][1][0] = 1; rectSol[3][1][1] = 4; rectSol[3][1][2] = 2; rectSol[3][1][3] = 5; rectSol[3][1][4] = 3; rectSol[3][1][5] = 6; rectSol[3][2][0] = 4; rectSol[3][2][1] = 4; rectSol[3][2][2] = 5; rectSol[3][2][3] = 5; rectSol[3][2][4] = 6; rectSol[3][2][5] = 6; rectSol[4][0][0] = 1; rectSol[4][0][1] = 1; rectSol[4][0][2] = 2; rectSol[4][0][3] = 3; rectSol[4][0][4] = 3; rectSol[4][0][5] = 4; rectSol[4][1][0] = 1; rectSol[4][1][1] = 2; rectSol[4][1][2] = 2; rectSol[4][1][3] = 3; rectSol[4][1][4] = 4; rectSol[4][1][5] = 4; rectSol[4][2][0] = 5; rectSol[4][2][1] = 5; rectSol[4][2][2] = 6; rectSol[4][2][3] = 7; rectSol[4][2][4] = 7; rectSol[4][2][5] = 8; rectSol[4][3][0] = 5; rectSol[4][3][1] = 6; rectSol[4][3][2] = 6; rectSol[4][3][3] = 7; rectSol[4][3][4] = 8; rectSol[4][3][5] = 8; rectSol[5][0][0] = 1; rectSol[5][0][1] = 1; rectSol[5][0][2] = 2; rectSol[5][0][3] = 3; rectSol[5][0][4] = 3; rectSol[5][0][5] = 4; rectSol[5][1][0] = 1; rectSol[5][1][1] = 2; rectSol[5][1][2] = 2; rectSol[5][1][3] = 3; rectSol[5][1][4] = 4; rectSol[5][1][5] = 4; rectSol[5][2][0] = 5; rectSol[5][2][1] = 5; rectSol[5][2][2] = 6; rectSol[5][2][3] = 6; rectSol[5][2][4] = 7; rectSol[5][2][5] = 7; rectSol[5][3][0] = 5; rectSol[5][3][1] = 8; rectSol[5][3][2] = 6; rectSol[5][3][3] = 9; rectSol[5][3][4] = 7; rectSol[5][3][5] = 10; rectSol[5][4][0] = 8; rectSol[5][4][1] = 8; rectSol[5][4][2] = 9; rectSol[5][4][3] = 9; rectSol[5][4][4] = 10; rectSol[5][4][5] = 10; rectSol[6][0][0] = 1; rectSol[6][0][1] = 1; rectSol[6][0][2] = 2; rectSol[6][0][3] = 3; rectSol[6][0][4] = 3; rectSol[6][0][5] = 4; rectSol[6][1][0] = 1; rectSol[6][1][1] = 2; rectSol[6][1][2] = 2; rectSol[6][1][3] = 3; rectSol[6][1][4] = 4; rectSol[6][1][5] = 4; rectSol[6][2][0] = 5; rectSol[6][2][1] = 5; rectSol[6][2][2] = 6; rectSol[6][2][3] = 7; rectSol[6][2][4] = 7; rectSol[6][2][5] = 8; rectSol[6][3][0] = 5; rectSol[6][3][1] = 6; rectSol[6][3][2] = 6; rectSol[6][3][3] = 7; rectSol[6][3][4] = 8; rectSol[6][3][5] = 8; rectSol[6][4][0] = 9; rectSol[6][4][1] = 9; rectSol[6][4][2] = 10; rectSol[6][4][3] = 11; rectSol[6][4][4] = 11; rectSol[6][4][5] = 12; rectSol[6][5][0] = 9; rectSol[6][5][1] = 10; rectSol[6][5][2] = 10; rectSol[6][5][3] = 11; rectSol[6][5][4] = 12; rectSol[6][5][5] = 12; rectSol[7][0][0] = 1; rectSol[7][0][1] = 1; rectSol[7][0][2] = 2; rectSol[7][0][3] = 3; rectSol[7][0][4] = 3; rectSol[7][0][5] = 4; rectSol[7][1][0] = 1; rectSol[7][1][1] = 2; rectSol[7][1][2] = 2; rectSol[7][1][3] = 3; rectSol[7][1][4] = 4; rectSol[7][1][5] = 4; rectSol[7][2][0] = 5; rectSol[7][2][1] = 5; rectSol[7][2][2] = 6; rectSol[7][2][3] = 7; rectSol[7][2][4] = 7; rectSol[7][2][5] = 8; rectSol[7][3][0] = 5; rectSol[7][3][1] = 6; rectSol[7][3][2] = 6; rectSol[7][3][3] = 7; rectSol[7][3][4] = 8; rectSol[7][3][5] = 8; rectSol[7][4][0] = 9; rectSol[7][4][1] = 9; rectSol[7][4][2] = 10; rectSol[7][4][3] = 10; rectSol[7][4][4] = 11; rectSol[7][4][5] = 11; rectSol[7][5][0] = 9; rectSol[7][5][1] = 12; rectSol[7][5][2] = 10; rectSol[7][5][3] = 13; rectSol[7][5][4] = 11; rectSol[7][5][5] = 14; rectSol[7][6][0] = 12; rectSol[7][6][1] = 12; rectSol[7][6][2] = 13; rectSol[7][6][3] = 13; rectSol[7][6][4] = 14; rectSol[7][6][5] = 14; squareSol[9][0][0] = 1; squareSol[9][0][1] = 1; squareSol[9][0][2] = 2; squareSol[9][0][3] = 2; squareSol[9][0][4] = 3; squareSol[9][0][5] = 3; squareSol[9][0][6] = 4; squareSol[9][0][7] = 5; squareSol[9][0][8] = 5; squareSol[9][1][0] = 1; squareSol[9][1][1] = 6; squareSol[9][1][2] = 6; squareSol[9][1][3] = 2; squareSol[9][1][4] = 3; squareSol[9][1][5] = 4; squareSol[9][1][6] = 4; squareSol[9][1][7] = 7; squareSol[9][1][8] = 5; squareSol[9][2][0] = 10; squareSol[9][2][1] = 6; squareSol[9][2][2] = 8; squareSol[9][2][3] = 8; squareSol[9][2][4] = 11; squareSol[9][2][5] = 9; squareSol[9][2][6] = 9; squareSol[9][2][7] = 7; squareSol[9][2][8] = 7; squareSol[9][3][0] = 10; squareSol[9][3][1] = 10; squareSol[9][3][2] = 12; squareSol[9][3][3] = 8; squareSol[9][3][4] = 11; squareSol[9][3][5] = 11; squareSol[9][3][6] = 9; squareSol[9][3][7] = 13; squareSol[9][3][8] = 13; squareSol[9][4][0] = 14; squareSol[9][4][1] = 14; squareSol[9][4][2] = 12; squareSol[9][4][3] = 12; squareSol[9][4][4] = 15; squareSol[9][4][5] = 15; squareSol[9][4][6] = 16; squareSol[9][4][7] = 17; squareSol[9][4][8] = 13; squareSol[9][5][0] = 14; squareSol[9][5][1] = 18; squareSol[9][5][2] = 19; squareSol[9][5][3] = 19; squareSol[9][5][4] = 15; squareSol[9][5][5] = 16; squareSol[9][5][6] = 16; squareSol[9][5][7] = 17; squareSol[9][5][8] = 17; squareSol[9][6][0] = 18; squareSol[9][6][1] = 18; squareSol[9][6][2] = 19; squareSol[9][6][3] = 20; squareSol[9][6][4] = 20; squareSol[9][6][5] = 21; squareSol[9][6][6] = 21; squareSol[9][6][7] = 22; squareSol[9][6][8] = 22; squareSol[9][7][0] = 23; squareSol[9][7][1] = 24; squareSol[9][7][2] = 24; squareSol[9][7][3] = 25; squareSol[9][7][4] = 20; squareSol[9][7][5] = 21; squareSol[9][7][6] = 26; squareSol[9][7][7] = 22; squareSol[9][7][8] = 27; squareSol[9][8][0] = 23; squareSol[9][8][1] = 23; squareSol[9][8][2] = 24; squareSol[9][8][3] = 25; squareSol[9][8][4] = 25; squareSol[9][8][5] = 26; squareSol[9][8][6] = 26; squareSol[9][8][7] = 27; squareSol[9][8][8] = 27; } typedef long long llong; const int MAXN = 200 + 10; const int INF = 1e9 + 1; int t, n; int ans[MAXN][MAXN]; int renum[30]; int cnt; void placeSquare(int size, int from, int to) { std::fill(renum, renum + 30, 0); for (int i = 0 ; i < size ; ++i) { for (int j = 0 ; j < size ; ++j) { if (squareSol[size][i][j] != 0 && renum[squareSol[size][i][j]] == 0) { renum[squareSol[size][i][j]] = ++cnt; } ans[from + i][to + j] = renum[squareSol[size][i][j]]; } } } void placeRect(int size, int from, int to) { std::fill(renum, renum + 30, 0); for (int i = 0 ; i < size ; ++i) { for (int j = 0 ; j < 6 ; ++j) { if (rectSol[size][i][j] != 0 && renum[rectSol[size][i][j]] == 0) { renum[rectSol[size][i][j]] = ++cnt; } ans[from + i][to + j] = renum[rectSol[size][i][j]]; } } } void placeRectRev(int size, int from, int to) { std::fill(renum, renum + 30, 0); for (int i = 0 ; i < 6 ; ++i) { for (int j = 0 ; j < size ; ++j) { if (rectSol[size][j][i] != 0 && renum[rectSol[size][j][i]] == 0) { renum[rectSol[size][j][i]] = ++cnt; } ans[from + i][to + j] = renum[rectSol[size][j][i]]; } } } void solve() { cnt = 0; if (n == 1) { std::cout << 0 << '\n'; std::cout << 0 << '\n'; return; } if (n == 3) { std::cout << 2 << '\n'; std::cout << "1 1 2" << '\n'; std::cout << "1 2 2" << '\n'; std::cout << "0 0 0" << '\n'; return; } if (n % 6 == 3) { placeSquare(9, 1, 1); for (int i = 10 ; i <= n ; i += 6) { placeRect(3, 1, i); placeRect(6, 4, i); } for (int i = 10 ; i <= n ; i += 6) { placeRectRev(3, i, 1); placeRectRev(6, i, 4); } for (int i = 10 ; i <= n ; i += 6) { for (int j = 10 ; j <= n ; j += 6) { placeRect(6, i, j); } } } else { int size = n % 6; if (size == 0) size = 6; if (size == 1) size = 7; placeSquare(size, 1, 1); for (int i = size + 1 ; i <= n ; i += 6) { placeRect(size, 1, i); } for (int i = size + 1 ; i <= n ; i += 6) { placeRectRev(size, i, 1); } for (int i = size + 1 ; i <= n ; i += 6) { for (int j = size + 1 ; j <= n ; j += 6) { placeRect(6, i, j); } } } assert(3 * cnt == n * n - n * n % 3); std::cout << cnt << '\n'; for (int i = 1 ; i <= n ; ++i) { for (int j = 1 ; j <= n ; ++j) { std::cout << ans[i][j] << ' '; } std::cout << '\n'; } } void read() { std::cin >> n; } void fastIO() { freopen("figures.in", "r", stdin); freopen("figures.out", "w", stdout); std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIO(); std::cin >> t; initializeSolutions(); while (t--) { read(); solve(); } return 0; }