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