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