#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1000000007;
const long long inf = (long long)2e18;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);
  long long t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<vector<long long>> grid(n, vector<long long>(n, 0));
    long long c = 0;
    for (long long i = 0; i < n; i++) {
      grid[i][c % n] = 1;
      grid[i][(c + 1) % n] = 1;
      c++;
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cout << grid[i][j] << " ";
      }
      cout << "\n";
    }
  }
  return 0;
}
