#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tst;
  cin >> tst;
  while (tst--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (n & 1 and i == 0 and j == n / 2)
          cout << 1;
        else if (n & 1 and i == n - 1 and j == n / 2)
          cout << 1;
        else if (n & 1 and j == 0 and i == n / 2)
          cout << 1;
        else if (n & 1 and j == n - 1 and i == n / 2)
          cout << 1;
        else if (j == i or j == n - i - 1)
          cout << 1;
        else
          cout << 0;
        cout << " ";
      }
      cout << endl;
    }
  }
}
