#include <bits/stdc++.h>
using namespace std;
long long n, t, m, k, i;
bool prim[100005];
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    long long l = 1, r = 2;
    for (i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (l <= j && j <= r || (i == n && (j == n || j == 1)))
          cout << "1 ";
        else
          cout << "0 ";
      }
      l++;
      r++;
      if (r == n + 1) r = 1;
      cout << "\n";
    }
  }
  return 0;
}
