#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  bool prime[101];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= 100; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 100; i += p) prime[i] = false;
    }
  }
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    if (prime[n]) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; j++) cout << "1 ";
        cout << "\n";
      }
    } else {
      int prev = n - 1;
      while (prime[prev] == false) --prev;
      int ans[n];
      for (int i = 0; i < prev; ++i) ans[i] = 1;
      for (int i = prev; i < n; ++i) ans[i] = 0;
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; j++) cout << ans[(i + j) % n] << " ";
        cout << "\n";
      }
    }
  }
  return 0;
}
