#include <bits/stdc++.h>
using namespace std;
long long int maxx(long long int a, long long int b) { return a > b ? a : b; }
long long int mod_exp(long long int a, long long int n, long long int mod) {
  if (n == 0) return 1;
  if (n % 2 == 0)
    return (mod_exp(a, n / 2, mod) * mod_exp(a, n / 2, mod)) % mod;
  else
    return (a * mod_exp(a, n - 1, mod)) % mod;
}
void solve() {
  int n;
  cin >> n;
  bool ans[n][n];
  memset(ans, 0, sizeof ans);
  for (int i = 0; i < n - 1; i++) {
    ans[i][i] = 1;
    ans[i][i + 1] = 1;
    ans[i + 1][i] = 1;
  }
  ans[n - 1][n - 1] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cout << ans[i][j] << " ";
    cout << endl;
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
