#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n + 1][n + 1];
    memset(a, 0, sizeof(a));
    for (int i = 1, k = 1; k <= n / 2; i += 2, k++) {
      a[i][i] = 1;
      a[i][i + 1] = 1;
      a[i + 1][i] = 1;
      a[i + 1][i + 1] = 1;
    }
    if (n % 2) {
      a[n][n] = 4;
      a[n][n - 1] = 1;
      a[n - 1][n] = 1;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) cout << a[i][j] << " ";
      cout << '\n';
    }
  }
  return 0;
}
