#include <bits/stdc++.h>
using namespace std;
bool isprime(int n) {
  bool x = true;
  if (n == 0 || n == 1) {
    x = false;
  } else {
    for (int i = 2; i <= n / 2; ++i) {
      if (n % i == 0) {
        x = false;
        break;
      }
    }
  }
  return x;
}
int nextPrime(int N) {
  if (N <= 1) return 2;
  int prime = N;
  bool found = false;
  while (!found) {
    prime++;
    if (isprime(prime)) found = true;
  }
  return prime;
}
void solve() {
  int n;
  cin >> n;
  if (isprime(n)) {
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cout << "1 ";
      }
      cout << "\n";
    }
    return;
  }
  int x = 1;
  while (!(isprime(x + n - 1) and !isprime(x))) {
    x++;
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (i == j)
        cout << x << ' ';
      else
        cout << "1 ";
    }
    cout << "\n";
  }
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
