#include <bits/stdc++.h>
using namespace std;
const int N = 1009;
int n, a[N][N];
void init() { scanf("%d", &n); }
void work() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 0;
  for (int i = 1; i < n; i++) a[i][i] = a[i][i + 1] = 1;
  a[n][n] = a[n][1] = 1;
  for (int i = 1; i <= n; i++, puts(""))
    for (int j = 1; j <= n; j++) printf("%d ", a[i][j]);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) init(), work();
  return 0;
}
