#include <bits/stdc++.h>
using namespace std;
set<int> prime;
const int mxn = 1e5 + 5;
bool composite[mxn] = {0};
void gensieve() {
  for (int i = 2; i <= mxn; i++) {
    if (composite[i]) continue;
    prime.insert(i);
    for (int j = 2 * i; j <= mxn; j += i) composite[j] = 1;
  }
}
void solve() {
  int n;
  cin >> n;
  if (prime.count(n)) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) cout << 1 << " ";
      cout << "\n";
    }
  } else if (n % 2 == 0) {
    for (int i = 0; i < n / 2; i++) {
      for (int j = 0; j < n; j++) {
        if (j == i * 2 || j == (i * 2 + 1))
          cout << 1 << " ";
        else
          cout << 0 << " ";
      }
      cout << "\n";
    }
    for (int i = n / 2; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (j == (n - i - 1) * 2 || j == ((n - i - 1) * 2 + 1))
          cout << 1 << " ";
        else
          cout << 0 << " ";
      }
      cout << "\n";
    }
  } else {
    int alpha = n - 1;
    for (int beta = 1;; beta++) {
      if (prime.count(beta)) continue;
      if (prime.count(alpha + beta)) {
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            if (i == j)
              cout << beta << " ";
            else
              cout << 1 << " ";
          }
          cout << "\n";
        }
        break;
      }
    }
  }
}
int main() {
  gensieve();
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
