#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test;
  cin >> test;
  while (test--) {
    int n;
    cin >> n;
    vector<vector<int>> v(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
      v[i][i] = 1;
      v[(i + 1) % n][i] = 1;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) cout << v[i][j] << " ";
      cout << "\n";
    }
  }
}
