#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 29;
int t, n;
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      if (i == n - 1) {
        cout << 1 << " ";
        for (int j = 0; j < n - 2; j++) cout << 0 << " ";
        cout << 1 << endl;
      } else {
        for (int j = 0; j < i; j++) cout << 0 << " ";
        cout << "1 1 ";
        for (int j = 0; j < n - i - 2; j++) cout << 0 << " ";
        cout << endl;
      }
    }
  }
  return 0;
}
