#include <bits/stdc++.h>
using namespace std;
void solution() {
  long long n;
  cin >> n;
  vector<vector<long long>> v(n, vector<long long>(n, 0));
  if (n % 2 != 0) {
    v[n - 1][(n - 1) / 2] = 1;
    v[(n - 1) / 2][n - 1] = 1;
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (i == j || i == n - j - 1) v[i][j] = 1;
      cout << v[i][j] << ' ';
    }
    cout << "\n";
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) solution();
}
