#include <bits/stdc++.h>
using namespace std;
void solve1();
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long power(long long x, long long y) {
  long long res = 1;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
long long fib(long long s1) {
  long long s2, s4, s5, s3;
  s2 = s3 = 1;
  s4 = s5 = 0;
  while (s1 > 0) {
    if (s1 % 2 == 0) {
      long long s6 = s3 * s3;
      s3 = 2 * s5 * s3 + s6;
      s5 = s5 * s5 + s6;
      s1 /= 2;
    } else {
      long long s6 = s2 * s3;
      s2 = s4 * s3 + s6 + s2 * s5;
      s4 = s4 * s5 + s6;
      s1 -= 1;
    }
  }
  return s4;
}
long long sum(long long arr[], long long a, long long n) {
  long long zz = 0;
  for (long long i = a; i < n; i++) {
    zz += arr[i];
  }
  return zz;
}
void solve() {
  long long t;
  cin >> t;
  while (t--) {
    solve1();
  }
}
bool allprime[10000001] = {0};
vector<long long> sieve_of_primes_upto_10e6() {
  long long n = 10000000;
  vector<long long> set_of_primes;
  set_of_primes.push_back(2);
  long long p;
  for (p = 2; p * p <= n; p++) {
    if (allprime[p] == 0) {
      set_of_primes.push_back(p);
      for (long long i = p * p; i <= n; i += p) {
        allprime[i] = 1;
      }
    }
  }
  for (p; p <= n; p += 2) {
    if (allprime[p] == 0) {
      set_of_primes.push_back(p);
    }
  }
  return set_of_primes;
}
signed main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  vector<long long> vec = sieve_of_primes_upto_10e6();
  solve();
  return 0;
}
void solve1() {
  long long n, m, a = 0, b;
  cin >> n;
  allprime[1] = 1;
  long long arr[n][n];
  for (long long i = n + 1; i < 200000; i++) {
    if (allprime[i] == 0 && allprime[i + 1 - n] == 1) {
      a = i;
      break;
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      arr[i][j] = 1;
    }
    arr[i][i] = a + 1 - n;
  }
  for (long long j = 0; j < n; j++) {
    for (long long i = 0; i < n; i++) cout << arr[j][i] << " ";
    cout << "\n";
    ;
  }
}
