#include <bits/stdc++.h>
using namespace std;
int t, n, sito[100005], x;
vector<int> primes;
int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(false);
  sito[1] = 1;
  for (int i = 4; i <= 10000; i += 2) {
    sito[i] = 1;
  }
  for (int i = 3; i <= 100; i++) {
    for (int j = i * i; j <= 10000; j += i) {
      sito[j] = 1;
    }
  }
  for (int i = 1; i <= 10000; i++) {
    if (sito[i] == 0) {
      primes.push_back(i);
    }
  }
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < primes.size(); i++) {
      if (primes[i] >= n and sito[primes[i] - n + 1] == 1) {
        x = primes[i] - n + 1;
        break;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) {
          cout << x << " ";
        } else {
          cout << 1 << " ";
        }
      }
      cout << endl;
    }
  }
  return 0;
}
