#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t-- > 0) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if ((i == j || i + j == n - 1) || (n % 2 && i == n / 2 && j == 0) ||
            (n % 2 && j == n / 2 && i == 0))
          cout << "1"
               << " ";
        else
          cout << "0"
               << " ";
      }
      cout << endl;
    }
  }
  return 0;
}
