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