#include <bits/stdc++.h>
using namespace std;
int num[100][100];
int main() {
  int t;
  cin >> t;
  while (t--) {
    memset(num, 0, sizeof num);
    int a;
    cin >> a;
    for (int i = 1; i <= a; i++) {
      num[i][i] = 1;
      num[i][a - i + 1] = 1;
    }
    if (a % 2 != 0) {
      num[a / 2 + 1][a] = 1;
      num[a][a / 2 + 1] = 1;
    }
    for (int i = 1; i <= a; i++) {
      for (int j = 1; j <= a; j++) {
        cout << num[i][j] << " ";
      }
      cout << endl;
    }
  }
  return 0;
}
