#include <bits/stdc++.h>
using namespace std;
void dbg(long long a) { cout << "Hello " << a << endl; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> grid(n, vector<int>(n, 1));
    set<int> primes;
    primes.insert(2);
    int my_x = -1;
    for (long long i = 3; i < 1001; i++) {
      bool flag = false;
      int mp = 0;
      for (long long j = 2; j < sqrt(i) + 1; j++) {
        if (i % j == 0) {
          flag = true;
          break;
        }
      }
      if (flag == false) {
        if (i >= n - 1) {
          int x = i - n + 1;
          if (primes.find(x) == primes.end()) {
            my_x = x;
            break;
          }
        }
        primes.insert(i);
      }
    }
    for (long long i = 0; i < n; i++) {
      grid[i][i] = my_x;
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cout << grid[i][j] << " ";
      }
      cout << endl;
    }
  }
  return 0;
}
