#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long a[n + 1][n + 1];
    if (n % 2 == 0) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (i == j || i + j == n + 1)
            a[i][j] = 1;
          else
            a[i][j] = 0;
        }
      }
    } else {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          if (i == j || i + j == n + 1)
            a[i][j] = 1;
          else
            a[i][j] = 0;
        }
      }
      a[n / 2 + 1][1] = 1;
      a[n][n / 2 + 1] = 1;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
}
