#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  long long t, i, j, x, y;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long a[n][n];
    if (n == 1) {
      cout << "1" << endl;
    } else if (n == 2) {
      cout << "1 1" << endl;
      cout << "1 1" << endl;
      continue;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        a[i][j] = 0;
      }
    }
    x = 0;
    y = 1;
    a[n - 1][0] = 1;
    a[n - 1][n - 1] = 1;
    for (i = 0; i < n - 1; i++) {
      a[i][x] = 1;
      a[i][y] = 1;
      x++;
      y++;
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        cout << a[i][j] << " ";
      }
      cout << "\n";
    }
  }
  return 0;
}
