#include <bits/stdc++.h>
using namespace std;
int t, n;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    int a = 0, b = 1;
    while (a < n) {
      for (int i = 0; i < n; i++) {
        if (i == a || i == b) {
          cout << 1 << ' ';
          continue;
        }
        cout << 0 << ' ';
      }
      cout << '\n';
      a++;
      b++;
      if (a == n - 1) {
        b = 0;
      }
    }
  }
}
