#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 100005;
const double eps = 1e-8;
int n, m;
int cnt, judge[N], prime[N];
void is_primes(int n) {
  judge[0] = 1, judge[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (judge[i] == 0) prime[++cnt] = i;
    for (int j = 1; j <= cnt && prime[j] <= n / i; j++) {
      judge[prime[j] * i] = 1;
      if (i % prime[j] == 0) break;
    }
  }
}
int main() {
  int i, j;
  int T;
  cin >> T;
  is_primes(100000);
  while (T--) {
    cin >> n;
    int res;
    for (i = n;; i++) {
      if (judge[i] == 0) {
        int t = i - n + 1;
        if (judge[t] == 1) {
          res = t;
          break;
        }
      }
    }
    int ans;
    for (i = (n - 1) * res + 1;; i++) {
      if (judge[i] == 0) {
        int t = i - (n - 1) * res;
        if (judge[t] == 1) {
          ans = t;
          break;
        }
      }
    }
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        if (i == n && j == n) {
          cout << ans << " ";
        } else if (i == n || j == n) {
          cout << res << " ";
        } else {
          cout << 1 << " ";
        }
      }
      cout << endl;
    }
  }
  return 0;
}
