#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t, n, i, j;
  cin >> t;
  while (t--) {
    cin >> n;
    long long int arr[n + 2][n + 2];
    memset(arr, 0, sizeof(arr));
    for (i = 1; i <= n; i++) {
      arr[i][i] = 1;
      if (i == n)
        arr[i][1] = 1;
      else
        arr[i][i + 1] = 1;
    }
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        if (j == n)
          cout << arr[i][j] << endl;
        else
          cout << arr[i][j] << " ";
      }
    }
  }
  return 0;
}
