#include <bits/stdc++.h>
using namespace std;
const long long int N = 100005;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int i, j, k, n, m, ans = 0, cnt = 0, sum = 0;
    cin >> n;
    long long int a[n + 1][n + 1];
    memset(a, 0, sizeof(a));
    i = 0, j = 0;
    while (i < n && j < n) {
      a[i][j] = 1;
      i++;
      j++;
    }
    i = 0, j = n - 1;
    while (i < n && j >= 0) {
      a[i][j] = 1;
      i++;
      j--;
    }
    if (n % 2 != 0) {
      a[0][n / 2] = 1;
      a[n / 2][0] = 1;
    }
    for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
      for (long long int j = (long long int)(0); j < (long long int)(n); j++) {
        cout << a[i][j] << " ";
      }
      cout << endl;
    }
  }
}
