#include <bits/stdc++.h>
using namespace std;
int T, n, x, m, tem;
bool flag;
int main() {
  scanf("%d", &T);
  while (T--) {
    cin >> n;
    flag = true;
    for (int i = 2; i * i <= n; i++)
      if (n % i == 0) flag = false;
    if (flag) {
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) cout << "1 ";
        cout << endl;
      }
    } else {
      for (int k = n + 1; k <= 100010; k++) {
        flag = true;
        for (int i = 2; i * i <= k; i++)
          if (k % i == 0) flag = false;
        if (flag) {
          tem = k - n + 1;
          flag = true;
          for (int i = 2; i * i <= tem; i++)
            if (tem % i == 0) flag = false;
          if (!flag) break;
        }
      }
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
          if (i != j)
            cout << "1 ";
          else
            cout << tem << " ";
        cout << endl;
      }
    }
  }
  return 0;
}
