#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 11;
const int maxd = 1e3 + 21;
int dp[maxd][maxd][2];
int a[maxd][maxd];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (i == j)
          a[i][j] = 1;
        else if (i + j == n + 1)
          a[i][j] = 1;
        else
          a[i][j] = 0;
      }
    if (n % 2 == 1) a[1][n / 2 + 1] = 1, a[n / 2 + 1][1] = 1;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        cout << a[i][j] << " ";
        if (j == n) cout << endl;
      }
  }
}
