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