#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e9 + 7;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  cin >> t;
  long long int primes[] = {2,  3,  5,  7,  11, 13, 17, 19, 23, 29, 31, 37, 41,
                            43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
  while (t--) {
    long long int n;
    cin >> n;
    if (n % 2) {
      long long int val = -1;
      for (long long int i = 0; i < 25; i++) {
        if (n % primes[i] == 0) {
          val = primes[i];
          break;
        }
      }
      for (long long int i = 0; i < n; i++) {
        for (long long int j = 0; j < n; j++) {
          long long int x = (i / val) * val;
          if (j >= x && j < x + val)
            cout << 1 << " ";
          else
            cout << 0 << " ";
        }
        cout << endl;
      }
    } else {
      for (long long int i = 0; i < n; i++) {
        for (long long int j = 0; j < n; j++) {
          if (i == j)
            cout << 1 << " ";
          else if (i == (n - 1 - j))
            cout << 1 << " ";
          else
            cout << 0 << " ";
        }
        cout << endl;
      }
    }
  }
}
