#include <bits/stdc++.h>
using namespace std;
bool pd(long long a) {
  if (a == 1) return 0;
  if (a == 2 || a == 3) return 1;
  if (a % 6 != 1 && a % 6 != 5) return 0;
  long long n = sqrt(a);
  for (long long i = 5; i <= n; i += 6) {
    if (a % i == 0 || a % (i + 2) == 0) return 0;
  }
  return 1;
}
int main() {
  int N;
  cin >> N;
  while (N--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n * n; ++i) {
    }
    int a;
    for (int i = n;; ++i) {
      if (pd(i)) {
        a = i - n + 1;
        if (pd(a)) continue;
        break;
      }
    }
    for (int i = 1; i <= n - 1; ++i) {
      for (int j = 1; j <= n - 1; ++j) {
        cout << "1 ";
      }
      cout << a;
      putchar('\n');
    }
    cout << a;
    for (int i = 2; i <= n - 1; ++i) {
      cout << " " << a;
    }
    int b;
    for (int i = a * (n - 1) + 1;; ++i) {
      if (pd(i)) {
        b = i - a * (n - 1);
        if (pd(b)) continue;
        break;
      }
    }
    cout << " " << b << endl;
  }
}
