#include <bits/stdc++.h>
const int N = (int)1e5 + 7;
const int MOD = (int)1e9 + 7;
const long long INF = (long long)1e18;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> A(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
      A[i][i] = 1;
      A[i][(i + 1) % n] = 1;
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) cout << A[i][j] << (j == n - 1 ? '\n' : ' ');
  }
  return 0;
}
