#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<vector<int>> ans(n, vector<int>(n));
  for (long long i = 0; i < n; i++) ans[i][i] = 1;
  long long c = n - 1;
  for (long long i = 0; i < n; i++) {
    ans[c][i] = 1;
    c -= 1;
  }
  if (n & 1) {
    ans[n / 2][0] = 1;
    ans[0][n / 2] = 1;
  }
  for (auto x : ans) {
    for (auto y : x) {
      cout << y << " ";
    }
    cout << endl;
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
