#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  if (n % 2 == 0) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j || i + j == n - 1)
          cout << 1 << " ";
        else
          cout << 0 << " ";
      }
      cout << "\n";
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j || i + j == n - 1 ||
            (i == (((n - 1) / 2) - 1) && j == n / 2) ||
            (i == n / 2 && j == (n / 2) + 1))
          cout << 1 << " ";
        else
          cout << 0 << " ";
      }
      cout << "\n";
    }
  }
  cout << "\n";
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
