#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n][n];
    memset(arr, 0, sizeof(arr));
    int c = 0;
    for (int i = 0; i < n - 1; i++) arr[i][c] = arr[i][c + 1] = 1, c++;
    arr[n - 1][n - 1] = arr[n - 1][0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) cout << arr[i][j] << " ";
      cout << endl;
    }
  }
}
