#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long arr[n][n];
    memset(arr, 0, sizeof arr);
    for (long long i = 0; i < n; i++) {
      arr[i][i] = 1;
      arr[i][n - i - 1] = 1;
    }
    if (n & 1) arr[n / 2 - 1][n / 2] = arr[n / 2][n / 2 - 1] = 1;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) cout << arr[i][j] << " ";
      cout << endl;
    }
  }
  return 0;
}
