import java.util.*;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
int n = sc.nextInt();
int[][] snail = new int[n][n];
snailNum(1, 0, n, snail);
System.out.println("#"+test_case);
for (int i = 0; i < snail.length; i++) {
for (int j = 0; j < snail.length; j++) {
System.out.printf("%2d ",snail[i][j]);
}
System.out.println();
}
}
}
static void snailNum(int k, int count, int n, int[][] snail) {
if (k >= n * n) {
if (n % 2 == 1)
snail[count][count] = k;
return;
}
for (int j = count; j < n - 1 - count; j++) {
snail[count][j] = k++;
}
for (int j = count; j < n - 1 - count; j++) {
snail[j][n - 1 - count] = k++;
}
for (int j = count; j < n - 1 - count; j++) {
snail[n - 1 - count][n - 1 - j] = k++;
}
for (int j = count; j < n - 1 - count; j++) {
snail[n - 1 - j][count] = k++;
}
snailNum(k, ++count, n, snail);
}
}