#include <bits/stdc++.h>
static int getPrimes(int num, int* p, int* np) {
  int i = 0;
  int j = 0;
  for (int val = 2; val <= num; ++val) {
    int div = 2;
    bool isPrime = true;
    while (div * div <= val) {
      if (val % div == 0) {
        isPrime = false;
        break;
      } else {
        ++div;
      }
    }
    if (isPrime) {
      p[i++] = val;
    } else {
      np[j++] = val;
    }
  }
  return i;
}
int main() {
  int* p = new int[1229];
  int* np = new int[8770];
  getPrimes(10000, p, np);
  int t;
  std::cin >> t;
  while (t--) {
    int n;
    std::cin >> n;
    int x = np[0], y = np[0];
    int tmp = n - 1;
    int i = 0;
    while (p[i] <= tmp) {
      ++i;
    }
    bool flag = true;
    int j = 0;
    do {
      x = p[i++] - tmp;
      while (np[j] <= x) {
        if (np[j++] == x) {
          flag = false;
        }
      }
    } while (flag);
    tmp = x * (n - 1);
    while (p[i] <= tmp) {
      ++i;
    }
    flag = true;
    j = 0;
    do {
      y = p[i++] - tmp;
      while (np[j] <= y) {
        if (np[j++] == y) {
          flag = false;
        }
      }
    } while (flag);
    for (int i = 0; i != n * n; ++i) {
      if (i == n * n - 1) {
        std::cout << y << "\n";
      } else if (i % n == n - 1) {
        std::cout << x << "\n";
      } else if (i / n == n - 1) {
        std::cout << x << " ";
      } else {
        std::cout << 1 << " ";
      }
    }
  }
  delete[] p;
  delete[] np;
}
