#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<bool> prime(100000, 1);
  prime[0] = 0;
  prime[1] = 0;
  prime[2] = 1;
  for (long long int i = 2; i < sqrt(100000); i++) {
    if (prime[i] == 0) continue;
    for (long long int j = i; j * i < 100000; j++) {
      prime[i * j] = 0;
    }
  }
  vector<long long int> store;
  for (long long int i = 0; i < 100000; i++) {
    if (prime[i]) {
      store.push_back(i);
    }
  }
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int show;
    long long int se = store.size() - 1;
    show = store[se] - (n - 1);
    while (prime[show] != 0) {
      se--;
      show = store[se] - (n - 1);
    }
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        if (i == j) {
          cout << show;
        } else {
          cout << 1;
        }
        cout << " ";
      }
      cout << endl;
    }
  }
  return 0;
}
