#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  long long int v[n][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) v[i][j] = 0;
  for (int i = 0; i < n; i++) {
    v[i][i] = 1;
    if (i > 0) v[i][i - 1] = 1, v[i - 1][i] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cout << v[i][j] << " ";
    cout << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tst = 1;
  cin >> tst;
  while (tst--) solve();
  return 0;
}
