#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t N = 1e6 + 5;
bool isPrime(int64_t x) {
  if (x == 2) {
    return true;
  }
  for (int64_t i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int64_t t;
  cin >> t;
  while (t--) {
    int64_t n;
    cin >> n;
    int64_t pm = n + 1000;
    while (1) {
      if (isPrime(pm)) {
        if (!isPrime(pm - n + 1)) {
          break;
        } else {
          pm++;
        }
      } else {
        pm++;
      }
    }
    for (int64_t i = 0; i < n; i++) {
      for (int64_t j = 0; j < n; j++) {
        if (i == j) {
          cout << pm - n + 1 << " ";
        } else {
          cout << 1 << " ";
        }
      }
      cout << "\n";
    }
  }
  return 0;
}
