#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e3 + 5;
const long long p = 31;
void solve() {
  long long n;
  cin >> n;
  long long a[n][n];
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) a[i][j] = 0;
  }
  for (long long i = 0; i < n; i++) {
    long long cnt = 0;
    for (long long j = i; j < n; j++) {
      a[i][j] = 1;
      cnt++;
      if (cnt == 2) break;
    }
  }
  a[n - 1][0] = 1;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      cout << a[i][j] << ' ';
    }
    cout << '\n';
  }
}
int32_t main() {
  long long t;
  cin >> t;
  while (t--) solve();
}
