#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int Dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
  int t;
  cin >> t;
  vector<int> dp(1000, 0);
  dp[1] = 1;
  for (int i = 2; i <= 1000; i++) {
    if (dp[i] == 0) {
      for (int j = i * 2; j <= 1000; j += i) {
        dp[j] = 1;
      }
    }
  }
  while (t--) {
    int n, m;
    cin >> n;
    int find = n - 1;
    int j = 1;
    while (dp[j + find] == 1) {
      j++;
      while (!dp[j]) j++;
    }
    int q = j;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (n - j - 1 == i) {
          cout << q << " ";
        } else
          cout << "1"
               << " ";
      }
      cout << endl;
    }
  }
}
