#include <bits/stdc++.h>
using namespace std;
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int prime;
    for (int i = 3; i <= 1000; i++) {
      int k = ((n - 1) * i) + 1;
      int l = (n - 1) + i;
      bool tempa = true;
      bool tempb = false;
      for (int j = 2; j * j <= k; j++) {
        if (((k % j) == 0)) {
          tempa = false;
          break;
        }
        if (j < i && ((i % j) == 0)) tempb = true;
        if (j < l && ((l % j) == 0)) {
          tempa = false;
          break;
        }
      }
      if (tempa && tempb) {
        prime = i;
        break;
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if ((i == n - 1 || j == n - 1) && !(i == n - 1 && j == n - 1))
          cout << "1 ";
        else
          cout << prime << " ";
      }
      cout << endl;
    }
  }
}
