#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, x;
    cin >> n;
    long long a[100][100];
    for (long long i = 0; i < n; i++) {
      for (long long j = i; j < i + n; j++) a[i][j % n] = (j - i <= 1 ? 1 : 0);
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) cout << a[i][j] << " ";
      cout << endl;
    }
  }
}
