Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. 

A square of size n × n is called prime if the following three conditions are held simultaneously: 

  * all numbers on the square are non-negative integers not exceeding 10^5; 
  * there are no prime numbers in the square; 
  * sums of integers in each row and each column are prime numbers. 



Sasha has an integer n. He asks you to find any prime square of size n × n. Sasha is absolutely sure such squares exist, so just help him!

Input

The first line contains a single integer t (1 ≤ t ≤ 10) — the number of test cases.

Each of the next t lines contains a single integer n (2 ≤ n ≤ 100) — the required size of a square.

Output

For each test case print n lines, each containing n integers — the prime square you built. If there are multiple answers, print any.

Example

Input


2
4
2


Output


4 6 8 1
4 9 9 9
4 10 10 65
1 4 4 4
1 1
1 1

Source: codeforces | Difficulty: EASY
Tags: Mathematics

Constructive algorithms


Solutions:
t = int(input())
for _ in range(t):
	n = int(input())
	for j in range(n):
		b = [0] * n
		b[j] = 1
		b[-j - 1] = 1
		if j != n - 1 and j != n // 2:
			print(*b)
		elif n % 2 == 0:
			print(*b)
		else:
			if j == n - 1:
				b[n // 2] = 1
				print(*b)
			if j == n // 2:
				b[n // 2 - 1] = 1
				print(*b)


t = int(input())
import math

def sieve(n):
	ass = []
	is_prime = [True] * (n + 1)
	is_prime[0] = False
	is_prime[1] = False
	for i in range(2, int(math.sqrt(n)) + 1):
		if not is_prime[i]:
			continue
		for j in range(i * 2, n + 1, i):
			is_prime[j] = False
	for i in range(n + 1):
		if is_prime[i]:
			ass.append(i)
	return ass

def is_prime(n):
	if n == 1:
		return False
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
S = sieve(10 ** 5)
T = set(S)
for _ in range(t):
	n = int(input())
	if n in T:
		ans = [[1] * n for _ in range(n)]
		for i in range(n):
			print(*ans[i])
	else:
		ans = [[1] * n for _ in range(n)]
		for y in range(1, 10 ** 5 + 1):
			if y not in T and is_prime(n - 1 + y):
				break
		for x in range(1, 10 ** 5 + 1):
			if x not in T and is_prime((n - 1) * y + x):
				break
		for i in range(n - 1):
			ans[i][n - 1] = y
		for i in range(n - 1):
			ans[n - 1][i] = y
		ans[n - 1][n - 1] = x
		for i in range(n):
			print(*ans[i])


import sys
input = sys.stdin.buffer.readline
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().decode().rstrip('\n\r')
wrt = sys.stdout.write
pr = lambda *args, end='\n': wrt(' '.join([str(x) for x in args]) + end)
xrange = lambda args: reversed(range(args))
cdiv = lambda x, y: -(-x // y)
enum = enumerate
inf = float('inf')
mod = 10 ** 9 + 7
from collections import defaultdict as dd
Cases = ri()
for Case in range(Cases):
	n = ri()
	a = [1, 1] + [0] * (n - 2)
	for i in range(n):
		for j in range(n):
			pr(a[(i + j) % n], end=' \n'[j == n - 1])


def isprime(n):
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
t = int(input())
for i in range(t):
	n = int(input())
	for el in range(n, 10 ** 6):
		if isprime(el) and (not isprime(el - (n - 1))):
			dia = el - (n - 1)
			break
	for x in range(n):
		start = '1 ' * x
		end = ' 1' * (n - x - 1)
		s = start + str(dia) + end
		print(s)


T = int(input())
while T > 0:
	n = int(input())
	start = 0
	for i in range(n):
		temp = [0] * n
		temp[start] = 1
		temp[(start + 1) % n] = 1
		start += 1
		print(*temp)
	T -= 1


def is_prime(n):
	if n < 2:
		return False
	if n == 2:
		return True
	if n % 2 == 0:
		return False
	for i in range(3, n, 2):
		if n % i == 0:
			return False
	return True

def find_first_prime_maker(n):
	p = 1
	while is_prime(p) or not is_prime(p + n - 1):
		p += 1
	return p
t = int(input())
for i in range(t):
	n = int(input())
	p = find_first_prime_maker(n)
	for j in range(n):
		for k in range(n):
			if k == j:
				print(p, end=' ')
			else:
				print(1, end=' ')
		print()


from collections import Counter
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
c = Counter(primes)
for i in range(int(input())):
	n = int(input())
	cur = 0
	prime = 0
	for i in primes:
		if i - (n - 1) > 0:
			if i - (n - 1) not in c:
				prime = i
				break
	arr = [[1 for i in range(n)] for i in range(n)]
	for i in range(n):
		arr[i][i] = prime - (n - 1)
	for i in arr:
		print(*i)


arr100 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(int(input())):
	n = int(input())
	adg = 1
	if n in arr100:
		lis = ['1'] * n
		for k in range(n):
			print(*lis)
	else:
		for kk in arr100:
			if kk > n:
				adg = kk - n
				if adg + 1 not in arr100:
					break
		for k in range(n):
			lis = ['1'] * n
			lis[k] = str(adg + 1)
			print(*lis)


def prime(n):
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	a = -1
	for i in range(1, 101):
		if prime(n - 1 + i) and (not prime(i)):
			a = i
			break
	for i in range(n):
		for j in range(n):
			if i == j:
				print(a, end=' ')
			else:
				print(1, end=' ')
		print()


def is_prime(num):
	if num == 1:
		return False
	for i in range(2, num):
		if num // i == num / i:
			return False
	return True

def to_prime(num):
	found = 0
	for i in range(num, 100000):
		if is_prime(i + num) and (not is_prime(i)):
			found = i
			break
	return found
t = int(input())
while t:
	t -= 1
	n = int(input())
	matrix = []
	x = to_prime(n - 1)
	for i in range(n - 1):
		temp = []
		for j in range(n - 1):
			temp.append(1)
		matrix.append(temp + [x])
	y = to_prime(x * (n - 1))
	matrix.append([x] * (n - 1) + [y])
	for i in matrix:
		print(*i)


x = int(input())

def is_prime(n):
	if n == 2 or n == 3:
		return True
	if n < 2 or n % 2 == 0:
		return False
	if n < 9:
		return True
	if n % 3 == 0:
		return False
	r = int(n ** 0.5)
	f = 5
	while f <= r:
		if n % f == 0:
			return False
		if n % (f + 2) == 0:
			return False
		f += 6
	return True
for number in range(0, x):
	num = int(input())
	square = []
	for row in range(0, num):
		ones = []
		for column in range(0, num):
			ones.append(1)
		square.append(ones)
	for row in range(0, num):
		rowAdded = 0
		columnAdded = 0
		for column in range(0, num):
			rowAdded += square[row][column]
			columnAdded += square[column][row]
		while is_prime(rowAdded) == False:
			notFound = True
			count = square[row][row]
			while notFound:
				if is_prime(count + 1) == False:
					notFound = False
				count += 1
			square[row][row] = count
			rowAdded = 0
			for column in range(0, num):
				rowAdded += square[row][column]
	for row in range(0, num):
		rowString = ''
		for column in range(0, num):
			rowString = rowString + str(square[row][column]) + ' '
		print(rowString)


def rotate(l):
	z = [l[-1]]
	return z + l[:-1]
for i in range(int(input())):
	n = int(input())
	if n == 2:
		print('1 1\n1 1\n')
	else:
		l = [1, 1]
		for i in range(n - 2):
			l.append(0)
		for i in range(n):
			print(*l)
			l = rotate(l)


for i in range(int(input())):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
	else:
		a = []
		for i in range(n):
			a1 = [0 for j in range(n)]
			a.append(a1)
		for i in range(n):
			for j in range(n):
				if i == j:
					a[i][j] = 1
					a[i][n - i - 1] = 1
		if n % 2 != 0:
			a[0][n // 2] = 1
			a[n - 1][n // 2] = 1
			a[n // 2][0] = 1
			a[n // 2][n - 1] = 1
		for i1 in a:
			print(*i1)


from sys import stdin
input = stdin.readline

def A():
	t = int(input())
	for __ in range(t):
		(n, m) = map(int, input().split())
		a = list(map(int, input().split()))
		if sum(a) == m:
			print('YES')
		else:
			print('NO')

def B():
	t = int(input())
	for __ in range(t):
		n = int(input())
		x = [[0 for j in range(n)] for i in range(n)]
		for i in range(n // 2):
			x[i][i] = 1
			x[i][n - 1 - i] = 1
			x[n - 1 - i][i] = 1
			x[n - 1 - i][n - 1 - i] = 1
		if n % 2 == 1:
			x[n // 2 - 1][n // 2] = 1
			x[n // 2 + 1][n // 2] = 1
			x[n // 2][n // 2 + 1] = 1
			x[n // 2][n // 2 - 1] = 1
		for i in range(n):
			print(*x[i])

def C():
	t = int(input())
	for __ in range(t):
		_ = 0

def D():
	t = int(input())
	for __ in range(t):
		_ = 0
B()


def nextNotPrimeOf(num):
	for i in range(num + 1, 10000):
		if not isPrime(i):
			return i

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
for i in range(t):
	num = int(input())
	if isPrime(num):
		for i in range(num):
			for j in range(num):
				print(1, end=' ')
			print('')
	else:
		for i in range(1, 10000):
			testnum = nextNotPrimeOf(i)
			if isPrime(num - 1 + testnum) and isPrime(testnum * (num - 1) + 1):
				for i in range(num - 1):
					for j in range(num - 1):
						print(1, end=' ')
					print(testnum)
				for i in range(num - 1):
					print(testnum, end=' ')
				print(1)
				break


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math
from collections import defaultdict
for _ in range(ri()):
	n = ri()
	ans = []
	for i in range(n):
		x = []
		for j in range(n):
			x.append(0)
		ans.append(x)
	k = n - 1
	j = 0
	for i in range(n):
		ans[i][j] = ans[i][k] = 1
		if i >= 1 and i & 1:
			j += 1
			k -= 1
	if n & 1:
		ans[n - 1][n // 2] = 1
		ans[0][n // 2] = 4
		ans[0][n - 1] = 0
		ans[n - 1][n - 1] = 1
	for i in ans:
		wia(i)


for q in range(int(input())):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
		continue
	p = 211
	b = [0] * (n - 2)
	b.append(1)
	b.append(p - 1)
	for i in range(n):
		print(*b[i:] + b[0:i])


import math

def is_prime(nn):
	if ii in prime_numbers:
		return True
	if nn == 1:
		return False
	elif nn == 2:
		prime_numbers.append(nn)
		return True
	else:
		if nn % 2 == 0:
			return False
		sqr = int(math.sqrt(nn)) + 1
		for divisor in range(3, sqr, 2):
			if nn % divisor == 0:
				return False
		prime_numbers.append(nn)
		return True
prime_numbers = []
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
		continue
	ii = 1
	ll = []
	tmp = []
	for j in range(n):
		while is_prime(ii):
			ii += 1
		tmp.append(ii)
		if j == n - 1:
			while is_prime(sum(tmp)) is False:
				ii += 1
				while is_prime(ii):
					ii += 1
				tmp[-1] = ii
	for i in range(n):
		ll.append(tmp.copy())
	for i in range(n):
		ii = ll[-1][i]
		while is_prime(sum([ll[ii][i] for ii in range(n)])) is False:
			ii += 1
			while is_prime(ii):
				ii += 1
			ll[-1][i] = ii
	if is_prime(sum(ll[-1])) is False:
		ii = ll[-1][-1]
		while is_prime(sum(ll[-1])) is False or is_prime(sum([ll[jj][-1] for jj in range(n)])):
			ii += 1
			if is_prime(ii):
				ii += 1
				continue
			ll[-1][-1] = ii
	for i in range(n):
		for j in range(n):
			print(ll[i][j], end=' ')
		print()


def foo1(x):
	z = pow(x, 0.5)
	c = 0
	for i in range(1, int(z) + 1):
		if x % i == 0:
			c += 1
	if c != 1:
		return 1
	return 0

def foo(x):
	x += 1
	z = pow(x, 0.5)
	while 1:
		z = pow(x, 0.5)
		c = 0
		for i in range(1, int(z) + 1):
			if x % i == 0:
				c += 1
		if c == 1:
			break
		x += 1
	return x
t = int(input())
for t1 in range(0, t):
	n = int(input())
	z = n - 1
	r = n - 1
	while 1:
		if foo1(r) == 0:
			r += 1
			continue
		s = foo1(r + z)
		if s == 0:
			break
		r += 1
	last = r * (n - 1)
	k = last
	while 1:
		if foo1(k) == 0:
			k += 1
			continue
		s = foo1(last + k)
		if s == 0:
			break
		k += 1
	ans = []
	for i in range(0, n - 1):
		for j in range(0, n - 1):
			print(1, end=' ')
		print(r, end=' ')
		print()
	for i in range(0, n - 1):
		print(r, end=' ')
	print(k, end=' ')
	print()


def solve(n):
	mat = []
	for i in range(n):
		r = []
		for j in range(n):
			if i == j or i + j == n - 1:
				r.append(1)
			else:
				r.append(0)
		mat.append(r)
	if n % 2 != 0:
		for i in range(n // 2 - 1, n // 2 + 2):
			for j in range(n // 2 - 1, n // 2 + 2):
				mat[i][j] = 1
	return mat
for _ in range(int(input())):
	n = int(input())
	res = solve(n)
	for j in res:
		print(*j)


def prime(n):
	np = []
	isprime = []
	for i in range(n + 1, n + 200):
		np.append(i)
	for j in np:
		val_is_prime = True
		for x in range(2, j - 1):
			if j % x == 0:
				val_is_prime = False
				break
		if val_is_prime:
			isprime.append(j)
	return min(isprime)

def is_prime(n):
	if n <= 1:
		return False
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for i in range(int(input())):
	n = int(input())
	p1 = prime(n - 1)
	while is_prime(p1 - (n - 1)):
		p1 = prime(p1)
	for i in range(n - 1):
		print('1 ' * (n - 1) + str(p1 - (n - 1)))
	p2 = prime(p1 * (n - 1))
	while is_prime(p2 - (p1 - (n - 1)) * (n - 1)):
		p2 = prime(p2)
	print((str(p1 - (n - 1)) + ' ') * (n - 1) + str(p2 - (p1 - (n - 1)) * (n - 1)))


def main(n):
	data = []
	for i in range(n):
		data.append([0] * n)
	for i in range(n):
		data[i][i] = 1
	for i in range(n - 1, -1, -1):
		data[n - i - 1][i] = 1
	if n % 2:
		data[(n + 1) // 2 - 1][0] = 1
		data[-1][(n + 1) // 2 - 1] = 1
	for i in data:
		print(*i)
n = int(input())
for i in range(n):
	x = int(input())
	main(x)


t = int(input())
for q in range(0, t):
	n = int(input())
	flag = 0
	for i in range(2, n):
		if n % i == 0:
			flag = 1
			break
	if flag == 0:
		ans = []
		for i in range(0, n):
			for j in range(0, n):
				print(1, end=' ')
			print('\n')
	else:
		m1 = 6 * (n - 1) + 1
		m2 = 6 * (n - 1) - 1
		flag = 0
		for i in range(2, m1):
			if m1 % i == 0:
				flag = 1
				break
		if flag == 0:
			ans = m1
		else:
			ans = m2
		for i in range(0, n):
			for j in range(0, n):
				if i == n - 1 and j == 0:
					print(1, end=' ')
				elif i == j or i + 1 == j:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print('\n')


for i in range(int(input())):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
	elif n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i == j:
					print(1, end=' ')
				elif abs(i + j) == n - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				if i == n // 2 and j == 0:
					print(1, end=' ')
				elif j == n // 2 and i == 0:
					print(1, end=' ')
				elif i == j:
					print(1, end=' ')
				elif abs(i + j) == n - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()


game = int(input())
l = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999]
for i in range(game):
	n = int(input())
	l2 = [1] * (n - 1)
	for value in l:
		if value - (n - 1) not in l and value - (n - 1) > 0:
			final = value - (n - 1)
			break
	for value in l:
		if value - final * (n - 1) not in l and value - (n - 1) * final > 0:
			final1 = value - final * (n - 1)
			break
	l3 = [final] * (n - 1)
	for i in range(n):
		if i != n - 1:
			print(*l2, final)
		else:
			print(*l3, final1)


t = int(input())
for i in range(t):
	n = int(input())
	u = [1, 1]
	v = [0]
	for i in range(n - 1):
		z = [0] * i + u + [0] * (n - i - 2)
		print(*z)
	w = [1] + [0] * (n - 2) + [1]
	print(*w)


import math

def prime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	l = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
	for i in l:
		if prime(i + 1 - n) == False:
			e = i + 1
			break
	a = [[1 for i in range(n)] for j in range(n)]
	for i in range(n):
		a[i][i] = e - n
		print(*a[i])


import math
for _ in range(int(input())):
	n = int(input())
	a = [0 for i in range(n - 2)]
	a.append(1)
	a.append(1)
	b = []
	for i in range(n):
		b.append(a)
		a = a[1:] + a[:1]
	for i in b:
		print(*i)


def is_prime(n):
	for i in range(3, n):
		if n % i == 0:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	el = [1] + [4] * (n - 2)
	s = sum(el)
	current = 4
	while not is_prime(s + current):
		current += 2
	el.append(current)
	for x in range(n):
		o = el[x:] + el[:x]
		print(*o)


t = int(input())
for i in range(t):
	n = int(input())
	for j in range(1, n + 1):
		for k in range(1, n + 1):
			if k == j or j + k == n + 1 or (n % 2 == 1 and j == n // 2 and (k == n // 2 + 1)) or (n % 2 and j == n // 2 + 1 and (k == n // 2 + 2)):
				print(1, end=' ')
			else:
				print(0, end=' ')
		print('')


def prime(n):
	if n == 2:
		return True
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	matrix = [[0] * n for i in range(n)]
	x = 0
	flag = not prime(n)
	b = n - 1
	if flag:
		while True:
			x += 1
			if prime(x):
				continue
			elif prime(x + b):
				break
	else:
		x = 1
	for i in range(n):
		for j in range(n):
			if i == j:
				matrix[i][j] = x
			else:
				matrix[i][j] = 1
	for i in range(n):
		print(*matrix[i])


t = int(input())
while t > 0:
	n = int(input())
	for i in range(n):
		for j in range(n):
			if i == j:
				print(4, end=' ')
			elif (i + 1) % n == j:
				print(9, end=' ')
			else:
				print(0, end=' ')
		print()
	t -= 1


def prime_check(i):
	prime = True
	if i > 1:
		for j in range(2, i):
			if i % j == 0:
				prime = False
				break
	else:
		prime = False
	return prime

def op():
	n = int(input())
	prime = prime_check(n)
	if prime:
		for i in range(n):
			for j in range(n):
				print('1', '', end='')
			if i < n:
				print()
		return
	temp = 0
	prime_1 = False
	while prime_1 == False:
		temp = temp + 1
		num = temp + (n - 1) * n
		a = prime_check(num)
		b = prime_check(temp)
		if a and b == False:
			prime_1 = True
	for i in range(n):
		for j in range(n):
			if i < n - 1 and j == 0:
				print(n, '', end='')
			elif i == n - 1 and j == 0:
				print(temp, '', end='')
			elif j > 0 and j == i + 1:
				print(temp, '', end='')
			else:
				print(n, '', end='')
		if i < n:
			print()
t = int(input())
while t > 0:
	t = t - 1
	op()


def is_simple(x):
	return all((x % i for i in range(2, x - 1)))
t = int(input())
for something in range(t):
	n = int(input())
	m = 2 * n
	while not is_simple(m + n - 1) or is_simple(m):
		m += 1
	for i in range(n):
		for j in range(n):
			if i == j:
				print(m, end=' ')
			else:
				print(1, end=' ')
		print()


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import collections as col
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def primes(n):
	sieve = [True] * n
	z = int(n ** 0.5) + 1
	for i in range(3, z, 2):
		if sieve[i]:
			sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
	return [2] + [i for i in range(3, n, 2) if sieve[i]]
P = primes(1000)

def solve():
	N = getInt()
	if N in P:
		ans = [1] * N
		for n in range(N):
			print(*ans)
		return
	p = P[-1]
	idx = -1
	while p - N + 1 in P:
		idx -= 1
		p = P[idx]
	ans = [p - N + 1] + [1] * (N - 1)
	for n in range(N):
		ans = ans[1:] + ans[:1]
		print(*ans)
	return
for _ in range(getInt()):
	solve()


for _ in range(int(input())):
	n = int(input())
	list1 = [[0] * n for i in range(n)]
	for i in range(0, n):
		for j in range(0, n):
			if i == j or i + j == n - 1:
				list1[i][j] = 1
	if n % 2 != 0:
		list1[n // 2][0] = 1
		list1[n // 2][n - 1] = 1
		list1[0][n // 2] = 1
		list1[n - 1][n // 2] = 1
	for i in range(0, n):
		for j in range(0, n):
			print(list1[i][j], end=' ')
		print()


t = int(input())
primes = set()

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	for p in range(2, n + 1):
		if prime[p]:
			primes.add(p)
SieveOfEratosthenes(100000)
for _ in range(t):
	n = int(input())
	arr = [1] * (n - 1)
	for i in range(1, 10 ** 5 + 1):
		if i in primes:
			continue
		if i + n - 1 in primes:
			arr.append(i)
			break
	for i in range(n):
		s = ' '.join([str(j) for j in arr[i:] + arr[:i]])
		print(s)


for _ in range(int(input())):
	n = int(input())
	a = [[1 if abs(j - i) < 2 else 0 for i in range(n)] for j in range(n)]
	for i in range(n):
		print(*a[i])


import typing
import sys
import math
import collections
import bisect
import itertools
import heapq
import decimal
INF = 10 ** 20
MOD = 10 ** 9 + 7

def ni():
	return int(sys.stdin.buffer.readline())

def ns():
	return map(int, sys.stdin.buffer.readline().split())

def na():
	return list(map(int, sys.stdin.buffer.readline().split()))

def make_prime_nums(n):
	prime_nums = [2 if n >= 2 else None]
	for i in range(3, n + 1):
		for p in prime_nums:
			if p >= int(i ** (1 / 2)) + 1:
				prime_nums.append(i)
				break
			if i % p == 0:
				break
	return prime_nums

def main():
	t = ni()
	for _ in range(t):
		n = ni()
		prm = make_prime_nums(10 ** 5)
		prm.sort()
		setprm = set(prm)
		num = -1
		for p in prm:
			if p - (n - 1) < 1:
				continue
			if p - (n - 1) not in setprm:
				num = p - (n - 1)
				break
		a = [[num if i == j else 1 for i in range(n)] for j in range(n)]
		for ai in a:
			print(*ai, sep=' ')
main()


for i in range(int(input())):
	n = int(input())
	a = [0 for i in range(n - 2)]
	a.append(1)
	a.append(4)
	for k in range(n):
		print(*a)
		a.append(a[0])
		a.pop(0)


t = int(input())

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n == 1:
			return False
		elif n % i == 0:
			return False
	return True
for i in range(t):
	n = int(input())
	if isprime(n):
		for j in range(n):
			sec = [1] * n
			print(*sec)
	else:
		kek = [[1] * n for i in range(n)]
		if n > 2:
			kl = 0
			for j in range(n + 1, 10 ** 5):
				if isprime(j + n - 1) and isprime(j) == False:
					kl = j
					break
			s = 0
			for j in range(n):
				kek[j][s] = kl
				s += 1
				print(*kek[j])


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	n = II()
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		if i < n - 1:
			ans[i][i] = ans[i][i + 1] = 1
		else:
			ans[i][i] = ans[i][0] = 1
	for i in range(n):
		print(*ans[i])


n = int(input())
for _ in range(n):
	mat = []
	m = int(input())
	mat = [list(range(1 + m * i, 1 + m * (i + 1))) for i in range(m)]
	for i in range(0, len(mat)):
		for j in range(0, len(mat)):
			mat[i][j] = 0
	for i in range(0, len(mat)):
		for j in range(0, len(mat)):
			if i == j:
				mat[i][j] = 1
				if j + 1 < m:
					mat[i][j + 1] = 1
					break
			else:
				pass
	mat[m - 1][0] = 1
	for i in range(0, len(mat)):
		print(*mat[i])


p = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103}
for i in range(int(input())):
	n = int(input())
	(a, b) = (0, 0)
	m = n - 2
	flag = m not in p
	while flag:
		for a in range(107):
			if a not in p:
				for b in range(107):
					if b not in p:
						m = n - 2 + a + b
						flag = m not in p
						if not flag:
							break
			if not flag:
				break
	print(a, '1 ' * (n - 2) + str(b))
	for j in range(n - 2):
		print('1', '0 ' * (n - 2) + '1')
	print(b, '1 ' * (n - 2) + str(a))


import os
import sys
from io import BytesIO, IOBase

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		output = [[0] * n for _ in range(n)]
		for i in range(n):
			output[i][i] = 1
		for i in range(n):
			output[i][n - 1 - i] = 1
		if n % 2 == 1:
			pos = n // 2
			output[pos][pos - 1] = 1
			output[pos][pos + 1] = 1
			output[pos + 1][pos] = 1
			output[pos - 1][pos] = 1
		for l in output:
			for x in l:
				print(x, end=' ')
			print()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = lambda s: self.buffer.write(s.encode()) if self.writable else None

	def read(self):
		if self.buffer.tell():
			return self.buffer.read().decode('ascii')
		return os.read(self._fd, os.fstat(self._fd).st_size).decode('ascii')

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline().decode('ascii')

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

def print(*args, sep=' ', end='\n', file=sys.stdout, flush=False):
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(end)
	if flush:
		file.flush()
(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(10000)
main()


def solve(N):
	for i in N:
		ans = [[0 for _ in range(i)] for _ in range(i)]
		for j in range(i):
			ans[j][j] = 1
			cols = [j - 1, j + 1]
			for each in cols:
				if each >= 0 and each < i:
					ans[j][each] = 1
		for each in ans:
			print(*each)
t = int(input())
N = []
for _ in range(t):
	t1 = int(input())
	N.append(t1)
solve(N)


t = int(input())
st = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(0, t):
	n = int(input())
	ls = [0] * n
	m = []
	for i in range(0, n):
		m.append(ls.copy())
	sm = n - 1
	if n in st:
		for i in range(0, n):
			for j in range(0, n):
				print(1, end=' ')
			print()
	else:
		cut = 100
		for i in st:
			if i > sm:
				cut = i
				if cut - sm not in st:
					break
		dif = cut - sm
		for i in range(0, n):
			for j in range(0, n):
				if i == j:
					print(dif, end=' ')
					continue
				print(1, end=' ')
			print()


t = int(input())
for i in range(t):
	n = int(input())
	matrix = [[0 for j in range(n)] for k in range(n)]
	for j in range(n):
		matrix[j][j] = 1
		matrix[j][j - 1] = 1
	for j in range(n):
		print(*matrix[j])


numTestCases = int(input())
squareSizes = []
for i in range(0, numTestCases):
	squareSizes.append(int(input()))

def genPrimeSquare(squareSize):
	arr = []
	for i in range(0, squareSize):
		arr.append([])
	if squareSize % 2 == 0:
		for i in range(0, squareSize):
			for j in range(0, squareSize):
				if i == j:
					arr[i].append(1)
				elif i + j == squareSize - 1:
					arr[i].append(1)
				else:
					arr[i].append(0)
	if squareSize % 2 == 1:
		middle = squareSize // 2
		for i in range(0, squareSize):
			for j in range(0, squareSize):
				if i == j:
					arr[i].append(1)
				elif i + j == squareSize - 1:
					arr[i].append(1)
				elif i == middle and (j == 0 or j == squareSize - 1) or (j == middle and (i == 0 or i == squareSize - 1)):
					arr[i].append(1)
				else:
					arr[i].append(0)
	return arr
for i in squareSizes:
	arr = genPrimeSquare(i)
	for i in arr:
		print(' '.join(map(str, i)))


import math
for _ in range(int(input())):
	n = int(input())
	ans = []
	for i in range(n):
		temp = [0] * n
		ans.append(temp)
	if n % 2 == 0:
		l = n // 2 - 1
		r = l + 1
		for i in range(l + 1):
			ans[i][i] = 1
			ans[i][n - 1 - i] = 1
		for i in range(n - 1, r - 1, -1):
			ans[i][i] = 1
			ans[i][n - 1 - i] = 1
	else:
		x = n // 2
		for i in range(x - 1):
			ans[i][i] = 1
			ans[i][n - 1 - i] = 1
		for i in range(n - 1, x + 1, -1):
			ans[i][i] = 1
			ans[i][n - 1 - i] = 1
		ans[x][x] = 1
		ans[x][x + 1] = 1
		ans[x - 1][x - 1] = 1
		ans[x - 1][x + 1] = 1
		ans[x + 1][x] = 1
		ans[x + 1][x - 1] = 1
	for i in ans:
		print(*i)


t = int(input())
for i in range(t):
	n = int(input())
	for j in range(n):
		a = []
		for k in range(n):
			if k == j or k == j + 1 or (k == 0 and j == n - 1):
				a.append('1')
			else:
				a.append('0')
		print(' '.join(a))


for t in range(int(input())):
	n = int(input())
	ls = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		ls[i][i] = 1
		ls[i][n - 1 - i] = 1
	if n % 2 == 1:
		ls[n // 2 - 1][n // 2] = 1
		ls[n // 2 + 1][n // 2] = 1
		ls[n // 2][n // 2 - 1] = 1
		ls[n // 2][n // 2 + 1] = 1
	for i in range(n):
		for j in range(n):
			print(ls[i][j], end=' ')
		print()


def is_prime(x):
	k = 2
	while k * k <= x:
		if x % k == 0:
			return False
		k += 1
	return True
for t in range(int(input())):
	n = int(input())
	a = [1]
	count = 1
	i = 2
	total = 1
	while True:
		if is_prime(i) and i - total != 2:
			a.append(i - total)
			total = i
			count += 1
		i += 1
		if count == n:
			break
	b = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		k = 0
		for j in range(i, i + n):
			b[i][j % n] = a[k]
			k += 1
	for i in range(n):
		for j in range(n):
			print(b[i][j], end=' ')
		print()


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(*a, sep=' ', end='\n'):
	stdout.write(sep.join([str(i) for i in a]) + end)
from math import sqrt

def isp(x):
	ans = True
	for i in range(2, int(sqrt(max(x, 0))) + 1):
		ans &= x % i != 0
	return ans
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021]
for _ in range(int(get())):
	n = int(get())
	x = n - 1
	ans = [[1 for i in range(n)] for j in range(n)]
	for a in primes:
		if not isp(a - x):
			for i in range(n):
				for j in range(n):
					if i == j:
						ans[i][j] = a - x
			break
	for line in ans:
		putf(*line)


def isPrime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	if isPrime(n):
		for i in range(n):
			print('1 ' * n)
	else:
		for i in range(n - 1):
			print('0 ' * i + '1 ' * 2 + '0 ' * (n - 2 - i))
		print('1 ' + '0 ' * (n - 2) + '1 ')


import sys
input = sys.stdin.readline
nums = [False, False] + [True] * 100010
for i in range(len(nums)):
	if nums[i]:
		for j in range(i, len(nums), i):
			nums[j] = False
		nums[i] = True
for _ in range(int(input())):
	n = int(input())
	if nums[n]:
		ans = [[1] * n for _ in range(n)]
	elif n % 2 == 0:
		ans = [[0] * n for _ in range(n)]
		for i in range(n):
			ans[i][i] = 1
		for i in range(n):
			ans[n - i - 1][i] = 1
	else:
		ans = [[0] * n for _ in range(n)]
		for i in range(n):
			ans[i][i] = 1
		for i in range(n):
			ans[n - i - 1][i] = 1
		ans[n // 2][n // 2] = 0
		ans[n // 2][0] = 1
		ans[0][n // 2] = 1
		ans[n // 2][-1] = 1
		ans[-1][n // 2] = 1
	for i in ans:
		print(*i)


from collections import deque
t = int(input())

def Prime(n):
	if n == 1:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
while t > 0:
	n = int(input())
	arr = []
	for x in range(n):
		y = [4] * (n - 1)
		s = sum(y)
		for v in range(s + 1, 10 ** 5):
			if not Prime(v) and Prime(s + v):
				y.append(v)
				break
		ic = deque(y)
		ic.rotate(x)
		arr.append(list(ic))
	for u in arr:
		print(' '.join([str(x) for x in u]))
	t -= 1


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))
for _ in range(inp()):
	n = inp()
	rem = n
	res = [[0] * n for _ in range(n)]
	l = 0
	while l < n:
		if rem == 2:
			r = l + 2
			rem -= 2
		elif rem == 3:
			r = l + 3
			rem -= 3
		else:
			r = l + 2
			rem -= 2
		for i in range(l, r):
			for j in range(l, r):
				res[i][j] = 1
		l = r
	for x in res:
		print(*x)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def isprime(x):
	for j in range(2, x):
		if x % j == 0:
			return False
	return True

def gthan(x):
	xc = x
	x += 1
	while 1:
		if isprime(x) and (not isprime(x - xc)):
			return x - xc
		else:
			x += 1
for _ in range(int(input()) if True else 1):
	n = int(input())
	a = [[0] * n for __ in range(n)]
	for i in range(n - 1):
		for j in range(n - 1):
			a[i][j] = 1
	g1 = gthan(n - 1)
	for i in range(n - 1):
		a[i][n - 1] = g1
	for j in range(n - 1):
		a[n - 1][j] = g1
	a[n - 1][n - 1] = gthan(g1 * (n - 1))
	for i in a:
		print(*i)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for pp in range(t):
		solve(pp)

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def solve(case):
	n = int(inp())
	for i in range(1, 10 ** 6 + 1):
		if isPrime(i) and i - (n - 1) >= 0 and (not isPrime(i - (n - 1))):
			val = i - (n - 1)
			break
	ans = [[1] * n for i in range(n)]
	for i in range(n):
		ans[i][i] = val
	for i in range(n):
		print(' '.join((str(ans[i][j]) for j in range(n))))
testcase(int(inp()))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [0 for _ in range(n)]
	arr[0] = 1
	arr[1] = 1
	for _ in range(n):
		for elem in arr:
			print(elem, end=' ')
		print()
		arr = arr[1:] + arr[:1]


import math
test_count = int(input())
tests = []
for _ in range(test_count):
	x = int(input())
	tests.append(x)

def is_prime(x):
	if x == 1:
		return False
	for i in range(2, int(math.sqrt(x)) + 1):
		if x % i == 0:
			return False
	return True

def get_next_prime(x):
	y = x + 1
	while True:
		if is_prime(y):
			if is_prime(y - x):
				y += 1
			else:
				return (y, y - x)
		else:
			y += 1
for test in tests:
	x = test
	if is_prime(x):
		for _ in range(x):
			print(' '.join(['1'] * x))
	else:
		(y, r) = get_next_prime(x - 1)
		(z, s) = get_next_prime(r * (x - 1))
		for _ in range(x - 1):
			print(' '.join(['1'] * (x - 1) + [str(r)]))
		print(' '.join([str(r)] * (x - 1) + [str(s)]))


T = int(input())
for t in range(T):
	n = int(input())
	first = ['1', '1'] + ['0'] * (n - 2)
	print(' '.join(first))
	for i in range(1, n - 1):
		res = ['0'] * i + ['1', '1'] + ['0'] * (n - 2 - i)
		print(' '.join(res))
	last = ['1'] + ['0'] * (n - 2) + ['1']
	print(' '.join(last))


for i in range(int(input())):
	n = int(input())
	p = []
	t = n - 1
	for j in range(n):
		m = [0 for i in range(n)]
		for k in range(n):
			if t == k or k == t - 1:
				m[t] = 1
				m[t - 1] = 1
		t -= 1
		p.append(m)
	for l in p:
		print(*l, sep=' ')


dic = {2: 1, 3: 1, 4: 4, 5: 1, 6: 6, 7: 1, 8: 4, 9: 9, 10: 4, 11: 1, 12: 6, 13: 1, 14: 4, 15: 8, 16: 4, 17: 1, 18: 6, 19: 1, 20: 10, 21: 9, 22: 6, 23: 1, 24: 6, 25: 4, 26: 4, 27: 6, 28: 4, 29: 1, 30: 8, 31: 1, 32: 10, 33: 6, 34: 6, 35: 4, 36: 6, 37: 1, 38: 4, 39: 6, 40: 4, 41: 1, 42: 18, 43: 1, 44: 4, 45: 8, 46: 4, 47: 1, 48: 6, 49: 4, 50: 4, 51: 8, 52: 6, 53: 1, 54: 14, 55: 8, 56: 6, 57: 6, 58: 4, 59: 1, 60: 12, 61: 1, 62: 6, 63: 6, 64: 6, 65: 4, 66: 8, 67: 1, 68: 4, 69: 6, 70: 4, 71: 1, 72: 8, 73: 1, 74: 4, 75: 8, 76: 8, 77: 6, 78: 6, 79: 1, 80: 4, 81: 8, 82: 6, 83: 1, 84: 6, 85: 4, 86: 12, 87: 12, 88: 4, 89: 1, 90: 12, 91: 6, 92: 6, 93: 9, 94: 4, 95: 10, 96: 6, 97: 1, 98: 4, 99: 9, 100: 4}
for t in range(int(input())):
	n = int(input())
	for i in range(n):
		for j in range(n):
			if i == j:
				if j == n - 1:
					print(1)
				else:
					print(1, end=' ')
			elif j == n - 1:
				print(dic[n])
			else:
				print(dic[n], end=' ')


t = int(input())
for num in range(t):
	n = int(input())
	i = 0
	for value in range(n):
		zero_list = [0 for x in range(n)]
		zero_list[i] = 1
		try:
			zero_list[i + 1] = 4
		except IndexError:
			zero_list[0] = 4
		i += 1
		print(' '.join((str(x) for x in zero_list)))


t = int(input())
for i in range(t):
	n = int(input())
	for j in range(1, n + 1):
		l = [0] * n
		for k in range(1, n + 1):
			if j == 1:
				if n % 2 == 1:
					l[n // 2] = 1
			if j == k or j == n - k + 1:
				l[k - 1] = 1
		if n % 2 == 1:
			if j == n // 2 + 1:
				l[0] = 1
		print(*l)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
primes = [True for i in range(10 ** 5 + 19)]
for i in range(2, 10 ** 5 + 5):
	if primes[i]:
		for j in range(2 * i, 10 ** 5 + 5, i):
			primes[j] = False
mymat = [[-1 for j in range(101)] for i in range(101)]
mymat[0][0] = mymat[0][1] = mymat[0][2] = 1
mymat[1][0] = mymat[1][1] = mymat[1][2] = 1
mymat[2][0] = mymat[2][1] = mymat[2][2] = 1
primes = [i for i in range(10 ** 5 + 5) if primes[i]]
primeset = set(primes)
primeset.remove(0)
for i in range(100):
	cursum = 0
	start = i
	if i < 3:
		start = 3
		cursum = 3
	else:
		for j in range(i):
			cursum += mymat[i][j]
	for j in range(start, 100):
		ind = bl(primes, cursum) + 1
		while primes[ind] - cursum in primeset:
			ind += 1
		temp = primes[ind] - cursum
		mymat[i][j] = mymat[j][i] = temp
		cursum += temp
for i in range(100 - 1, -1, -1):
	for j in range(100):
		mymat[j][i] = mymat[i][j]
for _ in range(val()):
	n = val()
	for i in range(n):
		for j in range(n):
			print(mymat[i][j], end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	ans = [0] * (n - 2)
	ans += [1] * 2
	for i in range(n):
		s = i
		ins = 0
		while ins < n:
			print(ans[s], end=' ')
			s = (s + 1) % n
			ins += 1
		print()


import sys, collections, math, itertools, random
INF = sys.maxsize

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()
mod = 1000000007
for _ in range(int(input())):
	n = int(input())
	arr = [0 for i in range(n)]
	for i in range(n):
		arr[i] = 1
		arr[(i + 1) % n] = 1
		print(*arr)
		arr = [0 for j in range(n)]


l = list(range(4, 201))
pl = [2, 3]
for i in l:
	prime = True
	for j in pl:
		if i % j == 0:
			prime = False
	if prime:
		pl.append(i)
l = list(range(1, 201))
np = list(set(l) - set(pl))
t = int(input())
for _ in range(t):
	n = int(input())
	res = [[1 for i in range(n)] for j in range(n)]
	if n not in pl + [1]:
		for i in pl:
			if i > n and i - n + 1 not in pl:
				x = i - n + 1
		for j in range(n):
			res[j][j] = x
	for i in range(n):
		resi = ''
		for j in range(n):
			resi = resi + str(res[i][j]) + ' '
		print(resi)


def problem_sol(n):
	r = n
	l = -1
	odd = False
	if n % 2 != 0:
		odd = True
	for i in range(n):
		print('\n')
		l += 1
		r -= 1
		for j in range(n):
			print(int(l == j or r == j or (odd and j == n // 2 and (i == n - 1)) or (odd and j == n - 1 and (i == n // 2))), end=' ')
no_inp = int(input())
while no_inp > 0:
	n = int(input())
	problem_sol(n)
	no_inp -= 1


import copy
primes = [0] * 10000
for i in range(2, 10000):
	for j in range(2, i):
		if j * j > i:
			break
		if i != j and i % j == 0:
			primes[i] = 1
t = int(input())
for i in range(t):
	n = int(input())
	a = [4, 1] + [0] * (n - 2)
	for i in range(n):
		print(' '.join([str(x) for x in a]))
		a = [a[-1]] + a[:-1]


prms = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973]

def nxpm(n, prms):
	for i in prms:
		if i > n and i - n not in prms:
			return i - n
t = int(input())
while t:
	n = int(input())
	m = [[1 for i in range(n)] for j in range(n)]
	if n in prms:
		for i in range(n):
			for j in range(n):
				print(m[i][j], end=' ')
			print()
	else:
		y = nxpm(n - 1, prms)
		for i in range(n - 1):
			m[i][-1] = y
		for j in range(n - 1):
			m[-1][j] = y
		m[-1][-1] = nxpm(y * (n - 1), prms)
		for i in range(n):
			for j in range(n):
				print(m[i][j], end=' ')
			print()
	t = t - 1


import io
import os
import sys
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def printd(*args, **kwargs):
	print(*args, **kwargs)
	pass

def get_str():
	return input().decode().strip()

def rint():
	return map(int, input().split())

def oint():
	return int(input())
t = oint()
for _ in range(t):
	n = oint()
	line = [0] * n
	if n % 2 == 0:
		for i in range(n):
			line[n - 1 - i] = 1
			line[i] = 1
			print(*line)
			line[n - 1 - i] = 0
			line[i] = 0
	else:
		for i in range(n):
			if i == n // 2 or i == 0 or i == n - 1:
				line[n // 2] = 1
				line[0] = 1
				line[n - 1] = 1
				print(*line)
				line[n // 2] = 0
				line[0] = 0
				line[n - 1] = 0
			else:
				line[n - 1 - i] = 1
				line[i] = 1
				print(*line)
				line[n - 1 - i] = 0
				line[i] = 0


def read_str():
	return str(input())

def read_int():
	return int(input())

def read_str_array():
	return [str(i) for i in input().split()]

def read_int_array():
	return [int(i) for i in input().split()]

def is_prime(n):
	for i in range(2, n):
		if n % i == 0:
			return True
	return False

def nxt(n):
	if n % 2 == 0:
		for i in range(1, int(100000.0) + 1, 2):
			if is_prime(n + i) == False and is_prime(i):
				return int(i)
	else:
		for i in range(2, int(100000.0) + 1, 2):
			if is_prime(n + i) == False and is_prime(i):
				return int(i)

def last_nxt(n1, n2):
	for i in range(4, int(100000.0) + 1):
		if is_prime(n1 + i) == False and is_prime(n2 + i) == False and is_prime(i):
			return int(i)
	return None

def get_sum(arr, l):
	ss = 0
	for j in range(0, len(arr) - 1):
		ss += arr[j][l]
	return ss

def even_s():
	evens = []
	for i in range(4, 250, 2):
		evens.append(4)
	return evens

def solve(n):
	even = even_s()
	temp = even[:n - 1]
	temp.append(nxt(sum(temp)))
	mat = [temp] * int(n - 1)
	mat.append([])
	for l in range(0, n - 1):
		sm = even[l] * (n - 1)
		mat[-1].append(nxt(sm))
		print(*mat[l])
	t = len(mat[0]) - 1
	mat[-1].append(last_nxt(mat[0][t] * (n - 1), mat[n - 1][0] * (n - 1)))
	print(*mat[n - 1])
	return mat
T = read_int()
for _ in range(1, T + 1):
	N = read_int()
	solve(N)


t = int(input())
for _ in range(t):
	n = int(input())
	square = [[0 for j in range(n)] for i in range(n)]
	for i in range(n):
		square[i][i] = 6
	for i in range(n - 1):
		square[i][i + 1] = 4
	square[n - 1][0] = 4
	for i in range(n - 1):
		square[i + 1][i] = 1
	square[0][n - 1] = 1
	for row in square:
		for ele in row:
			print(ele, end=' ')
		print()


from sys import stdin, stdout
import math
cases = int(stdin.readline().rstrip())
for case in range(cases):
	n = int(stdin.readline().rstrip())
	matrix = [['0'] * n for i in range(n)]
	for i in range(n):
		matrix[i][i] = '1'
		matrix[i][n - i - 1] = '1'
	if n % 2 == 1:
		mid = (n - 1) // 2
		matrix[mid][0] = '1'
		matrix[mid][n - 1] = '1'
		matrix[0][mid] = '1'
		matrix[n - 1][mid] = '1'
	for row in matrix:
		stdout.write(' '.join(row))
		stdout.write('\n')


from collections import Counter
import math
import sys
import heapq
from bisect import bisect, bisect_left, bisect_right

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def mod():
	return 10 ** 9 + 7
for i in range(INT()):
	n = INT()
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		ans[i][i] = 1
	for i in range(n):
		ans[i][n - i - 1] = 1
	if n % 2 != 0:
		ans[n // 2][0] = 1
		ans[n // 2][-1] = 1
		ans[0][n // 2] = 1
		ans[-1][n // 2] = 1
	for i in ans:
		print(*i)


t = int(input())
for i in range(t):
	n = int(input())
	arr = [[0 for i in range(n)] for j in range(n)]
	for i in range(n - 1):
		arr[i][i] = 1
		arr[i][i + 1] = 1
	arr[n - 1][0] = 1
	arr[n - 1][n - 1] = 1
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()


for i in range(int(input())):
	n = int(input())
	l = [0] * (n - 2)
	l.append(1)
	l.append(1)
	ans = []
	for j in range(n):
		row = []
		for k in range(n):
			if j == k:
				row.append(1)
			elif k == j + 1 and k + 1 <= n:
				row.append(1)
			else:
				row.append(0)
		ans.append(row)
	ans[n - 1][0] = 1
	for j in ans:
		print(*j)


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
t = ini()
for _ in range(t):
	n = ini()
	ans = [6, 1]
	start = 0
	end = n - 2
	for i in range(n):
		if i + 1 == n:
			start -= 1
			print(*[1] + [0] * start + [6])
		else:
			print(*[0] * start + ans + [0] * end)
		start += 1
		end -= 1


for _ in range(int(input())):
	n = int(input())
	mat = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		mat[i][i] = 1
		if i + 1 < n:
			mat[i][i + 1] = 1
	mat[-1][0] = 1
	for i in mat:
		print(*i)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	n = N()
	print(' '.join('1' + '0' * (n - 2) + '1'))
	for i in range(n - 2, -1, -1):
		print(' '.join('0' * i + '1' * 2 + '0' * (n - i - 2)))


prime = [0 for i in range(10001)]
prime[0] = 1
prime[1] = 1
for i in range(2, 10001):
	if prime[i] == 0:
		j = 2
		ans = i * j
		while ans < 10001:
			prime[ans] = 1
			j += 1
			ans = i * j
t = int(input())
for i in range(t):
	n = int(input())
	k = 2
	while k < 100:
		if prime[k] != 0 and prime[k * (n - 1) + 1] == 0:
			break
		k += 1
	ans = [[k for j in range(n)] for l in range(n)]
	for j in range(n):
		ans[j][j] = 1
	for s in ans:
		for j in s:
			print(j, end=' ')
		print('')


a = [1, 1]
for i in range(1, 100):
	a.append(0)
t = int(input())
while t:
	t = t - 1
	n = int(input())
	z = a[0:n]
	for i in range(n):
		zz = z[i:n] + z[0:i]
		print(*zz)


t = int(input())
prime = []
for i in range(2, 100000):
	f = True
	for j in range(2, int(i ** 0.5) + 1):
		if i % j == 0:
			f = False
			break
	if f:
		prime.append(i)
for _ in range(t):
	n = int(input())
	(a, b) = (0, 0)
	for p in prime:
		if p > n - 1 and p - n + 1 not in prime:
			a = p - n + 1
			break
	for p in prime:
		if p > (n - 1) * a and p - (n - 1) * a not in prime:
			b = p - (n - 1) * a
			break
	for i in range(n):
		if i < n - 1:
			print(' '.join(list(map(str, [1] * (n - 1) + [a]))))
		else:
			print(' '.join(list(map(str, [a] * (n - 1) + [b]))))


t = int(input())
P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397]
while t:
	t -= 1
	n = int(input())
	A = [[1 for i in range(n)] for j in range(n)]
	j = 0
	flag = False
	if n in P:
		flag = True
	while j < len(P):
		j += 1
		if n < P[j] and P[j] - n + 1 not in P:
			break
	if n != P[j]:
		for i in range(n):
			A[i][i] += abs(P[j] - n)
	for i in range(n):
		for k in range(n):
			print(A[i][k], end=' ')
		print()


import sys
input = sys.stdin.readline
t = int(input())
m = 10 ** 6
prime = [True for i in range(m + 1)]
p = 2
while p * p <= m:
	if prime[p] == True:
		for i in range(p * p, m + 1, p):
			prime[i] = False
	p += 1
l = []
d = {}
for i in range(2, m + 1):
	if prime[i]:
		l.append(i)
		d[i] = 1
for t1 in range(t):
	n = int(input())
	mat = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i == j - 1:
				mat[i][j] = 1
	for i in range(n):
		s = ''
		for j in range(n):
			if i == n - 1 and j == 0:
				mat[i][j] = 1
			s += str(mat[i][j]) + ' '
		print(s)


import os
import sys
from io import BytesIO, IOBase

def main():
	for tc in range(int(input())):
		n = int(input())
		for i in range(n):
			for j in range(n):
				if i == 0 and j == n // 2:
					print('1', end=' ')
				elif i == n // 2 and j == 0:
					print('1', end=' ')
				elif i == j or j == n - i - 1:
					print('1', end=' ')
				else:
					print('0', end=' ')
			print('')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def prime(candidat):
	for i in range(2, candidat):
		if candidat % i == 0:
			return False
	return True

def next_prime_numberwc(k):
	candidat = k
	while not (prime(candidat) and (not prime(candidat - k + 1))):
		candidat += 1
	return candidat

def affiche(mat):
	for lig in mat:
		for x in lig:
			print(x, end=' ')
		print()
t = int(input())
for _ in range(t):
	n = int(input())
	lig = [1] * (n - 1)
	pri = next_prime_numberwc(n) - n + 1
	lig.append(pri)
	mat = [lig[:] for j in range(n - 1)]
	s = pri * (n - 1)
	lastlig = [pri] * (n - 1) + [next_prime_numberwc(s + 1) - s]
	mat.append(lastlig)
	affiche(mat)


arr = [2, 3, 5]
for i in range(6, 550):
	for j in range(2, int(i ** 0.5) + 1):
		if i % j == 0:
			break
	else:
		arr.append(i)
for _ in range(int(input())):
	n = int(input())
	ans = [[1 for i in range(n)] for h in range(n)]
	som = n - 1
	for ele in arr:
		if ele - som > 0 and ele - som not in arr:
			val = ele - som
			break
	for i in range(n):
		ans[i][i] = val
	for i in range(n):
		for j in range(n):
			print(ans[i][j], end=' ')
		print()


import bisect
import math
primes = []

def isprime(n):
	for j in range(2, int(math.sqrt(n) + 1)):
		if n % j == 0:
			return False
	return True
z = 5
while len(primes) < 101:
	if isprime(z):
		primes.append(z)
	z += 1

def solve(n):
	ans = []
	cnt = 1
	for j in range(n):
		row = []
		for k in range(n):
			if j == k:
				row.append(primes[j] - cnt)
			elif k == j + 1:
				row.append(cnt)
			else:
				row.append(0)
		ans.append(row)
	ans[-1][-1] = 1
	ans[-1][0] = 1
	return ans
	pass
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	ans.append(solve(n))
for grid in ans:
	for row in grid:
		for ele in row:
			print(ele, end=' ')
		print()


import math
import sys, bisect
from heapq import *
from itertools import *
from collections import *
sys.setrecursionlimit(10 ** 6)
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
prime = SieveOfEratosthenes(10 ** 5)
prime[1] = False
prime[0] = False
A = []
for i in range(2, 10 ** 5 + 1):
	if prime[i]:
		A += [i]
for _ in range(I()):
	n = I()
	for j in A:
		f = 0
		for i in range(1, j + 1):
			if j - i * (n - 1) >= 0 and (not prime[i]) and (not prime[j - i * (n - 1)]):
				t = i
				k = j - i * (n - 1)
				f = 1
				break
		if f:
			break
	Ans = [[0] * n for i in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				Ans[i][j] = k
			else:
				Ans[i][j] = t
	for i in Ans:
		print(*i)


import sys
input = sys.stdin.readline

def prime_check(num):
	if num < 2:
		return False
	else:
		for i in range(2, int(num ** 0.5) + 1):
			if num % i == 0:
				return False
		return True
t = int(input())
for i in range(t):
	n = int(input())
	k = 1
	array = [[0] * n for _ in range(n)]
	while True:
		if prime_check(k) is False:
			if prime_check(1 + k * (n - 1)) is True:
				break
		k += 1
	for m in range(n):
		for l in range(n):
			array[m][l] = k
	for j in range(n):
		array[j][j] = 1
	for s in range(n):
		print(*array[s])


import math
from bisect import *

def primes(N=2 * 10 ** 5):
	sieve = [True] * N
	for i in range(3, int(math.sqrt(N)) + 1, 2):
		if sieve[i]:
			sieve[i * i::2 * i] = [False] * ((N - i * i - 1) // (2 * i) + 1)
	return [2] + [i for i in range(3, N, 2) if sieve[i]]
P = primes()
pSet = set(P)
for _ in range(int(input())):
	n = int(input())
	ind = bisect_right(P, n)
	rmc = None
	for i in range(ind, len(P)):
		t = P[i] - n + 1
		if t > 0 and t not in pSet:
			rmc = t
			break
	xc = (n - 1) * rmc
	ind = bisect_right(P, xc)
	x = None
	for i in range(ind, len(P)):
		t = P[i] - xc
		if t > 0 and t not in pSet:
			x = t
			break
	for i in range(n - 1):
		print('1 ' * (n - 1) + f'{rmc} ')
	print(f'{rmc} ' * (n - 1) + f'{x}')


for _ in range(int(input())):
	n = int(input())
	for i in range(n):
		l = [0] * n
		l[i] = 1
		l[(i + 1) % n] = 1
		print(*l)


t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(n):
		a = [0] * n
		a[i] = 1
		a[(i + 1) % n] = 1
		for x in a:
			print(x, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	ans = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		ans[i][i] = 1
		ans[i][n - 1 - i] = 1
	if n % 2:
		ans[0][n // 2] = 1
		ans[n // 2][0] = 1
		ans[n // 2][n - 1] = 1
		ans[n - 1][n // 2] = 1
	for i in ans:
		print(*i)


primes = []
isPrime = dict()
for y in range(2, 200):
	x = y
	dividers = []
	for x in range(2, x):
		if (y / x).is_integer():
			dividers.append(x)
	if len(dividers) < 1:
		isPrime[y] = True
		primes.append(y)
t = int(input())
for t1 in range(t):
	n = int(input())
	used_n = -1
	for i in range(len(primes)):
		new_n = primes[i] - (n - 1)
		if new_n > 0 and (not new_n in isPrime):
			used_n = new_n
			break
	for i in range(n):
		answer = [1] * n
		answer[i] = used_n
		print(' '.join(map(str, answer)))


def print_arr(arr):
	for i in range(len(arr)):
		s = ''
		for j in range(len(arr[i])):
			s += str(arr[i][j]) + ' '
		print(s)

def prime(num):
	if num == 2 or num == 3:
		return True
	for i in range(2, int(num ** (1 / 2) + 1)):
		if num % i == 0:
			return False
	return True

def primesq(n):
	result = []
	if prime(n) == True:
		for i in range(n):
			temp = []
			for j in range(n):
				temp.append(1)
			result.append(temp)
		return result
	for i in range(n):
		temp = []
		for j in range(n):
			if j == i or j == i + 1 or (j == 0 and i == n - 1):
				temp.append(1)
			else:
				temp.append(0)
		result.append(temp)
	return result
t = int(input())
for _ in range(t):
	n = int(input())
	print_arr(primesq(n))


t = int(input())
for _ in range(t):
	n = int(input())
	q = [0] * (n - 2)
	q.append(1)
	q.append(1)
	for i in range(n):
		e = q[i:] + q[:i]
		print(*e)


kk = [5, 11, 7, 13, 11, 31, 11, 17, 13, 19, 17, 37, 17, 23, 19, 31, 23, 43, 23, 29, 29, 31, 29, 59, 29, 41, 31, 37, 37, 79, 37, 41, 37, 43, 41, 61, 41, 47, 43, 61, 47, 67, 47, 53, 53, 61, 53, 73, 53, 59, 59, 61, 59, 79, 59, 71, 61, 67, 67, 109, 67, 71, 67, 73, 71, 101, 71, 83, 73, 79, 79, 97, 79, 83, 79, 97, 83, 103, 83, 89, 89, 97, 89, 109, 89, 101, 97, 97, 97, 139, 97, 101, 97, 103, 101, 131, 101, 107, 103]
T = int(input())
for _ in range(0, T):
	n = int(input())
	for i in range(n):
		L = []
		for j in range(n - i - 1):
			L.append(1)
		L.append(kk[n - 2] - n + 1)
		for j in range(i):
			L.append(1)
		print(*L)


prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(int(input())):
	n = int(input())
	main_n = 101
	for prime in prime_numbers:
		if prime - n + 1 > 0 and prime - n + 1 not in prime_numbers:
			main_n = prime
			break
	s = '1 '
	for i in range(n):
		line = s * i + str(main_n - n + 1) + ' ' + s * (n - i - 1)
		print(line.rstrip(' '))


def solve(n):
	ans = []
	s = [0] * n
	s[0] = 1
	s[1] = 1
	ans.append(s)
	print(*s)
	for i in range(n - 1):
		p = s[0]
		del s[0]
		s.append(p)
		print(*s)
for _ in range(int(input())):
	solve(int(input()))


import sys
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from collections import *
from math import *
for _ in range(ii1()):
	n = ii1()
	if n == 2:
		print(1, 1)
		print(1, 1)
	elif n == 3:
		print(1, 1, 1)
		print(1, 1, 1)
		print(1, 1, 1)
	elif n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i == j or i == n - j - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				if i == j or i == n - j - 1:
					print(1, end=' ')
				elif i == 0 and j == n // 2:
					print(1, end=' ')
				elif i == n - 1 and j == n // 2:
					print(1, end=' ')
				elif j == 0 and i == n // 2:
					print(1, end=' ')
				elif j == n - 1 and i == n // 2:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()


import array

def isprime(x):
	for i in range(2, x - 1):
		if x % i == 0:
			return 0
	return 1
T = int(input())
for Tid in range(T):
	n = int(input())
	x = -1
	for i in range(2, 100):
		if isprime(i) == 0:
			if isprime(i + n - 1) == 1:
				x = i
	for i in range(n):
		for j in range(n):
			if i == j:
				print(x, end=' ')
			else:
				print(1, end=' ')
		print()


import copy

def isprime(resultant):
	isprime = True
	for i in range(2, resultant):
		if resultant % i == 0:
			isprime = False
			return False
	return True

def replace_last(a):
	sums = sum(a)
	sums_minus = sums - 1
	for i in range(4, 10000):
		if isprime(i) != True:
			resultant = sums_minus + i
			if isprime(resultant):
				a[len(a) - 1] = i
				return a

def find_sum(a):
	return sum(a)

def find_prime_list(prime_square_size):
	n = prime_square_size
	a = [1] * n
	sums = find_sum(a)
	if isprime(sums):
		return a
	else:
		a = replace_last(a)
		return a

def shift_left(result):
	temp = result[0]
	for i in range(0, len(result) - 1):
		result[i] = result[i + 1]
	result[len(result) - 1] = temp
	return result
t = int(input())
res = []
for i in range(0, t):
	prime_square_size = int(input())
	resultant = find_prime_list(prime_square_size)
	x = copy.deepcopy(resultant)
	res.append(x)
	for i in range(0, prime_square_size - 1):
		final = []
		temp = resultant[0]
		for i in range(0, len(resultant) - 1):
			resultant[i] = resultant[i + 1]
		resultant[len(resultant) - 1] = temp
		final = copy.deepcopy(resultant)
		res.append(final)
for i in res:
	for j in i:
		print('{}'.format(j), end=' ')
	print('')


def pr(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True
for i in range(int(input())):
	x = int(input())
	z = 1
	while not pr(x - 1 + z) or pr(z):
		z += 1
	z2 = 1
	while not pr((x - 1) * z + z2) or pr(z2):
		z2 += 1
	for k in range(x - 1):
		for j in range(x - 1):
			print(1, end=' ')
		print(z)
	for j in range(x - 1):
		print(z, end=' ')
	print(z2)


import math

def isprime(n):
	ss = int(math.sqrt(n))
	for i in range(2, ss + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	i = 1
	while True:
		if not isprime(i):
			if isprime(n - 1 + i):
				break
		i += 1
	for k in range(n):
		for j in range(n):
			if k == j:
				print(i, end=' ')
			else:
				print(1, end=' ')
		print()


def isprime(n):
	if n <= 1:
		return False
	elif n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i += 6
	return True

def get(num):
	for k in range(num + 1, 1000000):
		if isprime(k) == True:
			dif = k - num
			if isprime(dif) == False:
				return dif
	return 1
tc = int(input())
for i in range(tc):
	n = int(input())
	k = get(n - 1)
	m = get(k * (n - 1))
	for i in range(n):
		for j in range(n):
			if i < n - 1 and j < n - 1:
				print(1, end='')
			elif i == n - 1 and j == n - 1:
				print(m, end='')
			else:
				print(k, end='')
			if j != n - 1:
				print(' ', end='')
		print()


import bisect
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def solution():
	t = inp()
	primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
	for _ in range(t):
		n = inp()
		solve(n, primes)

def solve(n, primes):
	if n == 1:
		print(2)
	else:
		k = bisect.bisect_left(primes, n)
		num_0 = n - primes[k - 1]
		row = [1] * (n - num_0) + [0] * num_0
		for j in range(n):
			for i in range(n):
				print(row[(i + j) % n], end=' ')
			print()
solution()


for T in range(int(input())):
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i + 1 == j:
				a[i][j] = 1
	a[n - 1][0] = 1
	for i in range(n):
		print(*a[i])


t = int(input())
primes = [1 for i in range(100001)]
primes[0] = 0
primes[1] = 0
res = 0
for i in range(10001):
	if primes[i] == 1:
		for j in range(i * i, 100001, i):
			primes[j] = 0
for k in range(t):
	n = int(input())
	ind = 0
	array = [[1 for i in range(n)] for j in range(n)]
	for i in range(1, 100001):
		if primes[i] == 0:
			ind = i + n - 1
			if primes[ind] == 1:
				res = i
				break
	for i in range(n):
		array[i][i] = res
	for i in range(n):
		for j in range(n):
			print(array[i][j], end=' ')
		print()


def main():
	N = 100010
	t = int(input())
	isPrimer = [True for _ in range(N + 1)]
	isPrimer[1] = False
	primers = []
	for i in range(2, N):
		if isPrimer[i]:
			primers.append(i)
			j = i + i
			while j <= N:
				isPrimer[j] = False
				j += i
	for _ in range(t):
		n = int(input())
		res = 2
		for primer in primers:
			if primer - (n - 1) > 0 and primer - (n - 1) not in primers:
				res = primer - (n - 1)
				break
		for i in range(n):
			print('1 ' * i, end='')
			print('{} '.format(res), end='')
			print('1 ' * (n - i - 1), end='')
			print('')
main()


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	lst = []
	for i in range(n):
		if i == 0:
			lst.append(1)
		elif i == 1:
			lst.append(1)
		else:
			lst.append(0)
	for j in range(len(lst)):
		saved = j
		for z in range(j, len(lst)):
			print(lst[z], end=' ')
		for d in range(0, j):
			print(lst[d], end=' ')
		print()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
from collections import deque
for _ in range(int(ri())):
	n = int(ri())
	ans = []
	if n % 2 == 0:
		for i in range(n // 2):
			temp = [0] * n
			temp[i] = 1
			temp[n - i - 1] = 1
			ans.append(temp[:])
			ans.append(temp[:])
	else:
		for i in range(n // 2):
			temp = [0] * n
			temp[i] = 1
			temp[n - i - 1] = 1
			ans.append(temp[:])
			ans.append(temp[:])
		temp = [0] * n
		temp[0] = temp[n - 1] = 1
		temp[n // 2] = 1
		ans.append(temp)
		ans[0][n // 2] = 1
		ans[1][n // 2] = 1
	for i in ans:
		print(*i)


for i in range(int(input())):
	n = int(input())
	count = 0
	if n % 2 == 0:
		for c in range(n):
			for ppi in range(n):
				if ppi - c == 0 or ppi + c == n - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()
	else:
		for c in range(n):
			for ppi in range(n):
				if c == 0 and ppi == n // 2 or (c == n // 2 and ppi == n - 1):
					print(1, end=' ')
				elif ppi - c == 0 or ppi + c == n - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	n = int(input())
	r = [[0 for j in range(n)] for i in range(n)]
	for i in range(n):
		r[i][n - 1 - i] = 4
	r[0][0] = 1
	for i in range(1, n):
		r[i][n - i] = 1
	for i in r:
		print(*i)


MAX_SIZE = 100 * (100 + 1) // 2
prime = [True] * (MAX_SIZE + 1)

def seive():
	p = 2
	while p * p <= MAX_SIZE:
		if prime[p]:
			for i in range(p * p, MAX_SIZE, p):
				prime[i] = False
		p += 1
seive()
for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		u = (n - 1) * 4
		c = 1
		while 1:
			if not prime[c] and prime[u + c]:
				break
			else:
				c += 1
		t = 0
		for i in range(n):
			for j in range(n):
				if j == t:
					print(c, end=' ')
				else:
					print(4, end=' ')
			print()
			t += 1
	else:
		u = n - 1
		c = 1
		while 1:
			if not prime[c] and prime[u + c]:
				break
			else:
				c += 1
		t = 0
		for i in range(n):
			for j in range(n):
				if j == t:
					print(c, end=' ')
				else:
					print(1, end=' ')
			print()
			t += 1


t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(n):
		l = []
		for j in range(n):
			if abs(j - i) <= 1:
				l.append('1')
			else:
				l.append('0')
		print(' '.join(l))


def isPrime(n):
	if n == 1:
		return False
	if n % 2 == 0:
		return n == 2
	d = 3
	while d * d <= n and n % d != 0:
		d += 2
	return d * d > n

def wrong_square(x):
	bad = False
	(n, m) = (len(x), len(x[0]))
	for i in range(n):
		line_total = 0
		for j in range(m):
			if x[i][j] < 0:
				return True
			if isPrime(x[i][j]):
				bad = True
				return bad
			line_total += x[i][j]
		if not isPrime(line_total):
			bad = True
			return bad
	for j in range(m):
		col_total = 0
		for i in range(n):
			col_total += x[i][j]
		if not isPrime(col_total):
			bad = True
			return bad
	return False
prime_numbers = [i for i in range(1000) if isPrime(i)]
not_prime_numbers = [i for i in range(1000) if not isPrime(i)]

def attach_new(x):
	(n, m) = (len(x), len(x[0]))
	y = [[0 for i in range(m + 1)] for i in range(n + 1)]
	for i in range(n):
		for j in range(m):
			y[i][j] = x[i][j]
	for i in range(n + 1):
		total_line = 0
		for j in range(m + 1):
			total_line += y[i][j]
			if i != n and j == m:
				idx = 0
				diff = prime_numbers[idx] - total_line
				cond = not isPrime(diff) and diff > 0
				while not cond:
					idx += 1
					diff = prime_numbers[idx] - total_line
					cond = not isPrime(diff) and diff > 0
				y[i][j] = diff
	for j in range(m + 1):
		total = 0
		for i in range(n + 1):
			total += y[i][j]
			if i == n and j != m:
				idx = 0
				diff = prime_numbers[idx] - total
				cond = not isPrime(diff) and diff > 0
				while not cond:
					idx += 1
					diff = prime_numbers[idx] - total
					cond = not isPrime(diff) and diff > 0
				y[i][j] = diff
	last_col = 0
	for i in range(n):
		last_col += y[i][m]
	last_row = 0
	for j in range(m):
		last_row += y[n][j]
	candidate = 1
	cond = isPrime(last_col + candidate) and isPrime(last_row + candidate)
	while not cond:
		candidate += 1
		cond = isPrime(last_col + candidate) and isPrime(last_row + candidate) and (not isPrime(candidate))
	y[n][m] = candidate
	return y

def print_square(x):
	n = len(x)
	output = ''
	for i in range(n):
		output += ' '.join(list(map(str, x[i])))
		if i != n - 1:
			output += '\n'
	return output
t = int(input())
answers = []
xs = []
for _ in range(t):
	xs.append(int(input()))
base_square = [[1, 1], [1, 1]]
squares = [None, [[1]], base_square]
for i in range(2, max(xs) + 1):
	squares.append(attach_new(squares[-1]))
for x in xs:
	answers.append(print_square(squares[x]))
for answer in answers:
	print(answer)


import sys, io, os, time
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109]
for ii in range(int(input())):
	n = int(input())
	ptr = 0
	arr = []
	arr.append(10861 - 1)
	arr.append(1)
	while n - 2:
		arr.append(0)
		n -= 1
	ans = []
	for i in range(len(arr)):
		ans.append(arr)
		arr = arr[-1:] + arr[:len(arr) - 1]
	for i in ans:
		print(*i)


num_tests = int(input())
for t in range(num_tests):
	n = int(input())
	for c in range(n):
		lst = []
		for r in range(n):
			if c == r or c == r + 1 or (c == 0 and r == n - 1):
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()


import sys
import math
from fractions import Fraction

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def lts(l):
	s = ''.join(map(str, l))
	return s

def stl(s):
	l = list(s)
	return l

def sq(a, target, arr=[]):
	s = sum(arr)
	if s == target:
		return arr
	if s >= target:
		return
	for i in range(len(a)):
		n = a[i]
		remaining = a[i + 1:]
		ans = sq(remaining, target, arr + [n])
		if ans:
			return ans
mod = int(1000000000.0) + 7

def sinp():
	return sys.stdin.readline().strip()

def iinp():
	return int(input())

def ninp():
	return map(int, sys.stdin.readline().strip().split())

def llinp():
	return list(map(int, sys.stdin.readline().strip().split()))

def p(xyz):
	print(xyz)

def p2(a, b):
	print(a, b)
for _ in range(iinp()):
	n = iinp()
	mat = [[0 for i in range(n)] for j in range(n)]
	if n % 2 == 0:
		k = 0
		for i in range(n):
			mat[i][k] = 1
			mat[i][n - 1 - k] = 4
			k = k + 1
		for i in mat:
			print(*i)
	else:
		k = 0
		for i in range(n):
			mat[i][k] = 1
			k = k + 1
			if k == n:
				k = 0
			mat[i][k] = 6
		for i in mat:
			print(*i)


t = int(input())
for i in range(t):
	n = int(input())
	p = [[0 for _ in range(n)] for __ in range(n)]
	for j in range(n // 2):
		p[j][j] = 1
		p[n - 1 - j][j] = 1
		p[j][n - 1 - j] = 1
		p[n - 1 - j][n - 1 - j] = 1
	if n % 2:
		p[n // 2][n // 2] = 1
		p[n // 2 - 1][n // 2] = 1
		p[n // 2][n // 2 + 1] = 1
	for j in range(n):
		print(*p[j])


from math import sqrt

def prime(x):
	for i in range(2, int(sqrt(x)) + 1):
		if x % i == 0:
			return False
	return True

def nexxtprime(x):
	x0 = x
	while True:
		x += 1
		if prime(x) and (not prime(x - x0)):
			return x - x0
for i in range(int(input())):
	n = int(input())
	k = n - 1
	s = nexxtprime(k)
	for i in range(n):
		for j in range(n):
			if i == j:
				print(s, end=' ')
			else:
				print(1, end=' ')
		print()


def is_prime(n):
	d = 2
	while d * d <= n:
		if n % d == 0:
			return False
		d += 1
	return n >= 2
for _ in range(int(input())):
	n = int(input())
	a = 0
	for i in range(1, 100000):
		if not is_prime(i) and is_prime(n - 1 + i):
			a = i
			break
	b = 0
	for i in range(1, 100000):
		if not is_prime(i) and is_prime(a * (n - 1) + i):
			b = i
			break
	for i in range(n - 1):
		print(*[1] * (n - 1) + [a])
	print(*[a] * (n - 1) + [b])


import sys
import copy
t = int(input())
for tc in range(t):
	n = int(input())
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if j == i or j == (i + 1) % n:
				ans[i][j] = 1
			else:
				ans[i][j] = 0
	for i in ans:
		for j in i:
			print(j, end=' ')
		print()


def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		ans = [[0 for i in range(n)] for i in range(n)]
		ans[0][n - 1] = 1
		ans[0][0] = 1
		for i in range(1, n):
			ans[i][i - 1] = 1
			ans[i][i] = 1
		for i in range(n):
			print(*ans[i])
main()


game = int(input())
for i in range(game):
	n = int(input())
	for i in range(n):
		if i == n - 1:
			print(1, end='')
			print(' 0' * (n - 2), end=' ')
			print(1)
		else:
			print('0 ' * i, end='')
			print(1, 1, end='')
			print(' 0' * (n - i - 2))


def isprime(n):
	if n == 1:
		return False
	elif n == 2:
		return True
	else:
		for x in range(2, n):
			if n % x == 0:
				return False
		return True
for i in range(int(input())):
	n = int(input())
	l = []
	ind = 1
	l.append(1)
	while 1:
		if len(l) == n:
			break
		elif isprime(ind):
			pass
		elif ind % 2 == 0:
			l.append(ind)
		ind += 1
	s = sum(l)
	if isprime(s):
		l2d = []
		for i in range(n):
			l2 = l.copy()
			l2d.append(l2)
			z = l.pop()
			l.insert(0, z)
		for i in l2d:
			print(*i)
	else:
		while isprime(sum(l)) != True:
			l[-1] += 1
			if isprime(l[-1]):
				l[-1] += 1
		l2d = []
		for i in range(n):
			l2 = l.copy()
			l2d.append(l2)
			z = l.pop()
			l.insert(0, z)
		for i in l2d:
			print(*i)


def check_prime(n):
	check = 0
	for i in range(1, n // 2 + 1):
		if n % i == 0:
			check += 1
	if check == 1:
		return True
	else:
		return False
for _ in range(int(input())):
	n = int(input())
	if check_prime(n) == True:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		next_prime = 0
		for i in range(n, 10 ** 9):
			if check_prime(i) == True:
				next_prime = i
				if check_prime(next_prime - (n - 1)) == True:
					continue
				else:
					break
		for i in range(n):
			for j in range(n):
				if i == j:
					print(next_prime - (n - 1), end=' ')
				else:
					print(1, end=' ')
			print()


def isprime(n):
	if n <= 1:
		return 1
	if n <= 3:
		return 1
	if n % 2 == 0 or n % 3 == 0:
		return 0
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return 0
		i = i + 6
	return 1

def prime(k):
	i = k + 1
	while 1:
		if isprime(i) == 1:
			if isprime(i - k) == 0:
				return i
		i += 1
	return -1
for _ in range(int(input())):
	n = int(input())
	x = prime(n - 1) - (n - 1)
	for i in range(n):
		for j in range(n):
			if i == j:
				print(x, end=' ')
			else:
				print(1, end=' ')
		print()


def printPrimeSquare(n):
	for i in range(n):
		for j in range(n):
			if j == i or j == i + 1 or (j == 0 and i == n - 1):
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()
t = int(input())
while t:
	n = int(input())
	printPrimeSquare(n)
	t -= 1


for s in [*open(0)][1:]:
	n = int(s)
	r = range(n)
	for i in r:
		print(*(int(j in (i, (i + 1) % n)) for j in r))


import os, sys
from os import curdir
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def get_input(a=str):
	return a(input())

def get_int_input():
	return get_input(int)

def get_input_arr(a):
	return list(map(a, input().split()))

def get_int_input_arr():
	return get_input_arr(int)

def sieve(n):
	primes = [-1] * n
	primes[0] = -1
	primes[1] = -1
	i = 2
	sqrt_n = int(n ** 0.5 + 1)
	prime_nums = []
	while i <= sqrt_n:
		if primes[i] == -1:
			primes[i] = i
			prime_nums.append(i)
			for j in range(i * i, n, i):
				primes[j] = 2
		i += 1
	for i in range(sqrt_n, n):
		if primes[i] == -1:
			primes[i] = i
			prime_nums.append(i)
	return prime_nums
import bisect

def solve():
	prime_nums = sieve(10 ** 5)
	prime_set_val = set(prime_nums)
	t = get_int_input()
	for _ in range(t):
		n = get_int_input()
		ones_val = n - 1
		next_val = float('inf')
		limit = 10
		while True and limit:
			for i in range(1, 10 ** 5):
				if i not in prime_set_val and i + ones_val in prime_set_val:
					next_val = i
					break
			if next_val != float('inf'):
				break
		grid = []
		for i in range(n):
			row = []
			for j in range(n):
				if i == j:
					row.append(next_val)
				else:
					row.append(1)
			grid.append(row)
		for i in range(n):
			cout << ' '.join(map(str, grid[i])) << endl

def main():
	solve()
main()


primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927]
primes_set = set(primes)
import bisect

def solution(n):
	e = 0
	i = bisect.bisect_left(primes, n)
	while i < len(primes):
		if primes[i] - n + 1 not in primes_set:
			e = primes[i] - n + 1
			break
		i += 1
	arr = [1] * (n - 1)
	arr.append(e)
	return arr
for _ in range(int(input())):
	n = int(input())
	arr = solution(n)
	for i in range(n):
		print(' '.join(map(str, arr[i:] + arr[:i])))


for i in range(int(input())):
	n = int(input())
	arr = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i + j == n - 1:
				arr[i][j] = 1
	if n % 2 != 0:
		for i in range(n):
			for j in range(n):
				if i == n // 2 and j == n // 2:
					arr[i][j] = 1
				if i == 0 and j == n // 2:
					arr[i][j] = 1
				if i == n // 2 and j == n - 1:
					arr[i][j] = 1
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()


t = int(input())
primes = [2]
for i in range(3, 202, 2):
	check = 0
	for num in primes:
		if i % num == 0:
			check += 1
			break
	if check == 0:
		primes.append(i)
for i in range(t):
	n = int(input())
	matrix = []
	if n in primes:
		for c in range(n):
			numlist = []
			for x in range(n):
				numlist.append(1)
			matrix.append(numlist)
		for lists in matrix:
			print(*lists, sep=' ')
	elif n % 2 == 0:
		for x in range(int(n / 2)):
			numlist = []
			for c in range(n):
				if c == x * 2 or c - 1 == x * 2:
					numlist.append(1)
				else:
					numlist.append(0)
			matrix.append(numlist)
			matrix.append(numlist)
		for lists in matrix:
			print(*lists, sep=' ')
	else:
		m = n - 1
		for x in range(int(m / 2)):
			numlist1 = []
			numlist2 = []
			for c in range(n):
				if x == 0 and c == n - 1:
					numlist1.append(1)
					numlist2.append(0)
				elif c == x * 2 or c - 1 == x * 2:
					numlist1.append(1)
					numlist2.append(1)
				else:
					numlist1.append(0)
					numlist2.append(0)
			matrix.append(numlist1)
			matrix.append(numlist2)
		numlist = []
		for x in range(n):
			if x == 0:
				numlist.append(1)
			elif x == n - 1:
				numlist.append(4)
			else:
				numlist.append(0)
		matrix.append(numlist)
		for lists in matrix:
			print(*lists, sep=' ')


def prime(n):
	c = 0
	for i in range(2, int(pow(n, 0.5)) + 1):
		if n % i == 0:
			c = 1
			break
	if c == 0:
		return 0
	else:
		return 1
for t in range(int(input())):
	n = int(input())
	if n == 2:
		print('1 1')
		print('1 1')
		continue
	elif n == 3:
		print('1 1 1')
		print('1 1 1')
		print('1 1 1')
		continue
	if prime(n) == 0:
		for i in range(n):
			s = '1 '
			print(s * n)
	else:
		for i in range(n + 1, 200):
			if prime(i) == 0:
				r = i - n + 1
				if prime(r) == 1:
					break
		l1 = [1] * n
		for i in range(n):
			l1[i] = r
			print(*l1)
			l1[i] = 1


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			line = ''
			for j in range(n // 2):
				if j == i // 2:
					line += '1 1 '
				else:
					line += '0 0 '
			print(line[:-1])
	else:
		for i in range(n - 1):
			line = ''
			for j in range(n // 2):
				if j == i // 2:
					line += '1 1 '
				else:
					line += '0 0 '
			if i == 0 or i == 1:
				line += '1'
			else:
				line += '0'
			print(line)
		print('1 1' + ' 0' * (n - 2))


for _ in range(int(input())):
	n = int(input())
	for i in range(n - 1):
		for j in range(n):
			if i == j or i + 1 == j:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print('')
	for i in range(n):
		if i == 0 or i == n - 1:
			print(1, end=' ')
		else:
			print(0, end=' ')
	print('')


import sys
from io import IOBase, BytesIO
from os import read, write, fstat
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size: int=...):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def split_input():
	return stdin.readline().split()

def _input():
	return stdin.readline()

def build_simple_square(size):
	square = [[0] * size for _ in range(size)]
	for i in range(size):
		square[i][i] = 1
	for j in range(size - 1):
		square[j][j + 1] = 1
	square[-1][0] = 1
	return square

def main():
	t = int(_input())
	for _ in range(t):
		n = int(_input())
		square = build_simple_square(n)
		for i in range(n):
			stdout.write(' '.join(map(str, square[i])) + '\n')
main()


t = int(input())
for i in range(t):
	n = int(input())
	s = '0' * (n - 2) + '11'
	for j in range(n):
		s1 = s[j:] + s[:j]
		for ch in s1:
			print(ch, end=' ')
		print()


import sys
t = int(sys.stdin.readline().strip())
for h in range(t):
	n = int(sys.stdin.readline().strip())
	arr = [[0 for j in range(n)] for i in range(n)]
	tl = [0, 0]
	bl = [n - 1, 0]
	tr = [0, n - 1]
	br = [n - 1, n - 1]
	pt = [tl, bl, tr, br]
	for k in range(n // 2):
		for j in range(len(pt)):
			arr[pt[j][0]][pt[j][1]] = 1
		pt[0][0] += 1
		pt[0][1] += 1
		pt[1][0] -= 1
		pt[1][1] += 1
		pt[2][0] += 1
		pt[2][1] -= 1
		pt[3][0] -= 1
		pt[3][1] -= 1
	if n % 2 == 1:
		arr[n // 2][0] = 1
		arr[0][n // 2] = 1
		arr[n - 1][n // 2] = 1
		arr[n // 2][n - 1] = 1
	for j in arr:
		for k in j:
			print(k, end=' ')
		print('')


def isprime(num):
	if num == 2 or num == 3:
		return True
	elif num % 2 == 0 or num % 3 == 0:
		return False
	i = 5
	while i * i <= num:
		if num % i == 0 or num % (i + 2) == 0:
			return False
		i += 6
	return True
count = 1
l1 = [1]
i = 4
add = 1
while count < 200:
	if isprime(add + i) == True:
		add += i
		l1.append(i)
		count += 1
	i += 2
out = 0
for i in l1:
	out += i
t = int(input())
for j in range(0, t):
	n = int(input())
	l2 = []
	for i in range(0, n):
		l2.append(l1[i])
	for i in range(0, n):
		for k in range(i, n):
			print(l2[k], end=' ')
		for k in range(0, i):
			print(l2[k], end=' ')
		print()


import sys, collections
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = [[0] * n for _ in range(n)]
	for i in range(n):
		a[i][i] = 1
		a[i][~i] = 1
	if n % 2:
		a[n // 2 - 1][n // 2] = 1
		a[n // 2][n // 2 + 1] = 1
	for i in range(n):
		for j in range(n):
			if j < n - 1:
				print(a[i][j], end=' ')
			else:
				print(a[i][j])


t = int(input())
answ = []
list = []
for i in range(0, t):
	i = int(input())
	list.append(i)
for i in list:
	j = 0
	for h in range(0, i):
		a = []
		for k in range(0, i):
			if j == i - 1:
				if k == 0 or k == j:
					a.append(1)
				else:
					a.append(0)
			elif k == j or k == j + 1:
				a.append(1)
			elif k != j or k != j + 1:
				a.append(0)
		j += 1
		for k in a:
			print(k, end=' ')
		print()


def main():
	primeSet = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2111, 4999}
	primelist = list(primeSet)
	primelist.sort()
	setLen = len(primelist)
	for _ in range(int(input())):
		n = int(input())
		primeArr = [[1 for _ in range(n)] for _ in range(n)]
		if n in primeSet:
			for i in range(n):
				print(*primeArr[i], sep=' ')
		else:
			diff = 0
			for i in range(setLen):
				if n - 1 < primelist[i] and primelist[i] - (n - 1) not in primeSet:
					diff = primelist[i] - (n - 1)
					break
			for i in range(n - 1):
				primeArr[i][n - 1] = primeArr[n - 1][i] = diff
			lastRowSum = diff * (n - 1)
			finalDiff = 0
			for i in range(setLen):
				if lastRowSum < primelist[i] and primelist[i] - lastRowSum not in primeSet:
					finalDiff = primelist[i] - lastRowSum
					break
			primeArr[n - 1][n - 1] = finalDiff
			for i in range(n):
				print(*primeArr[i], sep=' ')
main()


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()

def isp(x):
	a = 1
	for i in range(2, int(x ** 0.5) + 1):
		if x % i == 0:
			a = 0
			break
	return a
for i in range(t):
	(n,) = I()
	an = [[0] * n for i in range(n)]
	if isp(n):
		an = [[1] * n for i in range(n)]
	else:
		ar = [6] + [0] * (n - 2) + [1]
		cr = 0
		for i in range(n):
			for j in range(n):
				an[i][j] = ar[(cr + j) % n]
			cr += 1
	for i in an:
		print(*i)


import math
t = int(input())
ans = []
sub = []
inp = []
for i in range(t):
	n = int(input())
	inp.append(n)
for i in range(len(inp)):
	a = '1 1 '
	for j in range(inp[i] - 2):
		a = a + '0 '
	for j in range(inp[i]):
		sub.append(a)
		a = a[-2:] + a[:-2]
	ans.append(sub)
	sub = []
for i in range(len(ans)):
	for j in range(len(ans[i])):
		print(ans[i][j])


for case in range(int(input())):
	n = int(input())
	mat = [[0] * n for i in range(n)]
	for i in range(0, n, 2):
		if i + 1 < n:
			mat[i][i] = 1
			mat[i][i + 1] = 1
			mat[i + 1][i] = 1
			mat[i + 1][i + 1] = 1
		else:
			mat[i][i] = 1
			mat[i][i - 1] = 1
			mat[i - 1][i] = 1
	for li in mat:
		print(*li)


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	val = int(stdin.readline())
	list1 = [1, 1] + [0] * (val - 2)
	for i in range(val):
		print(*list1)
		list1 = [list1[-1]] + list1[:-1]


prime = [1] * 100001
(prime[0], prime[1]) = (0, 0)
for i in range(2, 100001):
	if prime[i]:
		j = i * i
		while j < 100001:
			prime[j] = 0
			j += i
primes = {}
comp = {}
for i in range(1, 100001):
	if prime[i]:
		primes[i] = 1
	else:
		comp[i] = 1
t = int(input())
while t > 0:
	N = int(input())
	arr = [[1] * N for _ in range(N)]
	a = 0
	for num in list(comp.keys()):
		if num + (N - 1) in primes:
			a = num
			break
	for i in range(N):
		arr[i][N - 1] = a
		arr[N - 1][i] = a
	if a * N not in primes:
		for num in list(comp.keys()):
			if num + a * (N - 1) in primes:
				arr[N - 1][N - 1] = num
				break
	for i in range(N):
		for j in range(N):
			print(arr[i][j], end=' ')
		print()
	t -= 1


def check(lol):
	i = 2
	while i * i <= lol:
		if lol % i == 0:
			return True
		i += 1
	return False
for __ in range(int(input())):
	n = int(input())
	ar = [1]
	for i in range(4, 2 * n - 1, 2):
		ar.append(i)
	kek = sum(ar)
	num = 4
	while check(kek + num):
		num += 2
	ar.append(num)
	print(*ar)
	for i in range(n - 1):
		kek = []
		for j in range(n):
			kek.append(ar[(j + 1) % n])
		ar = kek.copy()
		print(*ar)


for i in range(int(input())):
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		a[i][i] = 4
		a[i][(i + 1) % n] = 9
	for i in a:
		print(*i)


def findPrimes():
	primes = [True] * 101
	primes[0] = False
	primes[1] = False
	for i in range(2, 101):
		if primes[i]:
			for j in range(i + i, 101, i):
				primes[j] = False
	return primes
t = int(input())
primes = findPrimes()
for _ in range(t):
	n = int(input())
	if primes[n]:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
		continue
	result = [[0 for j in range(n)] for i in range(n)]
	if n % 2 == 0:
		for i in range(0, n, 2):
			result[i][i] = 1
			result[i][i + 1] = 1
			result[i + 1][i] = 1
			result[i + 1][i + 1] = 1
	else:
		for i in range(0, n - 1, 2):
			result[i][i] = 1
			result[i][i + 1] = 1
			result[i + 1][i] = 1
			result[i + 1][i + 1] = 1
		result[n - 1][n - 1] = 1
		result[n - 1][n - 2] = 1
		result[n - 1][n - 3] = 1
		result[n - 2][n - 1] = 1
	for i in range(n):
		for x in result[i]:
			print(x, end=' ')
		print()


for t in range(int(input())):
	n = int(input())
	s = [1, 1] + [0] * (n - 2)
	for i in range(n):
		print(*s)
		s = s[-1:] + s[:-1]


for tc in range(int(input())):
	n = int(input())
	array = [[1 if i == j or i + j == n - 1 else 0 for j in range(n)] for i in range(n)]
	if n % 2 == 1:
		k = n // 2
		array[k][k - 1] = 1
		array[k + 1][k] = 1
	for i in range(n):
		for j in range(n):
			array[i][j] = str(array[i][j])
		print(' '.join(array[i]))


from sys import stdin, stdout

def checker(x):
	if x == 2 or x == 3:
		return 1
	if x == 1:
		return 0
	i = 2
	while i * i <= x:
		if x % i == 0:
			return 0
		i += 1
	return 1
for _ in range(int(input())):
	n = int(input())
	array = []
	for i in range(n):
		lst = []
		for j in range(n):
			lst.append(1)
		array.append(lst)
	if checker(n) == 0:
		i = n + 1
		while True:
			if checker(i) and checker(i - n + 1) == 0:
				break
			i += 1
		cnt = i - n + 1
		for i in range(n):
			array[i][i] = cnt
	for i in array:
		print(*i)


import bisect, math

def foo(n):
	A = [[1] * n for _ in range(n)]
	i = 0
	while 1:
		if n - 1 < primes[i] and primes[i] - n + 1 not in sprimes:
			last = primes[i] - n + 1
			break
		i += 1
	for i in range(n - 1):
		A[i][-1] = last
		A[-1][i] = last
	s = last * (n - 1)
	i = 0
	while 1:
		if s < primes[i] and primes[i] - s not in sprimes:
			llast = primes[i] - s
			break
		i += 1
	A[-1][-1] = llast
	return A

def sieve(n):
	prime = [True] * (n + 1)
	p = 2
	sn = math.sqrt(n)
	while p <= sn:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	res = []
	for p in range(2, n + 1):
		if prime[p]:
			res.append(p)
	return res
primes = sieve(10 ** 5 + 1)
sprimes = set(primes)
t = int(input())
for _ in range(t):
	n = int(input())
	for row in foo(n):
		print(' '.join(map(str, row)))


ns = lambda : input()
ni = lambda : int(input())
nm = lambda : map(int, input().split())
nl = lambda : list(map(int, input().split()))
for _ in range(ni()):
	n = ni()
	if n % 2 == 0:
		(i, j) = (0, n - 1)
		a = [0] * n
		for k in range(n):
			a[i] = 1
			a[j] = 1
			for kk in range(n):
				print(a[kk], end=' ')
			print()
			a[i] = 0
			a[j] = 0
			i += 1
			j -= 1
	else:
		i = 0
		a = [0] * n
		for k in range(n):
			a[i % n] = 1
			a[(i + 1) % n] = 1
			for kk in range(n):
				print(a[kk], end=' ')
			print()
			a[i % n] = 0
			a[(i + 1) % n] = 0
			i += 1


from collections import deque
from collections import OrderedDict
from queue import PriorityQueue
from operator import attrgetter
import math
import sys
import os
import threading
import bisect
import operator
import queue
import heapq
from atexit import register
from io import BytesIO
import io
from sys import stdin, stdout
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def isComplex(n):
	sq = math.floor(math.sqrt(n))
	for i in range(2, sq + 1):
		if n % i == 0:
			return True
	else:
		return False
for _ in range(int(input())):
	n = int(input())
	ar = [[1] * n for i in range(n)]
	value = 1
	for i in range(1, 1000):
		if isComplex(i + (n - 1)) == False and isComplex(i) == True:
			value = i
			break
	for i in range(n):
		ar[i][i] = value
	for i in range(n):
		print(*ar[i], sep=' ')


def solve():
	n = int(input())
	arr = [[0 for x in range(n)] for x in range(n)]
	for i in range(n - 1):
		arr[i][i] = 15
		arr[i + 1][i] = 8
	arr[n - 1][n - 1] = 15
	arr[0][n - 1] = 8
	for x in range(n):
		for y in range(n):
			print(arr[x][y], end=' ')
		print()
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


from math import sqrt
nao_marcado = -1
nao_primo = 0
primo = 1
primos = {}
for i in range(2, 600):
	primos[i] = nao_marcado
for i in range(2, int(sqrt(600)) + 1):
	if primos[i] == nao_marcado:
		primos[i] = primo
		contador = i * 2
		while contador < 600:
			primos[contador] = nao_primo
			contador += i
for i in range(2, 600):
	if primos[i] == nao_marcado:
		primos[i] = primo
primos[0] = nao_primo
primos[1] = nao_primo

def solution(n):
	if primos[n] == primo:
		for i in range(n):
			print(' '.join(['1'] * n))
		return
	for i in range(n - 2):
		print(' '.join(['0'] * (n - 2)), end=' ')
		print('1 1')
	print(' '.join(['1'] * (n - 1)), end=' ')
	acumulo = n - 1
	resto1 = 0
	while primos[acumulo + resto1] != primo or primos[resto1] == primo:
		resto1 += 1
	print(resto1)
	print(' '.join(['1'] * (n - 2)), end=' ')
	print(resto1, end=' ')
	acumulo = n - 2 + resto1
	resto2 = 0
	while primos[acumulo + resto2] != primo or primos[resto2] == primo:
		resto2 += 1
	print(resto2)
numero_casos_teste = int(input())
casos = []
for i in range(numero_casos_teste):
	casos.append(int(input()))
for i in casos:
	solution(i)


import sys
import re
import random
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
input = sys.stdin.readline
getS = lambda : input().strip()
getN = lambda : int(input())
getList = lambda : list(map(int, input().split()))
getZList = lambda : [int(x) - 1 for x in input().split()]
INF = float('inf')
MOD = 10 ** 9 + 7
divide = lambda x: pow(x, MOD - 2, MOD)

def isprime(n):
	if n == 1:
		return False
	if n in [2, 3, 5, 7, 11, 13]:
		return True
	for i in range(2, int(math.sqrt(n) + 10)):
		if n % i == 0:
			return False
	return True
nonprimes = [x for x in range(1, 100001) if not isprime(x)]

def solve(n=0):
	if n == 0:
		n = getN()
	orn = n
	ans = 0
	for i in nonprimes:
		for j in nonprimes:
			if isprime(i + j + orn - 2):
				ans = [i, j]
				break
		if ans:
			break
	ans = [1] * (orn - 2) + ans
	ans = ans + ans
	for i in range(orn):
		print(*ans[i:i + orn])

def main():
	n = getN()
	for _ in range(n):
		solve()
	return
main()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [[0 for _ in range(n)] for i in range(n)]
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i == j or i + j == n - 1:
					a[i][j] = 1
		for i in range(n):
			for j in range(n):
				print(a[i][j], end=' ')
			print()
	elif n % 2 == 1:
		for i in range(n):
			for j in range(n):
				if i == j or i + j == n - 1:
					a[i][j] = 1
		a[n // 2 - 1][n // 2] = 1
		a[n // 2][n // 2 + 1] = 1
		for i in range(n):
			for j in range(n):
				print(a[i][j], end=' ')
			print()


import math
number = int(1500)
primes = []
for i in range(2, number + 1):
	primes.append(i)
i = 2
while i <= int(math.sqrt(number)):
	if i in primes:
		for j in range(i * 2, number + 1, i):
			if j in primes:
				primes.remove(j)
	i = i + 1
for _ in range(int(input())):
	n = int(input())
	ans = []
	if n in primes:
		ans = [[1] * n for i in range(n)]
	else:
		k = n
		for i in primes:
			if i > n and (not i - n + 1 in primes):
				k = i - n + 1
				break
		ans = [[1] * n for i in range(n)]
		for i in range(n):
			for j in range(n):
				if i == j:
					ans[i][j] = k
	for i in ans:
		print(*i)


def chk_prime(num):
	if num % 2 == 0 and num != 2:
		return False
	for i in range(3, int(num ** 0.5) + 1, 2):
		if num % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	var = (n - 1) * 10
	ans = [10] * (n - 1)
	i = var + 1
	while 1:
		if chk_prime(i):
			if i - var == 1 or chk_prime(i - var) == False:
				ans.append(i - var)
				break
		i += 1
	k = 0
	for i in range(n):
		for j in range(n):
			print(ans[(j + k) % n], end=' ')
		print('')
		k += 1


from collections import deque
PRIME_NUMBER = 211
t = int(input())

def calculate_first_row(n):
	return deque([1, PRIME_NUMBER - 1] + [0] * (n - 2))
for _ in range(t):
	n = int(input())
	row = calculate_first_row(n)
	for i in range(n):
		row.rotate(1)
		for elem in row:
			print(elem, end=' ')
		print()


for i in range(int(input())):
	n = int(input())
	ans = [[0] * n for i in range(n)]
	s = ('1 4' + ' 0' * (n - 2) + ' ') * 2
	index = 0
	for i in range(n):
		ans[i] = list(map(int, s[index:index + 2 * (n - 1) + 1].split()))
		index += 2
	for i in ans:
		print(*i)


siv = [1 for n in range(500)]
for n in range(2, 500):
	if siv[n]:
		for k in range(n * n, 500, n):
			siv[k] = 0
prm = []
for n in range(2, 500):
	if siv[n]:
		prm.append(n)

def solv():
	x = int(input())
	if x == 2 or x == 3:
		for n in range(x):
			for k in range(x):
				print(1, end=' ')
			print()
		return
	v = x - 1
	y = -1
	for n in prm:
		if n > v and siv[n - (x - 1)] == 0:
			y = n - (x - 1)
			break
	for n in range(x):
		for k in range(x):
			if n == k:
				print(y, end=' ')
			else:
				print(1, end=' ')
		print()
for n in range(int(input())):
	solv()


import sys
for i in range(int(sys.stdin.readline())):
	a = int(sys.stdin.readline())
	num = 1
	while True:
		temp = num + (a - 1)
		stat = True
		for z in range(2, temp // 2 + 2):
			if temp % z == 0:
				stat = False
				break
		if stat == True:
			stat = False
			for p in range(2, num // 2 + 2):
				if num % p == 0:
					stat = True
					break
			if num == 2:
				stat = False
			if stat == True:
				break
			num += 1
		else:
			num += 1
	ans = ''
	for p in range(a):
		text = ''
		for z in range(a):
			if z == p:
				text += str(num) + ' '
			else:
				text += str(1) + ' '
		ans += text + '\n'
	sys.stdout.write(ans)


cases = int(input())
for _ in range(cases):
	n = int(input())
	for i in range(n):
		if i == n - 1:
			print(*[1] + [0] * (n - 2) + [1])
		else:
			print(*[0] * i + [1, 1] + [0] * (n - i - 2))


for _ in range(int(input())):
	n = int(input())
	l = [1, 4] + [0] * (n - 2)
	for i in range(n):
		print(*l[i:] + l[:i], sep=' ')


from sys import stdin, exit
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	for x in range(n - 1):
		for y in range(n):
			if x == y or x == y - 1:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()
	print(1, ' ', '0 ' * (n - 2), 1, sep='')


def pc(n):
	if n == 1 or n == 0 or (n % 2 == 0 and n > 2):
		return False
	else:
		for i in range(3, int(n ** (1 / 2)) + 1, 2):
			if n % i == 0:
				return False
		return True
t = int(input())
while t:
	t -= 1
	n = int(input())
	if pc(n):
		l = [1 for _ in range(n)]
		for i in range(n):
			print(*l)
	else:
		i = n + 1
		j = 2
		while not pc(i) or pc(j):
			i += 1
			j += 1
		temp = j * (n - 1)
		ss = temp + 1
		st = 1
		while not pc(ss) or pc(st):
			ss += 1
			st += 1
		l = [1 for _ in range(n - 1)]
		l.append(j)
		ll = [j for _ in range(n - 1)]
		ll.append(st)
		for i in range(n):
			if i != n - 1:
				print(*l)
			else:
				print(*ll)


t = int(input())
for you in range(t):
	n = int(input())
	l = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				l[i][j] = 9
	for i in range(n):
		for j in range(n):
			if i + j == n - 1 and l[i][j] == 0:
				l[i][j] = 8
	if n % 2 == 1:
		l[n // 2 - 1][n // 2] = 14
		l[n // 2 + 1][n // 2] = 14
		l[n // 2][n // 2 - 1] = 14
		l[n // 2][n // 2 + 1] = 14
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()


def checkprime(e):
	for i in range(2, e):
		if e % i == 0:
			return False
	return True
t = int(input())
for _ in range(t):
	e = int(input())
	if e == 2:
		print(1, 1)
		print(1, 1)
		continue
	x = 0
	check = 0
	s = 1
	while check == 0:
		if checkprime(s) == True and checkprime(s - (e - 1)) == False:
			x = s
			check = 1
		else:
			s += 1
	arr = [['0' for i in range(e)] for j in range(e)]
	for j in range(e):
		for k in range(e):
			if j + k == e - 1:
				arr[j][k] = x - (e - 1)
			else:
				arr[j][k] = 1
	for j in range(e):
		for k in range(e):
			if k != e - 1:
				print(arr[j][k], end=' ')
			else:
				print(arr[j][k])


for _ in ' ' * int(input()):
	a = int(input())
	for i in range(a - 1):
		for j in range(a):
			if j == i or j == i + 1:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()
	z = [0] * a
	z[0] = z[-1] = 1
	print(*z)


from sys import stdin

def prime(n):
	if n == 1:
		return False
	for k in range(2, n // 2 + 1):
		if n % k == 0:
			return False
	return True

def main(n):
	square = [n * [1] for _ in range(n)]
	val = 1
	while prime(val) or not prime(val + n - 1):
		val += 1
	for i in range(n):
		square[i][i] = val
	for l in square:
		print(' '.join(map(str, l)))
t = int(stdin.readline())
cases = [int(stdin.readline()) for _ in range(t)]
for n in cases:
	main(n)


def solve():
	n = int(input().strip())
	prime_square = []
	for _ in range(n):
		prime_square.append(['0'] * n)
	for j in range(n // 2):
		prime_square[j][j] = '1'
		prime_square[j][n - 1 - j] = '1'
		prime_square[n - 1 - j][j] = '1'
		prime_square[n - 1 - j][n - 1 - j] = '1'
	if n & 1:
		l = n // 2
		prime_square[l][l] = '1'
		prime_square[l - 1][l] = '1'
		prime_square[l][n - 1] = '1'
	for j in range(n):
		print(' '.join(prime_square[j]))
for _ in range(int(input().strip())):
	solve()


pn = 1000
x = [True for _ in range(pn)]
p = []
for i in range(2, pn):
	if x[i]:
		p.append(i)
		for j in range(i, pn, i):
			x[j] = False
for _ in range(int(input())):
	n = int(input())
	for pp in p:
		k = pp - (n - 1)
		if k > 0 and k not in p:
			for i in range(n):
				ll = [1 for _ in range(n)]
				ll[i] = k
				print(' '.join([str(j) for j in ll]))
			break


import sys
import bisect as bi
import math as mt
import collections as cc
import heapq as hp
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
N = 101
pr = [0] * N
for i in range(2, N):
	if not pr[i]:
		for j in range(i * 2, N, i):
			pr[j] = 1
for tc in range(int(input())):
	(n,) = I()
	if not pr[n]:
		a = [1] * n
		for i in range(n):
			print(*a)
	else:
		x = 0
		y = 1
		ans = [[0] * n for i in range(n)]
		for i in range(n):
			for j in range(n):
				if j == x or j == y:
					ans[i][j] = 1
			x += 1
			y += 1
			x %= n
			y %= n
		for i in ans:
			print(*i)


primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223]
for _ in range(int(input())):
	n = int(input())
	if n in primes:
		ans = [1] * n
		for i in range(n):
			print(*ans)
	else:
		for i in primes:
			if i > n:
				diff = i - n + 1
				if diff not in primes:
					break
		ans = [1] * (n - 1) + [diff]
		for i in range(n):
			res = ans[i:] + ans[:i]
			print(*res)


def check(no):
	for i in range(2, no // 2 + 1):
		if no % i == 0:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	lis = [113, 127, 137, 139, 227, 229, 313, 433, 607, 619, 613, 167, 173, 181, 269, 181, 571, 251, 257, 733]
	for item in lis:
		if check(item - n + 1):
			continue
		else:
			val = item
			break
	for i in range(n):
		for j in range(n):
			if i == j:
				print(val - n + 1, end=' ')
			else:
				print(1, end=' ')
		print()


def solve(n):
	a = [[0] * n for _ in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i == j - 1 or i == j + 1:
				a[i][j] = 1
			else:
				a[i][j] = 0
	return a
t = int(input())
while t:
	n = int(input())
	k = solve(n)
	for x in k:
		print(*x)
	t -= 1


from sys import stdin, stdout
from math import factorial as f
from random import randint
for _ in range(int(input())):
	n = int(input())
	if n & 1:
		p = [1] * (n - 2) + [4] + [99991 - n - 2]
	else:
		p = [1] * (n - 1) + [99991 - n + 1]
	for i in range(n):
		print(*p)
		p = p[1:] + [p[0]]


t = int(input())
for i in range(t):
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	for j in range(n):
		a[j][j] = 1
		a[j][n - j - 1] = 1
	if n % 2 == 0:
		k = 0
		j = 0
		while j < n:
			print(a[j][k], end=' ')
			k += 1
			if k == n:
				k = 0
				j += 1
				print()
	else:
		a[0][n // 2] = 1
		a[n // 2][0] = 1
		k = 0
		j = 0
		while j < n:
			print(a[j][k], end=' ')
			k += 1
			if k == n:
				k = 0
				j += 1
				print()


def isPrime(d):
	fl = True
	it = 2
	while it * it <= d:
		if it * it == d:
			return False
		elif d % it == 0:
			return False
		it += 1
	return True
for _ in range(int(input())):
	n = int(input())
	m = 1
	for i in range(1, 10000000):
		if isPrime(n - 1 + i) and (not isPrime(i)):
			m = i
			break
	ang = 1
	for i in range(1, 10000000):
		if isPrime((n - 1) * m + i) and (not isPrime(i)):
			ang = i
			break
	for i in range(n - 1):
		print(*[1] * (n - 1) + [m])
	print(*[m] * (n - 1) + [ang])


import time, math as mt, bisect as bs, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * (2 * 10 ** 5 + 5)
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(2 * 10 ** 5 + 5):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return

def readTree(n, e):
	adj = [set() for i in range(n + 1)]
	for i in range(e):
		(u1, u2) = IP()
		adj[u1].add(u2)
	return adj
mod = 10 ** 9 + 7

def solve():
	n = II()
	prm = sieve()
	mat = [[0 for j in range(n)] for i in range(n)]
	cnt = 0
	for i in range(n):
		mat[i][i] = 1
		mat[i][(i + 1) % n] = 1
	for ele in mat:
		print(*ele)
	return
t = II()
for i in range(t):
	solve()


for i in range(int(input())):
	n = int(input())
	for i in range(n):
		for j in range(n):
			print((abs(i - j) == 1) + (i + j == 0) + (i + j == 2 * n - 2), end=' ')
		print()


import math
n = 400000
array = [True for i in range(n + 1)]
for i in range(2, int(math.sqrt(n)) + 1):
	if array[i] == True:
		j = 2
		while i * j <= n:
			array[i * j] = False
			j += 1
data = [1, 1]
array[1] = False
now = 1
summary = 2
for i in range(2, 1000):
	if array[now] == True:
		now += 1
		continue
	if array[summary + now]:
		data.append(now)
		summary += now
	else:
		now += 1
for _ in range(int(input())):
	n = int(input())
	now = data[:n]
	now = now + now
	for i in range(n):
		for j in range(i, n + i):
			print(now[j], end=' ')
		print()


def check_prime(num):
	if num == 1:
		return False
	for i in range(2, num):
		if num % i == 0:
			return False
	else:
		return True
for _ in range(int(input())):
	n = int(input())
	if check_prime(n):
		a = [1] * n
		for i in range(n):
			print(*a)
	else:
		s = n + 1
		while check_prime(s - n + 1):
			s += 1
			while not check_prime(s):
				s += 1
		s = s - n + 1
		a = [1] * n
		a[0] = s
		print(*a)
		for i in range(1, n):
			(a[i - 1], a[i]) = (a[i], a[i - 1])
			print(*a)


import math

def solve(n):
	ans = [[1 for _ in range(n)] for _ in range(n)]
	if isPrime(n):
		for line in ans:
			for index in range(n):
				if index == n - 1:
					print(line[index])
				else:
					print(line[index], end=' ')
	else:
		count = 1
		while isPrime(count) or not isPrime(n + count - 1):
			count += 1
		for i in range(n):
			ans[i][i] = count
		for line in ans:
			for index in range(n):
				if index == n - 1:
					print(line[index])
				else:
					print(line[index], end=' ')

def isPrime(x):
	if x < 0:
		return False
	if x == 0:
		return False
	if x == 1:
		return False
	if x < 4:
		return True
	if x % 2 == 0:
		return False
	if x < 9:
		return True
	if x % 3 == 0:
		return False
	x_sqrt = int(math.sqrt(x))
	f = 5
	while f <= x_sqrt:
		if x % f == 0:
			return False
		if x % (f + 2) == 0:
			return False
		f += 6
	return True
t = int(input())
for r in range(t):
	n = int(input())
	solve(n)


import bisect
l = [0] * 200
for i in range(2, 200):
	if l[i] == 0:
		for j in range(2 * i, 200, i):
			l[j] = 1
p = []
for i in range(2, 200):
	if l[i] == 0:
		p.append(i)
t = int(input())
for _ in range(t):
	n = int(input())
	j = bisect.bisect_left(p, n)
	for y in range(j, 200):
		x = p[y] - (n - 1)
		if x not in p:
			break
	for i in range(n):
		for j in range(n - 1):
			if i == j:
				print(x, end=' ')
			else:
				print(1, end=' ')
		if i == n - 1:
			print(x)
		else:
			print(1)


from sys import stdin
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	ans = [[1 for i in range(n)] for j in range(n)]
	if n % 2 == 0 and n != 100:
		for i in range(n):
			ans[i][i] = 101 - (n - 1)
			print(*ans[i])
	elif n == 100:
		for i in range(n):
			ans[i][i] = 4
			print(*ans[i])
	else:
		ans = [[0 for i in range(n)] for j in range(n)]
		if n > 3:
			for i in range(0, n - 4, 2):
				ans[i][i // 2] = 1
				ans[i][n - 1 - i // 2] = 1
				ans[i + 1][i // 2] = 1
				ans[i + 1][n - 1 - i // 2] = 1
		x = (n - 3) // 2
		ans[n - 3][x] = 1
		ans[n - 3][x + 2] = 1
		ans[n - 2][x] = 1
		ans[n - 2][x + 1] = 1
		ans[n - 1][x + 1] = 1
		ans[n - 1][x + 2] = 1
		for i in ans:
			print(*i)


for s in [*open(0)][1:]:
	r = (1, 1, *[0] * (int(s) - 2))
	for _ in r:
		print(*r)
		r = r[1:] + r[:1]


from math import *
sInt = lambda : int(input())
mInt = lambda : map(int, input().split())
lInt = lambda : list(map(int, input().split()))
t = sInt()
for _ in range(t):
	s = sInt()
	c = 0
	for i in range(s - 1):
		a = [0 for j in range(s)]
		a[c] = 1
		a[c + 1] = 1
		c += 1
		print(*a, sep=' ')
	a = [0 for i in range(s)]
	a[0] = 1
	a[-1] = 1
	print(*a, sep=' ')


for _ in range(int(input())):
	n = int(input())
	l = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		l[i][i] = 1
		l[i][n - i - 1] = 1
	if n & 1:
		l[0][n // 2] = 1
		l[n - 1][n // 2] = 1
		l[n // 2][0] = 1
		l[n // 2][n - 1] = 1
	for i in l:
		print(*i)


t = int(input())
for test_case in range(t):
	n = [int(x) for x in input().split()]
	n = n[0]
	if n % 2 == 0:
		torf = 1
	else:
		torf = 2
	point1 = 0
	point2 = 0
	for i in range(n):
		res = []
		point1 = 0
		for i in range(n):
			if point1 == point2 or point1 == point2 + 1 or point1 == point2 - 1:
				res.append(1)
			else:
				res.append(0)
			point1 += 1
		print(*res)
		point2 += 1


t = int(input())
primes = 9851
for _ in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(1, 1)
		print(1, 1)
	elif n % 2:
		ans = [0] + [1] * (n - 2) + [primes - (n - 2)]
		for i in range(n):
			print(*ans)
			ans = [ans[-1]] + ans[0:-1]
	else:
		ans = [1] * (n - 1) + [primes - (n - 1)]
		for i in range(n):
			print(*ans)
			ans = [ans[-1]] + ans[0:-1]


import sys
input = sys.stdin.readline
import math
x = 10000
L = math.floor(math.sqrt(x))
Primelist = [i for i in range(x + 1)]
Primelist[1] = 0
for i in Primelist:
	if i > L:
		break
	if i == 0:
		continue
	for j in range(2 * i, x + 1, i):
		Primelist[j] = 0
Primes = [Primelist[j] for j in range(x + 1) if Primelist[j] != 0]
ANS = [0] * 101
for i in range(2, 101):
	for j in range(1, 10001):
		if j in Primes:
			continue
		if j * (i - 1) + 1 in Primes:
			ANS[i] = j
			break
t = int(input())
for tests in range(t):
	n = int(input())
	A = [[1] * n for i in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				continue
			A[i][j] = ANS[n]
	for a in A:
		print(*a)


MAX = 100
p = [True for i in range(MAX)]
(p[0], p[1]) = (False, False)
for i in range(2, MAX):
	if p[i]:
		for x in range(i * i, MAX, i):
			p[x] = False
primes = set()
listprimes = []
for i in range(2, MAX):
	if p[i]:
		primes.add(i)
		listprimes.append(i)
t = int(input())
for _ in range(t):
	n = int(input())
	r = ['1', '1']
	r += ['0' for __ in range(n - 2)]
	for i in range(n):
		s = r[i:] + r[:i]
		print(' '.join(s))


T = int(input())
prime_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
for t in range(T):
	n = int(input())
	if n in prime_list:
		for i in range(n):
			to_print = [1] * n
			print(*to_print)
	else:
		for j in prime_list:
			if j in prime_list:
				if n % j == 0:
					k = n // j
					start_position = 0
					for sub_k in range(k):
						to_print = [0] * n
						for sub_j in range(start_position, start_position + j):
							to_print[sub_j] = 1
						for sub_j in range(j):
							print(*to_print)
						start_position += j
					break


t = int(input())
o = [0, 1]
checkMe = range(1, 100)
primes = []
for y in checkMe[1:]:
	x = y
	dividers = []
	for x in range(2, x):
		if (y / x).is_integer():
			dividers.append(x)
	if len(dividers) < 1:
		primes.append(y)
q = [[4, 6, 8, 1], [4, 9, 9, 9], [4, 10, 10, 65], [1, 4, 4, 4]]
for i in range(t):
	n = int(input())
	if n == 4:
		for j in range(n):
			for k in range(n):
				print(q[j][k], end=' ')
			print()
	else:
		diff = 0
		diff1 = n - 1
		for row in range(n):
			for column in range(n):
				if n % 2 == 0:
					if row == diff and column == diff1:
						print(o[1], end=' ')
						diff += 1
						diff1 -= 1
					elif row == column:
						print(o[1], end=' ')
					else:
						print(o[0], end=' ')
				elif row == diff and column == diff1:
					print(o[1], end=' ')
					diff += 1
					diff1 -= 1
				elif row == n // 2 - 1 and column == n // 2 or (column == n // 2 + 1 and row == n // 2):
					print(o[1], end=' ')
				elif row == column:
					print(o[1], end=' ')
				else:
					print(o[0], end=' ')
			print()


def isprime(n):
	prime = [True for i in range(n + 1)]
	prime[0] = prime[1] = False
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime[n]

def find(N):
	if N == 1:
		return 1
	(prime, counter) = (N + 1, 1)
	found = False
	while not found:
		if isprime(prime) and (not isprime(counter)):
			found = True
		else:
			prime += 1
			counter += 1
	return counter
for _ in range(int(input())):
	n = int(input())
	if n <= 3:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		t = find(n - 1)
		res = []
		for i in range(n):
			l = []
			for j in range(n):
				if i == j:
					l.append(t)
				else:
					l.append(1)
			res.append(l)
		for i in range(n):
			print(*res[i])


import sys
import math
input = lambda : sys.stdin.readline().rstrip()

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(input())):
	n = int(input())
	l = [[1 for i in range(n)] for j in range(n)]
	flag = 0
	for i in range(n, 10 ** 5 + n):
		if isPrime(i):
			if not isPrime(i - n + 1):
				flag = 1
				for j in range(n - 1, -1, -1):
					l[j][n - j - 1] = i - n + 1
				break
		if flag:
			break
	for i in range(n):
		print(*l[i])


t = 1
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [[0 for _ in range(n)] for _ in range(n)]
	for i in range(n):
		arr[i][i] = 1
	for i in range(n):
		if i + 1 < n:
			arr[i][i + 1] = 1
		else:
			arr[i][0] = 1
	for row in arr:
		print(*row)


primes = [2, 3, 5]
for i in range(2, 200):
	if all((i % p for p in primes)):
		primes.append(i)

def fbi(num):
	i = 0
	while primes[i] < num:
		i += 1
	return i
for t in range(int(input())):
	n = int(input())
	if n in primes:
		for i in range(n):
			print(' '.join(['1'] * n))
	else:
		i = fbi(n)
		bigi = primes[i]
		mat = ['0'] * n
		for i in range(n):
			mat[i] = '1'
			mat[(i + 1) % n] = str(bigi - 1)
			print(' '.join(mat))
			mat = ['0'] * n


rn = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rns = lambda : map(int, input().split())
rs = lambda : input()
yn = lambda x: print('Yes') if x else print('No')
YN = lambda x: print('YES') if x else print('NO')
for _ in range(rn()):
	n = rn()
	for i in range(n):
		if i == n - 1:
			a = [1]
			for j in range(1, n - 1):
				a.append(0)
			a.append(1)
			print(*a)
		else:
			a = i * [0] + [1, 1] + (n - i - 2) * [0]
			print(*a)


from collections import Counter
from collections import OrderedDict
from collections import defaultdict
import bisect

def freq_count(mylist):
	return Counter(mylist)
t = int(input())
for _ in range(t):
	n = int(input())
	grid = [[0 for x in range(n)] for x in range(n)]
	for i in range(n):
		grid[i][i] = 6
	for i in range(n):
		grid[i][n - i - 1] = 1
	if n % 2 != 0:
		grid[n // 2][-1] = 4
		grid[0][n // 2] = 4
	for i in range(n):
		print(*grid[i])


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		o = []
		for j in range(n):
			o.append(0)
		l.append(o)
	for i in range(n):
		for j in range(n):
			if i == j:
				l[i][j] = 941 - 1
				if i == n - 1:
					l[0][n - 1] = 1
				else:
					l[i + 1][j] = 1
	for i in l:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	arr = [[]]
	for i in range(n - 1):
		for j in range(n):
			if j == i or j == i + 1:
				arr[-1].append(1)
			else:
				arr[-1].append(0)
		arr.append([])
	arr[-1].append(1)
	for i in range(n - 2):
		arr[-1].append(0)
	arr[-1].append(1)
	for i in arr:
		print(*i)


for t in range(int(input())):
	n = int(input())
	nums = [[0] * n for a in range(n)]
	for i in range(n):
		nums[i][i] = 1
		nums[i][(i + 1) % n] = 1
	for line in nums:
		for a in line:
			print(a, end=' ')
		print()


temp = [True for i in range(102)]
i = 2
temp[0] = False
temp[1] = False
while i < 102:
	if temp[i] == True:
		curr = 2
		while i * curr < 102:
			temp[i * curr] = False
			curr += 1
	i += 1
for _ in range(int(input())):
	n = int(input())
	if temp[n]:
		for i in range(n):
			for j in range(n):
				print('1', end=' ')
			print()
	else:
		q = n
		while temp[q] == False:
			q += 1
		for i in range(n):
			for j in range(n):
				if i == n - 1 and j == 0:
					print(q - 1, end=' ')
				elif j == i:
					print('1', end=' ')
				elif j == i + 1:
					print(q - 1, end=' ')
				else:
					print('0', end=' ')
			print()


def Prime(Q):
	if Q < 2:
		return False
	F = 0
	for I in range(1, Q + 1):
		if Q % I == 0:
			F += 1
	return F == 2
T = int(input())
for I in range(0, T):
	N = int(input())
	if Prime(N):
		A = N
	else:
		A = 1
		while A <= N:
			if N % A == 0 and Prime(A):
				break
			A += 1
	J = 0
	while J < N:
		P = J // A * A
		Q = list('0' * N)
		Q[P:P + A] = list('1' * A)
		print(' '.join(Q))
		J += 1


import sys
import math

class Solution:

	def __init__(self):
		self.primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239]

	def run(self):
		n = int(input())
		result = []
		for j in range(n):
			result.append([1 for i in range(n)])
		if n in self.primes:
			return self.to_string(result)
		i = 0
		while self.primes[i] < n:
			i += 1
		diff = self.primes[i] - n
		while diff + 1 in self.primes:
			i += 1
			diff = self.primes[i] - n
		for i in range(n):
			result[i][i] += diff
		return self.to_string(result)

	def to_string(self, result):
		s = ''
		for r in result:
			for c in r:
				s += str(c) + ' '
			s += '\n'
		return s.strip()
t = int(input())
s = Solution()
for i in range(t):
	print(s.run())


t = int(input())
while t:
	t -= 1
	n = int(input())
	v = [['0'] * n for i in range(n)]
	for i in range(n):
		v[i][i] = '9'
		v[i][(i + 1) % n] = '4'
	print('\n'.join([' '.join(i) for i in v]))


import math

def isPrime(n):
	if n < 2:
		return False
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
t = int(input())
for p in range(t):
	n = int(input())
	if isPrime(n):
		ans = [[1 for i in range(n)] for j in range(n)]
		for i in ans:
			print(*i)
	else:
		ans = [[0 for i in range(n)] for j in range(n)]
		if n % 2 == 0:
			for i in range(n):
				ans[i][i] = 1
				ans[i][n - 1 - i] = 1
		else:
			for i in range(n):
				ans[i][i] = 1
				ans[i][n - 1 - i] = 1
			i = n // 2 - 1
			j = n // 2 - 1
			ans[i][i + 1] = 1
			ans[i + 2][i + 1] = 1
			ans[i + 1][i] = 1
			ans[i + 1][i + 2] = 1
		for i in ans:
			print(*i)


for _ in range(int(input())):
	n = int(input())
	(l, m) = (0, 1)
	arr = []
	for i in range(n):
		arr.append([1 if j == l or j == m else 0 for j in range(n)])
		l += 1
		if l == n - 1:
			m = -1
		m += 1
	for e in arr:
		print(*e)


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
T = iinp()
for _ in range(T):
	n = iinp()
	if isprime(n):
		ans = [1 for i in range(n)]
		for i in range(n):
			print(*ans)
	else:
		ans = [4 for i in range(n - 1)]
		s = sum(ans)
		ele = 1
		while True:
			s += ele
			if isprime(s) and (not isprime(ele)):
				ans.append(ele)
				break
			else:
				s -= ele
				ele += 1
		for i in range(n):
			print(*ans)
			ans = ans[-1:] + ans
			ans.pop()


def isPrime(n):
	if n == 1:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def nextPrime(n):
	cur = n
	n = 1
	while True:
		if isPrime(cur + n) and (not isPrime(n)):
			return cur + n
		n += 1
for _ in range(int(input())):
	n = int(input())
	nextPrim = nextPrime(n - 1) - (n - 1)
	matrix = [[1 for i in range(n)] for j in range(n)]
	i = 0
	j = 0
	tmp = n
	flag = True
	one = 0
	two = n - 1
	while i < n and j < n:
		if flag:
			j = one
			one += 1
			flag = False
		else:
			j = two
			two -= 1
			flag = True
		matrix[i][j] = nextPrim
		i += 1
	for row in matrix:
		print(*row)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
for x in range(0, t):
	n = int(input())
	true = True
	false = False
	if isPrime(n) == true:
		for i in range(0, n):
			for j in range(0, n):
				print(1, '', end='')
			print('')
	else:
		l = n - 1
		h = -1
		for i in range(1, 100):
			if isPrime(l + i) == true and isPrime(i) == false:
				h = i
				break
		for i in range(0, n):
			for j in range(0, n):
				if i == j:
					print(h, '', end='')
				else:
					print(1, '', end='')
			print('')


import sys
import math

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	l = []
	for i in range(n):
		l.append([0] * n)
	for i in range(n):
		for j in range(n):
			if i == j:
				l[i][j] = 1
				l[i][(j + 1) % n] = 1
		print(*l[i])


def square(n):
	arr = []
	col = 0
	for row in range(n - 1):
		arr.append([0] * n)
		arr[row][col] = 1
		arr[row][col + 1] = 1
		col += 1
	arr.append([0] * n)
	arr[-1][0] = 1
	arr[-1][-1] = 1
	return arr
T = int(input())
for t in range(T):
	n = int(input())
	ans = square(n)
	for row in ans:
		print(' '.join([str(i) for i in row]))


for _ in range(int(input())):
	n = int(input())
	for x in range(n):
		for y in range(n):
			if x == y or abs(x - y) == 1:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
from itertools import permutations, combinations

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
INF = 10 ** 9
mod = int(1000000000.0) + 7

def isPrime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True
for t in range(int(data())):
	n = int(data())
	ind = n
	while True:
		if isPrime(ind) and (not isPrime(ind - n + 1)):
			break
		ind += 1
	mat = [[1] * n for i in range(n)]
	for i in range(n):
		mat[i][i] = ind - n + 1
		print(*mat[i])


import math
import sys
from typing import List
sys.setrecursionlimit(10 ** 9)

def lmi():
	return list(map(int, input().split()))

def get_sieve_of_eratosthenes(n):
	if not isinstance(n, int):
		raise TypeError('n is int type.')
	if n < 2:
		return []
	prime = []
	limit = math.sqrt(n)
	data = [i + 1 for i in range(1, n)]
	while True:
		p = data[0]
		if limit <= p:
			return prime + data
		prime.append(p)
		data = [e for e in data if e % p != 0]

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		result = solve(n)
		for line in result:
			print(*line)

def solve(n) -> list:
	ans = [[1] * n for _ in range(n)]
	primes = set(get_sieve_of_eratosthenes(10 ** 5))
	cur_num = 4
	tate_sm = 0
	for i in range(n - 1):
		while n - 1 + cur_num not in primes or cur_num in primes:
			cur_num += 1
		ans[i][-1] = cur_num
		tate_sm += cur_num
	yoko_sm = 0
	for i in range(n - 1):
		while n - 1 + cur_num not in primes or cur_num in primes:
			cur_num += 1
		ans[-1][i] = cur_num
		yoko_sm += cur_num
	cur_num += 1
	while yoko_sm + cur_num not in primes or tate_sm + cur_num not in primes or cur_num in primes:
		cur_num += 1
	ans[-1][-1] = cur_num
	return ans

def trans(list_2d) -> list:
	return list(map(list, zip(*list_2d)))

def is_prime(x):
	if x < 2:
		return False
	if x == 2 or x == 3 or x == 5:
		return True
	if x % 2 == 0 or x % 3 == 0 or x % 5 == 0:
		return False
	prime = 7
	step = 4
	while prime <= math.sqrt(x):
		if x % prime == 0:
			return False
		prime += step
		step = 6 - step
	return True
main()


def prime(n):
	ok = True
	for i in range(2, 1 + int(n ** 0.5)):
		if n % i == 0:
			ok = False
	return ok
t = int(input())
for _ in range(t):
	n = int(input())
	a = [[0] * n for i in range(n)]
	if not n % 2:
		for i in range(n):
			a[i][i] = 1
			a[i][n - i - 1] = 1
		for i in a:
			print(*i)
	else:
		for i in range(n):
			for j in range(n):
				if i == 0 or i == n - 1 or j == 0 or (j == n - 1):
					a[i][j] = 1
		total = n - 1
		x = 0
		while not prime(total + x) or prime(x):
			x += 1
		a[n // 2][0] = x
		a[0][n // 2] = x
		a[n // 2][n - 1] = x
		a[n - 1][n // 2] = x
		total = 2 * x
		target = 0
		while not prime(total + target) or prime(target):
			target += 1
		a[n // 2][n // 2] = target
		for i in a:
			print(*i)


def is_prime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	tmp = n - 1
	res = 0
	for i in range(1, 100000):
		if is_prime(i + tmp) and (not is_prime(i)):
			res = i
			break
	for y in range(n):
		for x in range(n):
			if x != y:
				print(1, end=' ')
			else:
				print(res, end=' ')
		print()
4


import sys, math, itertools, collections, copy
input = sys.stdin.readline
N = 30000
prime = [True] * N
for i in range(2, N):
	if prime[i]:
		for j in range(i * i, N, i):
			prime[j] = False
primes = []
for i in range(2, N):
	if prime[i]:
		primes.append(i)
diff = []
K = len(primes)
for i in range(1, K):
	diff.append(primes[i] - primes[i - 1])

def solve():
	n = int(input())
	m = [4, 9]
	if n != 2:
		begin = 5
		for i in range(2, n):
			m.append(diff[begin] + diff[begin + 1] + diff[begin + 2])
			begin += 3
	for i in range(n):
		print(*m)
		arr = [0] * len(m)
		for i in range(len(m)):
			arr[(i + 1) % len(m)] = m[i]
		for i in range(len(m)):
			m[i] = arr[i]
T = int(input())
for t in range(T):
	solve()


n = 10 ** 3
primes = [True] * (n + 1)
p = {}
for i in range(2, n + 1):
	if primes[i] == True:
		p[i] = 1
		for j in range(2 * i, n + 1, i):
			primes[j] = False
for _ in range(int(input())):
	N = int(input())
	t = 1
	while p.get(N - 1 + t, None) == None:
		t += 1
		while p.get(t, None) != None:
			t += 1
	arr = [1] * (N - 1)
	ans = []
	j = 0
	for i in range(N):
		tem = arr[:]
		tem.insert(j, t)
		j += 1
		ans.append(tem)
	for i in range(N):
		print(*ans[i])


import sys
import time
import os
from math import sqrt, log, log2, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial
from copy import copy, deepcopy
from sys import exit, stdin, stdout
from collections import Counter, defaultdict, deque
from itertools import permutations
import heapq
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = 1000000007
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
intl = lambda : list(map(int, inp().split(' ')))
mint = lambda : map(int, inp().split())
flol = lambda : list(map(float, inp().split(' ')))
flush = lambda : stdout.flush()

def permute(nums):

	def fun(arr, nums, cur, v):
		if len(cur) == len(nums):
			arr.append(cur.copy())
		i = 0
		while i < len(nums):
			if v[i]:
				i += 1
				continue
			else:
				cur.append(nums[i])
				v[i] = 1
				fun(arr, nums, cur, v)
				cur.pop()
				v[i] = 0
				i += 1
		return arr
	res = []
	nums.sort()
	v = [0] * len(nums)
	return fun(res, nums, [], v)

def subsets(res, index, arr, cur):
	res.append(cur.copy())
	for i in range(index, len(arr)):
		cur.append(arr[i])
		subsets(res, i + 1, arr, cur)
		cur.pop()
	return res

def sieve(N):
	root = int(sqrt(N))
	primes = [1] * (N + 1)
	(primes[0], primes[1]) = (0, 0)
	for i in range(2, root + 1):
		if primes[i]:
			for j in range(i * i, N + 1, i):
				primes[j] = 0
	return primes

def bs(arr, l, r, x):
	if x < arr[0] or x > arr[len(arr) - 1]:
		return -1
	while l <= r:
		mid = l + (r - l) // 2
		if arr[mid] == x:
			return mid
		elif arr[mid] < x:
			l = mid + 1
		else:
			r = mid - 1
	return -1

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	p = int(sqrt(n))
	for i in range(5, p + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def solve(s):
	n = iinp()
	if isPrime(n):
		for i in range(n):
			print('1 ' * n)
	else:
		target = n - 1
		i = n - 1
		while True:
			if s[i] == 0 and s[1 * (n - 1) + i] == 1:
				ele = i
				break
			i += 1
		arr = []
		for i in range(n - 1):
			a = [1 for j in range(n - 1)]
			a.append(ele)
			arr.append(a)
		arr.append([ele for j in range(n)])
		got = ele * (n - 1)
		i = got
		while True:
			if s[i] == 0 and s[got + i] == 1:
				ele2 = i
				break
			i += 1
		arr[-1][-1] = ele2
		for i in arr:
			print(*i)
start_time = time.time()
s = sieve(10 ** 5 + 1)
for _ in range(iinp()):
	solve(s)


t = int(input())
for _ in range(t):
	n = int(input())
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		ans[i][i] = 1
	for i in range(n):
		ans[i][(i + 1) % n] = 1
	for i in range(n):
		print(*ans[i], sep=' ')


import math

def check(n):
	if n <= 1:
		return False
	if n == 2:
		return True
	for q in range(2, int(math.sqrt(n)) + 1):
		if n % q == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	data = [[0 for x in range(n)] for y in range(n)]
	for q in range(n):
		temp = 0
		for w in range(n):
			if data[q][w] == 0:
				if w == n - 1:
					for qq in range(1, 10000):
						if check(qq):
							continue
						if check(temp + qq):
							data[q][w] = qq
							break
					break
				else:
					data[q][w] = 1
					temp += 1
			else:
				temp += data[q][w]
		for w in range(n):
			data[w][q] = data[q][w]
	for q in data:
		for w in q:
			print(w, end=' ')
		print()


MAX_SIZE = 100 * 101 // 2
prime = [True] * (MAX_SIZE + 1)

def seive():
	p = 2
	while p * p <= MAX_SIZE:
		if prime[p]:
			for i in range(p * p, MAX_SIZE, p):
				prime[i] = False
		p += 1
seive()
for _ in range(int(input())):
	n = int(input())
	c = 0
	while prime[c] or not prime[n - 1 + c]:
		c += 1
	l = [1] * n
	for i in range(n):
		l[i] = c
		print(*l)
		l[i] = 1


from sys import stdin, stdout
input = stdin.readline
print = lambda x: stdout.write(str(x) + '\n')

def isprime(t):
	for i in range(2, t):
		if t % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	t = 103 - n + 1
	grid = [[1] * n for _ in range(n)]
	if isprime(t):
		t += 1
		for i in range(n):
			(grid[i][n - i - 2], grid[i][n - i - 1]) = (0, t)
	else:
		for i in range(n):
			grid[i][n - i - 1] = t
	ans = '\n'.join([' '.join(map(str, r)) for r in grid])
	print(ans)


from sys import stdin
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	A = [['0'] * n for _ in range(n)]
	for i in range(n):
		A[i][i] = '1'
		if i < n - 1:
			A[i][i + 1] = '1'
	A[n - 1][0] = '1'
	for row in A:
		print(' '.join(row))


import sys, os, io
I = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for tc in range(1, 1 + int(I())):
	n = int(I())
	for i in range(n):
		l = [1 if (i + j) % n < 2 else 0 for j in range(n)]
		print(*l)


T = int(input())
prime = [2]
A = 100
for L in range(3, A, 2):
	for L2 in prime:
		if L % L2 == 0:
			break
	else:
		prime.append(L)
for _ in range(T):
	N = int(input())
	if N in prime:
		li = [1] * N
		for i in range(N):
			print(*li)
	else:
		x = 100
		for i in range(len(prime) - 1, -1, -1):
			if prime[i] < N:
				x = prime[i]
				break
		li = [1] * x + [0] * (N - x)
		for i in range(N):
			ans = li[i:] + li[:i]
			print(*ans)


from pathlib import Path
import sys
import functools
import itertools
import re

def solve():
	prime = []
	for p in range(2, 1001):
		ok = True
		for i in range(2, p):
			if p % i == 0:
				ok = False
				break
		if ok == True:
			prime.append(p)
	for test in range(int(input())):
		n = int(input())
		if n in prime:
			for i in range(n):
				for j in range(n):
					print(1, end=' ')
				print()
		else:
			a = [[1] * n for x in range(n)]
			k = -1
			for p in prime:
				if p - n + 1 > 0 and (p - n + 1 in prime) == False:
					k = p - n + 1
					break
			for i in range(n):
				a[i][i] = k
			for i in range(n):
				for j in range(n):
					print(a[i][j], end=' ')
				print()

def main():
	if Path('test.inp').exists():
		sys.stdin = open('test.inp', 'r')
	solve()
main()


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))
import math

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math as M
MOD = 10 ** 9 + 7
from random import randint as R
p = 10000
D = [True] * (p + 1)
i = 2
while i ** 2 <= p + 1:
	if D[i] is True:
		for j in range(i ** 2, p + 1, i):
			D[j] = False
	i += 1
x = [1]
P = []
for i in range(2, len(D)):
	if D[i] is False:
		x.append(i)
	else:
		P.append(i)
for _ in range(int(input())):
	n = int(input())
	for ele in x:
		if ele + n - 1 in P:
			ans = ele
			break
	A = [1] * (n - 1) + [ans]
	for i in range(n):
		for j in range(n):
			print(A[(j + i) % n], end=' ')
		print()


for i in range(int(input())):
	n = int(input())
	s = 2 * '1' + (n - 2) * '0'
	s = 2 * s
	for j in range(n):
		for char in s[j:j + n]:
			print(char, end=' ')
		print()


def isPrime(n):
	if n <= 1:
		return False
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for y in range(int(input())):
	n = int(input())
	lst = []
	for i in range(n):
		lst.append([0] * n)
	for i in range(n):
		lst[i][i] = 9
	for i in range(1, n - 1):
		lst[i][0] = lst[0][i] = lst[n - 1][i] = lst[i][n - 1] = 1
	summ = 9 + n - 2
	for i in range(summ, summ + 5000):
		if isPrime(i) == True:
			temp = i - summ
			if isPrime(temp):
				continue
			else:
				break
	lst[0][n - 1] = lst[n - 1][0] = temp
	for i in lst:
		print(*i)


n = int(input())
for __ in range(n):
	k = int(input())
	arr = [[0] * k for __2 in range(k)]
	for i in range(k):
		arr[i][i] = 1
		arr[i][(i + 1) % k] = 1
	for line in arr:
		print(*line)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = [0] * n
	arr[0] = 1
	arr[-1] = 1
	for i in range(n):
		for j in range(n):
			print(arr[(i + j) % n], end=' ')
		print()


from collections import *
from itertools import *
from bisect import *

def inp():
	return int(input())

def arrinp():
	return [int(x) for x in input().split()]

def is_prime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def find_prime(n):
	if n <= 1:
		return 2
	if is_prime(n):
		return n
	prime = n
	found = False
	while not found:
		prime += 1
		if is_prime(prime):
			found = True
	return prime

def main():
	t = inp()
	for _ in range(t):
		n = inp()
		primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
		prime_set = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199}
		if n in prime_set:
			for i in range(n):
				temp = [1 for t in range(n)]
				print(*temp, sep=' ')
		else:
			i = bisect_left(primes, n)
			diff = primes[i] - (n - 1)
			while diff in prime_set:
				i += 1
				diff = primes[i] - (n - 1)
			edge = diff * (n - 1)
			i = find_prime(edge)
			corner_diff = i - edge
			while is_prime(corner_diff):
				i = find_prime(i + 1)
				corner_diff = i - edge
			for t in range(n - 1):
				x = [1 for temp in range(n - 1)] + [diff]
				print(*x, sep=' ')
			x = [diff for temp in range(n - 1)] + [corner_diff]
			print(*x, sep=' ')
main()


from collections import deque
import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())
prime = [0] * 10 ** 5
p = {}
for i in range(2, 10 ** 5):
	for j in range(2 * i, 10 ** 5, i):
		prime[j] = 1
for i in range(2, 10 ** 5):
	if prime[i] == 0:
		p[i] = 1
t = int(input())
for _ in range(t):
	n = int(input())
	a = [[1 for i in range(n)] for j in range(n)]
	for num in range(4, 10 ** 5):
		if num not in p.keys() and num + n - 1 in p.keys():
			for i in range(n - 1):
				a[n - 1][i] = num
				a[i][n - 1] = num
			break
	for num2 in range(4, 10 ** 5):
		if num2 not in p.keys() and num * (n - 1) + num2 in p.keys():
			a[n - 1][n - 1] = num2
			break
	for i in a:
		print(*i)


t = int(input())
for i in range(t):
	n = int(input())
	l = [4 for i in range(n - 2)]
	l.append(1)
	l.append(400 - 4 * (n - 2))
	for i in range(n):
		for j in range(n):
			print(l[(i + j) % n], end=' ')
		print()


import sys
input = sys.stdin.readline

def solve():
	n = int(input())

	def SeiveOfEratosthenes(n):
		prime = [1] * (n + 1)
		p = 2
		while p * p <= n:
			if prime[p] == 1:
				for i in range(2 * p, n + 1, p):
					prime[i] = 0
			p += 1
		prime[0] = 0
		prime[1] = 0
		ans = []
		for i in range(n):
			if prime[i] > 0:
				ans.append(i)
		return ans
	arr = SeiveOfEratosthenes(1000)
	L = [1] * (n - 1)
	for i in range(1, 1000):
		if i not in arr:
			v = n - 1 + i
			if v in arr:
				break
	L = [1] * (n - 1) + [i]
	for i in range(n):
		print(*L)
		L = [L[-1]] + L[:-1]
for _ in range(int(input())):
	solve()


l = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
for i in range(int(input())):
	n = int(input())
	if n in l:
		for j in range(n):
			print(*[1] * n)
	else:
		q = n - 1
		r = 0
		for j in range(1, 101):
			if j not in l and j + q in l:
				r = j
				break
		for j in range(n):
			w = [1] * n
			w[j] = r
			print(*w)


from math import floor, sqrt

def generateSeive():
	n = 10 ** 5 + 5
	is_prime = [True] * (n + 1)
	is_prime[1] = False
	for i in range(2, floor(sqrt(n)) + 2):
		if is_prime[i] is True:
			for j in range(2 * i, n, i):
				is_prime[j] = False
	prime_list = []
	next_prime = [-1] * n
	stack = []
	for i in range(1, n):
		if is_prime[i] is False:
			stack.append(i)
		else:
			while stack:
				index_pop = stack.pop()
				next_prime[index_pop] = i
			stack.append(i)
	return (next_prime, is_prime)
(next_prime, is_prime) = generateSeive()
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(1)
		continue
	row1 = [1] * (n - 1)
	curr_arr_sum = n - 1
	curr_val = n - 1
	to_add = next_prime[curr_val] - curr_arr_sum
	while is_prime[to_add] is True:
		curr_val = next_prime[curr_val]
		to_add = next_prime[curr_val] - curr_arr_sum
	row1.append(to_add)
	ans = [row1]
	for i in range(1, n):
		ans.append(row1[i:] + row1[:i])
	for i in ans:
		for j in i:
			print(j, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	k = 0
	for i in range(n - 1):
		for j in range(k):
			print(0, end=' ')
		print(1, 1, end=' ')
		for h in range(n - 2 - k):
			print(0, end=' ')
		print()
		k += 1
	print(1, end=' ')
	for h in range(n - 2):
		print(0, end=' ')
	print(1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	if n <= 3:
		arr = [[1] * n] * n
	else:
		r = n
		for _ in range(n):
			arr.append([0] * n)
		if n % 2 == 0:
			mid1 = n // 2 - 1
			mid2 = mid1 + 1
			count = 0
			n -= 1
			for i in range(r):
				if count < mid1:
					(arr[i][count], arr[i][n - count]) = (1, 1)
				elif count == mid1 or count == mid2:
					(arr[i][mid1], arr[i][mid2]) = (1, 1)
				else:
					(arr[i][n - count], arr[i][count]) = (1, 1)
				count += 1
		else:
			n -= 1
			mid1 = n // 2 - 1
			mid2 = mid1 + 1
			mid3 = mid2 + 1
			count = 0
			for i in range(r):
				if count < mid1:
					(arr[i][count], arr[i][n - count]) = (1, 1)
				elif count == mid1 or count == mid2 or count == mid3:
					(arr[i][mid1], arr[i][mid2], arr[i][mid3]) = (1, 1, 1)
				else:
					(arr[i][n - count], arr[i][count]) = (1, 1)
				count += 1
	for a in arr:
		print(*a)


def isPrime(n):
	x = n
	prime = True
	for i in range(2, x // 2):
		if x % i == 0:
			prime = False
			break
	return prime
tc = int(input())
for _ in range(tc):
	n = int(input())
	s = ''
	for y in range(n):
		for z in range(n):
			if y == z or y == z + 1 or (y == 0 and z == n - 1):
				s += '1 '
			else:
				s += '0 '
		print(s[0:-1])
		s = ''


t = int(input())
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(t):
	n = int(input())
	if n == 1:
		print(1)
		continue
	add = 0
	while n + add not in primes or 1 + add in primes:
		add += 1
	ans = [[1 for i in range(n)] for j in range(n)]
	for i in range(n):
		ans[i][i] += add
	for x in ans:
		print(*x)


t = int(input())
for _ in range(t):
	n = int(input())
	ans = []
	for i in range(n):
		x = []
		for j in range(n):
			x.append(0)
		ans.append(x)
	for i in range(n):
		for j in range(n):
			if i == j:
				ans[i][j] = 1
			if i == 0 and j == n - 1:
				ans[i][j] = 4
			if i == j + 1:
				ans[i][j] = 4
	for i in ans:
		print(*i)


primes = [1] * 10 ** 5
primes2 = [0] * 10 ** 5
p = []
for i in range(2, 10 ** 5):
	if primes[i]:
		for j in range(i * i, 10 ** 5, i):
			primes[j] = 0
for i in range(2, 10 ** 5):
	if primes2[i] == 0:
		primes2[i] = i
		p.append(i)
		if not primes[i]:
			print('boi u messed up')
	for j in range(len(p)):
		if p[j] > primes2[i] or p[j] * i >= 10 ** 5:
			break
		primes2[p[j] * i] = p[j]
for _ in range(int(input())):
	n = int(input())
	idx = 0
	selected = 0
	for prime in p:
		if prime <= n - 1:
			continue
		if not primes[prime - (n - 1)]:
			selected = prime - (n - 1)
			break
	output = [['1'] * n for i in range(n)]
	for i in range(n):
		output[i][i] = str(selected)
	for a in output:
		print(' '.join(a))


t = int(input())
for i in range(0, t):
	n = int(input())
	for row in range(1, n + 1):
		l = []
		for col in range(1, n + 1):
			if row == col:
				l.append(5196)
				continue
			if row + 1 == col:
				l.append(1)
				continue
			if row == n and col == 1:
				l.append(1)
			else:
				l.append(0)
		print(*l)


M = 100000
is_prime = [True] * M
is_prime[0] = is_prime[1] = False
for i in range(2, int(M ** 0.5) + 1):
	if not is_prime[i]:
		continue
	for j in range(i * 2, M, i):
		is_prime[j] = False

def main():
	N = int(input())
	if is_prime[N]:
		txt = ' '.join(('1' for _ in range(N)))
		print('\n'.join([txt] * N))
		return
	for x in range(4, N + N):
		if not is_prime[x] and is_prime[x + N - 1]:
			break
	for i in range(N):
		txt = ['1'] * N
		txt[i] = str(x)
		print(' '.join(txt))
for __ in [0] * int(input()):
	main()


from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math

def geti():
	return map(int, input().strip().split())

def getl():
	return list(map(int, input().strip().split()))

def gets():
	return input()

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def solve():
	for _ in range(geta()):
		n = geta()
		let = 232
		ans = [[0] * n for _ in range(n)]
		ans[0][-1] = 1
		for i in range(n):
			ans[i][i] = let
		j = 0
		for i in range(1, n):
			ans[i][j] = 1
			j += 1
		for i in ans:
			print(*i)
solve()


def prime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for i in range(int(input())):
	n = int(input())
	if prime(n):
		a = [[1] * n for i in range(n)]
		for i in a:
			print(*i)
	else:
		a = [[0] * n for i in range(n)]
		temp = 0
		for i in range(n):
			if i == n - 1:
				a[i][0] = 1
				a[i][temp] = 1
				continue
			a[i][temp] = 1
			a[i][temp + 1] = 1
			temp += 1
		for i in a:
			print(*i)


for t in range(int(input())):
	n = int(input())
	res = [[0 for x in range(n)] for y in range(n)]
	for i in range(n):
		res[i][i] = 1
		res[i][n - 1 - i] = 1
	if n % 2 == 1:
		res[n // 2 - 1][n // 2] = 1
		res[n // 2][n // 2 + 1] = 1
	for row in res:
		print(*row)


t = int(input())
for _ in range(t):
	n = int(input())
	p = 101
	grid = [[0 for i in range(n)] for j in range(n)]
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i == j:
					grid[i][j] = p - 1
				if i + j == n - 1:
					grid[i][j] = 1
	else:
		for i in range(n):
			for j in range(n):
				if i == j:
					grid[i][j] = p - 1
				if i + j == n - 1:
					grid[i][j] = 1
		grid[n // 2][n // 2] = 100
		grid[0][0] = 99
		grid[0][n // 2] = grid[n // 2][0] = 1
	for i in grid:
		print(*i)


import itertools
import math
import sys
import os
from collections import defaultdict

def is_debug():
	return 'PYPY3_HOME' not in os.environ

def stdin_wrapper():
	data = '2\n8\n2\n\n'
	for line in data.split('\n'):
		yield line
if not is_debug():

	def stdin_wrapper():
		while True:
			yield sys.stdin.readline()
inputs = stdin_wrapper()

def input_wrapper():
	return next(inputs)

def get_str():
	if is_debug():
		return input_wrapper()
	return input()

def get(_type):
	if _type == str:
		return get_str()
	return _type(input_wrapper())

def get_arr(_type):
	return [_type(x) for x in input_wrapper().split()]

def tuplerize(method):

	def wrap(*args, **kwargs):
		res = method(*args, **kwargs)
		if not isinstance(res, (tuple, list)):
			res = (res,)
		return res
	return wrap

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

@tuplerize
def solve(n):
	if isPrime(n):
		result = [' '.join(['1' for _ in range(n)]) for _ in range(n)]
		return '\n'.join(result)
	holder = 0
	row = [holder for _ in range(n - 2)] + [1, 1]
	result = []
	for i in range(n):
		result.append(row[i:] + row[:i])
	result_str = '\n'.join([' '.join(map(str, x)) for x in result])
	return result_str
t = get(int)
for _ in range(t):
	n = get(int)
	print(*solve(n))


from collections import deque
cases = int(input())
for t in range(cases):
	n = int(input())
	out = deque([0] * (n - 2) + [1] * 2)
	for i in range(n):
		print(*out)
		v = out.popleft()
		out.append(v)


for t in range(int(input())):
	n = int(input())
	l = []
	s = 0
	for i in range(n - 1):
		a = []
		j = 0
		while j < n:
			if j == s:
				a.append(1)
				a.append(1)
				j += 2
				s += 1
			else:
				a.append(0)
				j += 1
		l.append(a)
	a = []
	for k in range(n):
		if k == 0 or k == n - 1:
			a.append(1)
		else:
			a.append(0)
	l.append(a)
	for i in l:
		print(' '.join((str(j) for j in i)))


for _ in range(int(input())):
	n = int(input().strip())
	ans = []
	ans.append([])
	for i in range(n - 2):
		ans[0].append(0)
	ans[0].append(1)
	ans[0].append(1)
	z = n - 2
	r = 1
	for i in range(n - 2):
		row = [0] * (z - r) + [1, 1] + [0] * r
		ans.append(row)
		r += 1
	ans.append([])
	ans[-1].append(1)
	for i in range(n - 2):
		ans[-1].append(0)
	ans[-1].append(1)
	for row in ans:
		print(*row)


def cal(n):
	(start, end) = (0, n - 1)
	for i in range(n):
		arr = [0] * n
		arr[start] = 1
		arr[end] = 1
		start = (start + 1) % n
		end = (end + 1) % n
		for num in arr:
			print(num, end=' ')
		print()
t = int(input())
while t:
	n = int(input())
	cal(n)
	t -= 1


from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
import random
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10010
MOD = 10 ** 9 + 7
spf = [i for i in range(MAXN)]
spf[0] = -1

def sieve():
	for i in range(2, MAXN, 2):
		spf[i] = 2
	for i in range(3, int(MAXN ** 0.5) + 1):
		if spf[i] == i:
			for j in range(i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def fib(n, m):
	if n == 0:
		return [0, 1]
	else:
		(a, b) = fib(n // 2)
		c = a % m * (b % m * 2 - a % m) % m
		d = a % m * (a % m) % m + b % m * b % m % m
		if n % 2 == 0:
			return [c, d]
		else:
			return [d, c + d]

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN(x=' '):
	return list(map(int, sys.stdin.readline().strip().split(x)))

def ncr(n, r):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % MOD
		den = den * (i + 1) % MOD
	return num * pow(den, MOD - 2, MOD) % MOD

def flush():
	return sys.stdout.flush()
sieve()

def isprime(n):
	return spf[n] == n
for _ in range(int(input())):
	n = int(input())
	if isprime(n):
		for i in range(n):
			print('1 ' * n)
	else:
		for i in range(n + 1, MAXN):
			if isprime(i) and (not isprime(i - n + 1)):
				x = i
				break
		for i in range(n):
			for j in range(n):
				if i == j:
					print(x - n + 1, end=' ')
				else:
					print(1, end=' ')
			print()


import sys
import math
from collections import Counter
from collections import OrderedDict
from collections import defaultdict
from functools import reduce
sys.setrecursionlimit(10 ** 6)

def inputt():
	return sys.stdin.readline().strip()

def printt(n):
	sys.stdout.write(str(n) + '\n')

def listt():
	return [int(i) for i in inputt().split()]

def gcd(a, b):
	return math.gcd(a, b)

def lcm(a, b):
	return a * b // gcd(a, b)

def factors(n):
	step = 2 if n % 2 else 1
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(math.sqrt(n)) + 1, step) if n % i == 0)))

def comb(n, k):
	factn = math.factorial(n)
	factk = math.factorial(k)
	fact = math.factorial(n - k)
	ans = factn // (factk * fact)
	return ans

def is_prime(n):
	if n <= 1:
		return False
	if n == 2:
		return True
	if n > 2 and n % 2 == 0:
		return False
	max_div = math.floor(math.sqrt(n))
	for i in range(3, 1 + max_div, 2):
		if n % i == 0:
			return False
	return True
t = int(inputt())
for _ in range(t):
	n = int(inputt())
	li = [0] * n
	li2 = [0] * n
	li[-1] = 1
	li[-2] = 1
	li2[0] = 1
	li2[-1] = 1
	for i in range(n):
		print(*li)
		if li[0] != 1:
			li.remove(li[0])
			li.append(0)
		else:
			print(*li2)
			break


from math import *
import threading
import sys

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = -1
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = mid
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def graph(n, m):
	adj = dict()
	for i in range(1, n + 1):
		adj.setdefault(i, 0)
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a] += 1
		adj[b] += 1
	return adj

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	li = []
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, len(prime)):
		if prime[p]:
			li.append(p)
	return li
INF = 1000000000.0
primes = SieveOfEratosthenes(1300000)

def fn(n):
	i = 2
	res = n - 1
	val = 0
	for i in range(int(sqrt(n)) + 1, 1, -1):
		val = i - 1 + int(ceil((n - i) / i))
		if val <= res:
			res = val
		else:
			break
	return res

def solve(a, b, c):
	dis = b ** 2 - 4 * a * c
	ans1 = (-b - sqrt(dis)) / (2 * a)
	print(int(ans1))
	return
for _ in range(int(input())):
	n = int(input())
	ans = ['0'] * (n - 2)
	ans += ['1', '1']
	for i in range(n):
		temp = ans[i:] + ans[:i]
		print(*temp)


def greaterprime(x):
	for i in range(2, x // 2 + 1):
		if x % i == 0:
			return greaterprime(x + 1)
	return x
for _ in range(int(input())):
	n = int(input())
	l = ['1'] * (n - 1)
	ans = greaterprime(n) - n + 1
	while greaterprime(ans) == ans:
		ans = greaterprime(ans + n) - n + 1
	for i in range(n - 1):
		print(' '.join(l), end=' ')
		print(ans)
	for i in range(n - 1):
		print(ans, end=' ')
	if n == 2:
		print(1)
	else:
		fans = greaterprime(ans * (n - 1)) - ans * (n - 1)
		while greaterprime(fans) == fans:
			fans = greaterprime(fans + ans * (n - 1) + 1) - ans * (n - 1)
		print(fans)


dct = {}

def square(n):
	if n == 2:
		return [[1, 1], [1, 1]]
	if n == 3:
		return [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
	tmp = square(n - 2)
	ns = [[1, 1] + [0] * (n - 2), [1, 1] + [0] * (n - 2)]
	for o in range(n - 2):
		temp = [0 for k in range(n)]
		temp[2:] = tmp[o]
		ns.append(temp)
	if n not in dct:
		dct[n] = ns
	return ns
t = int(input())
ans = []
for _ in range(t):
	n = int(input())
	j = [4, 1] + (n - 2) * [0]
	y = []
	for k in range(n):
		y.append(j)
		j = [j[-1]] + j[:-1]
	ans.append(y)
for el in ans:
	for row in el:
		print(' '.join(map(str, row)))


import math

def prime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	inf = 1000000
	for i in range(n, inf):
		if prime(i):
			if prime(i - n + 1) == False:
				ans = i
				break
	for i in range(n):
		for j in range(n):
			if i == j:
				print(ans - n + 1, end=' ')
			else:
				print(1, end=' ')
		print()


from collections import deque
import sys
input = iter(sys.stdin.read().splitlines()).__next__
t = int(input())
output = []
for _ in range(t):
	n = int(input())
	for i in range(n):
		line = ['0'] * n
		line[i] = '1'
		line[i - 1] = '1'
		output.append(' '.join(line))
print(*output, sep='\n')


T = int(input())
for tt in range(1, T + 1):
	N = int(input())
	ans = [[0] * N for _ in range(N)]
	for i in range(0, N - 1, 2):
		ans[i][i] = ans[i][i + 1] = ans[i + 1][i] = ans[i + 1][i + 1] = 1
	if N % 2:
		ans[-1][0] = ans[-1][-1] = ans[0][-1] = 1
	for r in ans:
		print(*r)


def prime1(n):
	i = 2
	while i < n:
		if n % i == 0:
			return False
		else:
			i += 1
	if i == n:
		return True
for _ in range(int(input())):
	n = int(input())
	if prime1(n) == True:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		x11 = 1
		while 1:
			if prime1(n - 1 + x11) == True and prime1(x11) == False:
				break
			else:
				x11 += 1
		y11 = 4
		while 1:
			if prime1((n - 1) * x11 + y11) == True and prime1(y11) == False:
				break
			else:
				y11 += 1
		for i in range(n):
			for j in range(n):
				if i != n - 1 and j != n - 1:
					print(1, end=' ')
				elif i == n - 1 and j == n - 1:
					print(y11, end=' ')
				else:
					print(x11, end=' ')
			print()


import sys

def I():
	return int(sys.stdin.readline().rstrip())

def f(n):
	p = 0
	for i in range(n + 10, 10 ** 8):
		for j in range(2, int(i ** 0.5) + 1):
			if i % j == 0:
				break
		else:
			p = i
			break
	s = p - (n - 2)
	if s % 2 == 0:
		(q, r) = (4, s - 4)
	else:
		(q, r) = (1, s - 1)
	X = [0] * (n - 2) + [1] * 2
	Y = [1] * (n - 2) + [q, r]
	Z = [1] * (n - 2) + [r, q]
	A = []
	for _ in range(n - 2):
		A.append(X)
	A.append(Y)
	A.append(Z)
	return A
t = I()
for _ in range(t):
	n = I()
	A = f(n)
	for i in range(n):
		print(*A[i])


t = int(input())
for _ in range(t):
	n = int(input())
	s = ['1', '1'] + ['0'] * (n - 2)
	for _ in range(n):
		print(' '.join(s))
		s = s[-1:] + s[:-1]


for _ in range(int(input())):
	n = int(input())
	for i in range(n):
		row = [0] * n
		row[i] = 4
		row[(i + 1) % n] = 1
		print(*row)


tc = int(input())
for i in range(tc):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
	else:
		lst = []
		for i in range(n):
			a1 = []
			for j in range(n):
				a1.append(0)
			lst.append(a1)
		for ix in range(n):
			for jx in range(n):
				if ix == jx:
					lst[ix][jx] = 1
					lst[ix][n - ix - 1] = 1
		if n % 2 != 0:
			lst[n // 2][0] = 1
			lst[n // 2][n - 1] = 1
			lst[0][n // 2] = 1
			lst[n - 1][n // 2] = 1
		for x in lst:
			print(*x)


for it in range(int(input())):
	n = int(input())
	a = [[0] * n for i in range(n)]
	for i in range(n - 1):
		a[i][i] = 1
		a[i][i + 1] = 1
	a[n - 1][0] = 1
	a[n - 1][-1] = 1
	for i in a:
		print(*i)


t = int(input())
for i in range(t):
	n = int(input())
	ok = '11' + '0' * (n - 2)
	out = []
	for i in range(n):
		out.append(ok[i:] + ok[:i])
	for x in out:
		print(' '.join(x))


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = lambda : sys.stdin.buffer.readline().rstrip()
for _ in range(int(input())):
	q = int(input())
	w = [1, 1] + [0] * (q - 2)
	e = [[] for i in range(q)]
	for i in range(q):
		for j in range(i, i + q):
			r = j % q
			e[i].append(w[r])
	for i in e:
		print(*i)


def solve():
	n = int(input())
	a = [[0] * n for i in range(n)]
	for i in range(n):
		a[i][i] = 1
		a[i][n - i - 1] = 1
	if n % 2 == 1:
		m = n // 2
		a[m][m - 1] = 1
		a[m][m + 1] = 1
		a[m - 1][m] = 1
		a[m + 1][m] = 1
	return '\n'.join((' '.join(map(str, r)) for r in a))
t = int(input())
i = 0
while i < t:
	print(solve())
	i += 1


t = int(input())
p = [1] * 200
for i in range(2, 200):
	if p[i]:
		for j in range(i * 2, 200, i):
			p[j] = 0
p = {i for i in range(2, 200) if p[i]}
for _ in range(t):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
		continue
	use = (-1, -1)
	for pi in p:
		if pi % (n - 1) not in p and pi // (n - 1) not in p:
			use = (pi % (n - 1), pi // (n - 1))
			break
		if use[0] >= 0:
			break
	ans = [[use[1] if i != j else use[0] for i in range(n)] for j in range(n)]
	for ai in ans:
		print(*ai)


from math import sqrt, ceil

def is_prime(num):
	if num == 2:
		return True
	elif num % 2 == 0:
		return False
	for i in range(3, ceil(sqrt(num)) + 1, 2):
		if num % i == 0:
			return False
	return True

def fill_with_zeros(size, two_d_list):
	for sublist in two_d_list:
		for i in range(size):
			sublist.append('0')

def find_prime_square(size, two_d_list):
	for i in range(len(two_d_list)):
		two_d_list[i][i] = '1'
		two_d_list[i][size - i - 1] = '1'
	if size % 2 != 0:
		middle_y = size // 2
		middle_x = size // 2
		two_d_list[middle_y - 1][middle_x] = '1'
		two_d_list[middle_y][middle_x + 1] = '1'

def print_two_d_list(two_d_list):
	for sublist in two_d_list:
		print(' '.join(sublist))

def main():
	test_cases = int(input())
	for i in range(test_cases):
		size = int(input())
		two_d_list = []
		for j in range(size):
			two_d_list.append([])
		fill_with_zeros(size, two_d_list)
		find_prime_square(size, two_d_list)
		print_two_d_list(two_d_list)
main()


import math

def solve():
	size = int(input())
	for i in range(size):
		temp = []
		for j in range(size):
			if j == i or j == (i + 1) % size:
				temp.append(1)
			else:
				temp.append(0)
		print(' '.join(map(str, temp)))
	return None
for t in range(int(input())):
	solve()


t = int(input())
for _ in range(t):
	n = int(input())
	sb_first_last = [1, 1]
	sb_mid = [1, 1, 1]
	print(*sb_first_last + [0] * (n - 2))
	for i in range(1, n - 1):
		print(*(i - 1) * [0] + sb_mid + max(0, n - 2 - i) * [0])
	print(*[0] * (n - 2) + sb_first_last)


P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

def is_prime(n):
	if n in P:
		return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	if n in P:
		for _ in range(n):
			for _ in range(n):
				print(1, end=' ')
			print()
	else:
		index = 0
		for i in range(len(P)):
			if P[i] > n:
				index = i
				break
		diff = P[index] - n + 1
		while is_prime(diff):
			index += 1
			diff = P[index] - n + 1
		for i in range(n):
			for j in range(n):
				if i == j:
					print(diff, end=' ')
				else:
					print(1, end=' ')
			print()


p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(int(input())):
	n = int(input())
	if n in p:
		for i in range(n):
			for j in range(n):
				print('1', end=' ')
			print()
	else:
		x = []
		for i in range(n):
			y = []
			for j in range(n):
				if i == j:
					y.append(1)
				else:
					y.append(0)
			x.append(y)
		i = 0
		j = n - 1
		while i < n and j >= 0:
			x[i][j] = 1
			i += 1
			j -= 1
		if n % 2 != 0:
			x[0][n // 2] = 1
			x[n // 2][0] = 1
		for i in range(n):
			for j in range(n):
				print(x[i][j], end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	ml = [[0] * n for i in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i == j + 1 or j - i == n - 1:
				ml[i][j] += 1
	for i in range(n):
		print(*ml[i])


t = int(input())
for _ in range(t):
	n = int(input())
	l = [[0] * n for i in range(n)]
	if n == 2 or n == 3:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		(l[0][0], l[0][1], l[n - 1][n - 2], l[n - 1][n - 1]) = (1, 1, 1, 1)
		for i in range(1, n - 1):
			l[i][i - 1] = 1
			l[i][i + 1] = 1
		for i in range(n):
			for j in range(n):
				print(l[i][j], end=' ')
			print()


s = [1 for i in range(101)]
s[0] = 0
s[1] = 0
for i in range(2, 101):
	if s[i] == 1:
		for j in range(i * i, 101, i):
			s[j] = 0
p = []
for i in range(2, 101):
	if s[i] == 1:
		p.append(i)
for _ in range(int(input())):
	n = int(input())
	l = [[1 for i in range(n)] for j in range(n)]
	if s[n] == 1:
		for i in l:
			print(*i)
	else:
		l = [[0 for i in range(n)] for j in range(n)]
		if n % 2 == 0:
			for i in range(n):
				l[i][i] = 1
				l[i][-i - 1] = 1
			for i in l:
				print(*i)
		else:
			for i in range(n):
				if i == 0:
					l[0][n // 2] = 1
					l[0][0] = 1
					l[0][n - 1] = 1
				elif i == n // 2:
					l[i][0] = 1
					l[i][n - 1] = 1
					l[i][i] = 1
				elif i == n - 1:
					l[n - 1][n // 2] = 1
					l[n - 1][n - 1] = 1
					l[n - 1][0] = 1
				else:
					l[i][i] = 1
					l[i][-i - 1] = 1
			for i in l:
				print(*i)


nb = int(input())
tabs = []
for i in range(nb):
	tabs.append(int(input()))
for tab in tabs:
	caree = [0] * tab
	for i in range(tab):
		caree[i] = [0] * tab
	if tab % 2 == 0:
		for i in range(tab):
			caree[i][i] = 1
			caree[i][tab - 1 - i] = 1
	if tab % 2 != 0:
		caree[0][1] = 1
		caree[1][0] = 1
		caree[1][1] = 1
		caree[0][0] = 1
		caree[tab - 1][0] = 1
		caree[0][tab - 1] = 1
		for i in range(1, tab):
			caree[i][i] = 1
			caree[i][tab - i] = 1
	for care in caree:
		for i in range(tab):
			if i == 0:
				print(care[i], end='')
			else:
				print(' %d' % care[i], end='')
		print()


for _ in range(int(input())):
	n = int(input())
	for i in range(n - 1):
		for j in range(n):
			if j == i:
				print(4, end=' ')
			elif j == i + 1:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print('')
	print(1, end=' ')
	for h in range(n - 2):
		print(0, end=' ')
	print(4, end=' ')
	print('')


def solve(n):
	ans_arr = [[0 for _ in range(n)] for _ in range(n)]
	for i in range(0, n, 2):
		ans_arr[i][i] = 1
		ans_arr[i][i + 1] = 1
		ans_arr[i + 1][i] = 1
		ans_arr[i + 1][i + 1] = 1
		if n - i == 3:
			for j in range(3):
				ans_arr[i + 2][i + j] = 1
				ans_arr[i + j][i + 2] = 1
			break
	return ans_arr
for _ in range(int(input())):
	n = int(input())
	arr = solve(n)
	for row in arr:
		print(' '.join(list(map(str, row))))


import sys
input = sys.stdin.readline

def solve(n):
	ans = [[] * 1 for i in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				ans[i].append(1)
			else:
				ans[i].append(0)
	for i in range(n - 1):
		for j in range(n - 1):
			if i == j:
				ans[i][j + 1] = 1
				break
	ans[n - 1][0] = 1
	for a in ans:
		print(*a)
for _ in range(int(input())):
	n = int(input())
	solve(n)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()

class Sieve:

	def __init__(self, n):
		self.plist = [2]
		min_prime_factor = [2, 0] * (n // 2 + 5)
		for x in range(3, n + 1, 2):
			if min_prime_factor[x] == 0:
				min_prime_factor[x] = x
				self.plist.append(x)
				if x ** 2 > n:
					continue
				for y in range(x ** 2, n + 5, 2 * x):
					if min_prime_factor[y] == 0:
						min_prime_factor[y] = x
		self.min_prime_factor = min_prime_factor

	def isprime(self, x):
		return self.min_prime_factor[x] == x
sv = Sieve(10 ** 5)
for _ in range(II()):
	n = II()
	aa = [[1] * n for _ in range(n)]
	s = 0
	for a in sv.plist:
		if a - (n - 1) > 0 and (not sv.isprime(a - (n - 1))):
			for i in range(1, n):
				aa[i][0] = aa[0][i] = a - (n - 1)
			s = (a - (n - 1)) * (n - 1)
			break
	for a in sv.plist:
		if a - s > 0 and (not sv.isprime(a - s)):
			aa[0][0] = a - s
			break
	for row in aa:
		print(*row)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def prime(n):
	if n < 2:
		return False
	for i in range(2, n - 1):
		if n % i == 0:
			return False
	return True
t = inp()
for _ in range(t):
	n = inp()
	target = n
	while not prime(target) or prime(target - n + 1):
		target += 1
	boshbe = target - n + 1
	for i in range(n):
		for j in range(n):
			if i != j:
				print(1, end=' ')
			else:
				print(boshbe, end=' ')
		print()


def zip_sorted(a, b):
	(a, b) = zip(*sorted(zip(a, b)))
	sorted(zip(a, b), key=lambda x: x[1])
	return (a, b)
t = int(input())
for t1 in range(t):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			a = [0] * n
			for j in range(n):
				if i == j or j == n - i - 1 or i == n - j - 1:
					a[j] = 1
			print(*a)
	else:
		for i in range(n):
			a = [0] * n
			for j in range(n):
				if i == j or j == n - i - 1 or i == n - j - 1:
					a[j] = 1
			if i == int((n - 1) / 2):
				a[0] = 1
				a[-1] = 1
			if i == n - 1 or i == 0:
				a[int((n - 1) / 2)] = 1
			print(*a)


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 10 ** 9 + 7

def lcm(x, y):
	return x * y / gcd(x, y)

def fact(x, mod=mod):
	ans = 1
	for i in range(1, x + 1):
		ans = ans * i % mod
	return ans

def arr2D(n, m, default=0):
	return [[default for j in range(m)] for i in range(n)]

def arr3D(n, m, r, default=0):
	return [[[default for k in range(r)] for j in range(m)] for i in range(n)]

def sortDictV(x):
	return {k: v for (k, v) in sorted(x.items(), key=lambda item: item[1])}

class DSU:

	def __init__(self, length):
		self.length = length
		self.parent = [-1] * self.length

	def getParent(self, node, start):
		if node >= self.length:
			return False
		if self.parent[node] < 0:
			if start != node:
				self.parent[start] = node
			return node
		return self.getParent(self.parent[node], start)

	def union(self, node1, node2):
		parent1 = self.getParent(node1, node1)
		parent2 = self.getParent(node2, node2)
		if parent1 == parent2:
			return False
		elif self.parent[parent1] <= self.parent[parent2]:
			self.parent[parent1] += self.parent[parent2]
			self.parent[parent2] = parent1
		else:
			self.parent[parent2] += self.parent[parent1]
			self.parent[parent1] = parent2
		return True

	def getCount(self, node):
		return -self.parent[self.getParent(node, node)]

def SieveOfEratosthenes(n):
	ans = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			ans.append(p)
	return ans
from bisect import bisect_left
prime = SieveOfEratosthenes(10 ** 5)

def findNum(n):
	ind = bisect_left(prime, n)
	for i in range(ind, len(prime)):
		ind1 = bisect_left(prime, prime[i] - n + 1)
		if prime[ind1] == prime[i] - n + 1:
			continue
		else:
			return prime[i] - n + 1

def solve(n):
	val = findNum(n)
	lst = arr2D(n, n, default=1)
	for i in range(n):
		lst[i][i] = val
	for i in lst:
		for j in i:
			print(j, end=' ')
		print()
for _ in range(int(input())):
	n = int(input())
	solve(n)


n = int(input())
for i in range(n):
	m = int(input())
	v = [[0 for k in range(m)] for kk in range(m)]
	for j in range(m):
		v[j][j] = 9
		v[j][j - 1] = 4
	for i in range(m):
		print(*v[i])


t = int(input())
for _ in range(t):
	n = int(input())
	ans = 0
	flag = False
	D = {}
	sett = set()
	q = 2
	while not flag:
		if q not in D:
			D[q * q] = [q]
			sett.add(q)
			if q - n + 1 not in sett and q - n + 1 > 0:
				ans = q - n + 1
				flag = True
				break
		else:
			for p in D[q]:
				D.setdefault(p + q, []).append(p)
			del D[q]
		if flag == True:
			break
		q += 1
	for i in range(n):
		arr = [1] * n
		arr[i] = ans
		print(*arr)


def is_prime(n):
	if n == 1:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
for _ in range(int(input())):
	t = int(input())
	divisor = 1
	(sum_is_prime, num_is_prime) = (None, None)
	while True:
		(sum_is_prime, num_is_prime) = (False, False)
		num_is_prime = is_prime(divisor)
		if num_is_prime == False:
			sum_is_prime = is_prime(divisor + t - 1)
		if sum_is_prime == True and num_is_prime == False:
			break
		divisor += 1
	divisor = str(divisor)
	for ind in range(t):
		output_arr = ['1'] * t
		output_arr[ind] = divisor
		print(' '.join(output_arr))


for _ in range(int(input())):
	n = int(input())
	l = [0 for x in range(n)]
	l[0] = 1
	l[1] = 1
	for i in range(n):
		for j in l[i:]:
			print(j, end=' ')
		for j in l[:i]:
			print(j, end=' ')
		print()


test = int(input())

def printEven(n: int):
	for i in range(n):
		ans = []
		for j in range(n):
			if i == j or n - 1 - i == j:
				ans.append('1')
			else:
				ans.append('0')
		print(' '.join(ans))

def printOdd(n):
	for i in range(n):
		ans = []
		for j in range(n):
			if i == j or n - 1 - i == j or (i == n // 2 - 1 and j == n // 2) or (i == n // 2 and j == n // 2 - 1):
				ans.append('1')
			else:
				ans.append('0')
		print(' '.join(ans))
for tc in range(test):
	n = int(input())
	if n % 2 == 0:
		printEven(n)
	else:
		printOdd(n)


import collections
prime = [1]
seen = set([1])
for i in range(2, 500):
	if i in seen:
		continue
	for j in range(i + i, 500, i):
		seen.add(j)
	prime.append(i)
seen = sorted(seen, reverse=True)
for t in range(int(input())):
	n = int(input())
	pre = 1
	val = 1
	for i in prime:
		if i > 3 * n:
			val = i
			break
	diff = val - (n - 1)
	v = seen[:]
	v.pop()
	while diff in prime:
		pre = v.pop()
		diff = val - (n - 2) - pre
	for i in range(n - 1):
		ans = [1] * i + [diff, pre] + [1] * (n - i - 2)
		print(*ans)
	if pre != 1:
		ans = [pre] + [1] * (n - 2) + [diff]
		print(*ans)
	else:
		ans = [1] * (n - 1) + [diff]
		print(*ans)


def is_prime(n):
	if n == 2 or n == 3:
		return True
	if n == 1 or n % 2 == 0:
		return False
	d = 3
	while d * d <= n:
		if n % d == 0:
			return False
		d += 2
	return True

def solve(n):
	ans = [[1 for j in range(n)] for i in range(n)]
	x = 1
	while is_prime(x) or not is_prime(x + n - 1):
		x += 1
	for i in range(n):
		ans[i][i] = x
		print(*ans[i])
t = int(input())
for i in range(t):
	n = int(input())
	solve(n)


t = int(input())
answers = []
for _ in range(t):
	answer = []
	n = int(input())
	for j in range(n):
		answer.append(['0'] * n)
	for k in range(n):
		answer[k][k] = '1'
		answer[k][-(k + 1)] = '1'
	if n % 2 != 0:
		answer[0][int(n / 2)] = '1'
		answer[int(n / 2)][0] = '1'
		answer[int(n / 2)][n - 1] = '1'
		answer[n - 1][int(n / 2)] = '1'
	answers.append(answer)
for i in range(t):
	for l in range(len(answers[i])):
		print(' '.join(answers[i][l]))


from sys import stdin, stdout
t = int(stdin.readline())
for i in range(t):
	n = int(stdin.readline())
	arr = [[1 for i in range(n)] for _ in range(n)]
	if n % 2 != 0:
		for i in range(n):
			arr[i][i] = 113 - (n - 2) - 4
			arr[i][(i + 1) % n] = 4
	else:
		for i in range(n):
			arr[i][i] = 113 - (n - 1)
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print('')


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def sieve():
	prime = [1] * 200001
	prime[0] = prime[1] = 0
	i = 2
	while i * i <= 200000:
		for j in range(2 * i, 200001, i):
			prime[j] = 0
		i += 1
	return prime
primes = sieve()
for _ in range(int(data())):
	n = int(data())
	mat = [[1] * n for i in range(n)]
	temp = n - 1
	for i in range(1, 100000):
		if primes[i + temp] and (not primes[i]):
			temp = i
			break
	for i in range(n):
		mat[i][i] = temp
	for i in mat:
		outa(*i)


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)

def tup():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return input().strip()

def grid(r, c):
	return [lint() for i in range(r)]

def debug(*args, c=6):
	print('\x1b[3{}m'.format(c), *args, '\x1b[0m', file=stderr)
for _ in range(I()):
	n = I()
	mat = [[0 for i in range(n)] for j in range(n)]
	if n % 2 == 1:
		mat[n - 1][0] = 1
		mat[n - 1][n - 1] = 1
		for i in range(n - 1):
			for j in range(n - 1):
				if i == j:
					mat[i][j] = 1
					mat[i][j + 1] = 1
	else:
		for i in range(n):
			for j in range(n):
				if i == j:
					mat[i][j] = 1
				else:
					mat[i][j] = 0
		for i in range(n):
			for j in range(n):
				if i + j == n - 1:
					mat[i][j] = 1
				elif mat[i][j] == 1:
					continue
				else:
					mat[i][j] = 0
	for i in range(n):
		print(*mat[i])


a = input()
for x in range(int(a)):
	b = input()
	list1 = [[0 for i in range(int(b))] for i in range(int(b))]
	if int(b) % 2 == 0:
		i = 0
		j = int(b) - 1
		for x in range(int(b)):
			list1[x][i + x] = 1
			list1[x][j - x] = 1
	else:
		i = 0
		j = int(b) - 1
		for x in range(int(b)):
			if x == 0 or x == int(b) - 1 or x == int(b) // 2:
				list1[x][0] = 1
				list1[x][-1] = 1
				list1[x][int(b) // 2] = 1
			else:
				list1[x][i + x] = 1
				list1[x][j - x] = 1
	for i in range(int(b)):
		for j in range(int(b)):
			print(list1[i][j], end=' ')
		print()


def prime(n):
	if n < 2:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def solve():
	n = int(input())
	horisum = 0
	vertisum = 0
	a = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			pass
			if i == j:
				a[i][j] = 1
	for i in range(n):
		for j in range(n):
			if i + j == n - 1:
				a[i][j] = 1
	if n % 2 != 0:
		a[n // 2][n // 2] = 0
		a[n // 2][n // 2 - 1] = 1
		a[n // 2][n // 2 + 1] = 1
		a[n // 2 - 1][n // 2] = 1
		a[n // 2 + 1][n // 2] = 1
	for i in a:
		print(*i)
t = int(input())
for i in range(t):
	solve()


for i in range(int(input())):
	A = int(input())
	num = 0
	for i in range(A):
		Ans = []
		for j in range(A):
			if j == num or j == num + 1:
				Ans += [1]
			elif i == A - 1 and j == 0:
				Ans += [1]
			else:
				Ans += [0]
		num += 1
		print(*Ans)


from functools import reduce
import os
import sys
from collections import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
inf = 1e+18
div = 998244353
for _ in range(inn()):
	n = inn()
	ans = []
	for i in range(n - 1):
		l = [0] * n
		l[i] = 1
		l[i + 1] = 1
		ans += [l]
	l = [0] * n
	l[0] = 1
	l[-1] = 1
	ans += [l]
	for i in range(n):
		print(*ans[i])


a = int(input())
for i in range(a):
	b = int(input())
	c = [[0] * b for j in range(b)]
	for j in range(b):
		c[j][b - j - 1] = 1
		c[j][b - j - 2] = 1
	for j in c:
		print(*j)


from collections import Counter, defaultdict
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	n = I()
	if n == 2:
		print(1, 1)
		print(1, 1)
	else:
		a = [0] * n
		a[0] = 1
		a[1] = 6
		for i in range(n):
			print(*a)
			a[i] = 0
			a[(i + 1) % n] = 1
			a[(i + 2) % n] = 6


for t in range(int(input())):
	n = int(input())
	a = [1, 1]
	a.extend([0] * (n - 2))
	for i in range(n):
		for j in range(n):
			print(a[(i + j) % n], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	s = [[0 for _ in range(n)] for _ in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				s[i][j] = 1
			elif i == j - 1 and i != n - 1:
				s[i][j] = 1
	s[n - 1][0] = 1
	for i in range(n):
		for j in range(n):
			print(s[i][j], end=' ')
		print()


t = int(input())
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541]

def notPrime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return True
	return False

def solve():
	n = int(input())
	other = -1
	for i in primes:
		if i > n - 1:
			val = i - (n - 1)
			if notPrime(val):
				other = val
				break
	for i in range(n):
		lst = ['1' for i in range(n)]
		lst[i] = str(other)
		print(' '.join(lst))
for i in range(t):
	solve()


import sys
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	ans = ['1'] * 2 + ['0'] * (n - 2)
	for i in range(n - 2):
		print(' '.join(ans))
		ans[i] = '0'
		ans[i + 2] = '1'
	print(' '.join(ans))
	ans[-2] = '0'
	ans[0] = '1'
	print(' '.join(ans))


import sys
input = sys.stdin.readline
for f in range(int(input())):
	p = 283
	n = int(input())
	for i in range(n):
		out = [1] * n
		out[i] = p - n + 1
		if out[i] % 2 == 1:
			out[i] += 1
			out[i - 1] -= 1
		print(*out)


def is_prime(n):
	max_factor = int(n ** 0.5)
	for nn in range(n, 0, -1):
		is_prime = True
		for factor in range(2, max_factor + 1):
			if nn % factor == 0:
				is_prime = False
				break
		return is_prime
for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		n_blocks = int(n // 2)
		for i in range(n_blocks):
			row = [0] * 2 * i + [1] * 2 + [0] * 2 * (n_blocks - i - 1)
			print(*row)
			print(*row)
	elif is_prime(n):
		for i in range(n):
			row = [1] * n
			print(*row)
	elif is_prime(n - 2):
		n = n - 2
		row = [1] * 2 + [0] * n
		print(*row)
		print(*row)
		for i in range(n):
			row = [0] * 2 + [1] * n
			print(*row)
	else:
		n = n - 3
		row = [1] * 3 + [0] * n
		print(*row)
		print(*row)
		print(*row)
		for i in range(2, n):
			if is_prime(i) and is_prime(n - i):
				for ii in range(i):
					row = [0] * 3 + [1] * i + [0] * (n - i)
					print(*row)
				for ii in range(i, n):
					row = [0] * 3 + [0] * i + [1] * (n - i)
					print(*row)
				break


p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197]
for _ in range(int(input())):
	n = int(input())
	t = -1
	for j in p:
		if j - n + 1 >= 1 and j - n + 1 not in p:
			t = j
			break
	for i in range(n):
		s = ''
		for j in range(n):
			if i == j:
				s += str(t - n + 1) + ' '
			else:
				s += str(1) + ' '
		print(s)


prime = set((2, 3, 5))
for i in range(7, 1000):
	c = 0
	for j in range(2, int(i ** (1 / 2)) + 1):
		if i % j == 0:
			c += 1
			break
	if c == 0:
		prime.add(i)
for i in range(int(input())):
	n = int(input())
	ans = []
	if n in prime:
		for i in range(n):
			print(*[1 for i in range(n)])
	else:
		x = n - 1
		for e in prime:
			if e - x > 0:
				if e - x not in prime:
					ans = [[1 for i in range(n)] for i in range(n)]
					for i in range(n):
						ans[i][i] = e - x
						print(*ans[i])
					break


primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
from bisect import bisect_left

def solve(n):
	pos = bisect_left(primes, n)
	if pos == len(primes):
		p = primes[-1]
	elif primes[pos] == n:
		p = n
	else:
		p = primes[pos - 1]
	zeros = n - p
	ans = []
	row = [0] * zeros + [1] * p
	for i in range(n):
		rotated = row[i:] + row[:i]
		print(' '.join(map(str, rotated)))
for _ in range(int(input())):
	n = int(input())
	solve(n)


import math

def isPrime(n):
	if n == 1:
		return False
	if n == 2 or n == 3:
		return True
	if n % 2 == 0:
		return False
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		if n % i == 0:
			return False
	else:
		return True
t = int(input())
for x in range(t):
	n = int(input())
	i = 4
	while 1:
		ans = n - 1 + i
		if isPrime(ans) and (not isPrime(i)):
			break
		else:
			i = i + 1
	save = i
	ans = []
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if i == j:
				ans.append(save)
				continue
			else:
				ans.append(1)
		print(*ans)
		ans.clear()


for t in range(int(input())):
	n = int(input())
	li = list()
	for i in range(n):
		li.append([0] * n)
	if n == 2:
		li = [[1, 1], [1, 1]]
	else:
		for i in range(n):
			(li[i][i % n], li[i][(i + 1) % n], li[i][(i + 2) % n]) = (1, 1, 1)
	for i in range(n):
		for j in range(n):
			print(li[i][j], end=' ')
		print()


import math

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(math.sqrt(n) + 1), 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def nextPrime(N):
	if N <= 1:
		return 2
	prime = N
	found = False
	while not found:
		prime = prime + 1
		if isPrime(prime) == True:
			found = True
	return prime
t = int(input())
for _ in range(t):
	n = int(input())
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		ans[i][i] = 1
		ans[i][n - 1 - i] = 1
	if n % 2 == 1:
		ans[n // 2][0] = 1
		ans[0][n // 2] = 1
	for i in ans:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	mat = [[0 for j in range(n)] for i in range(n)]
	if n % 2 == 0:
		for i in range(n):
			mat[i][i] = 4
			mat[i][n - 1 - i] = 1
	else:
		for i in range(n // 2 - 1):
			mat[i][i] = 4
			mat[i][n - 1 - i] = 1
			mat[n - 1 - i][n - 1 - i] = 4
			mat[n - 1 - i][i] = 1
		x = n // 2
		(mat[x - 1][x - 1], mat[x - 1][x], mat[x - 1][x + 1]) = (1, 1, 1)
		(mat[x][x - 1], mat[x][x], mat[x][x + 1]) = (1, 1, 1)
		(mat[x + 1][x - 1], mat[x + 1][x], mat[x + 1][x + 1]) = (1, 1, 1)
	for i in mat:
		print(*i)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
from collections import Counter, defaultdict, deque

def int1():
	return int(input())

def map1():
	return map(int, input().split())

def list1():
	return list(map(int, input().split()))
mod = pow(10, 9) + 7

def solve():
	n = int1()
	l1 = [[0 for j in range(n)] for i in range(n)]
	for i in range(n):
		l1[i][i] = 1
		l1[i][(i + 1) % n] = 1
	for i in range(n):
		print(*l1[i])
for _ in range(int(input())):
	solve()


def chk(n):
	if n <= 1:
		return 0
	if n == 2:
		return 1
	for i in range(2, int(n ** (1 / 2)) + 1):
		if n % i == 0:
			return 0
	return 1
for _ in range(int(input())):
	n = int(input())
	x = n - 1
	for i in range(1, 100000):
		z = x + i
		if not chk(i) and chk(z):
			x = i
			break
	mat = [[1] * n for i in range(n)]
	for i in range(n):
		mat[i][i] = x
		print(*mat[i])


import os
import sys
from io import BytesIO, IOBase
import math
from itertools import permutations
from decimal import *
getcontext().prec = 25
MOD = pow(10, 9) + 7
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def sieve_of_eratosthenes(n):
	all_prime = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n):
		if prime[p]:
			all_prime.append(p)
	return all_prime
l = sieve_of_eratosthenes(200)
for i in range(int(input())):
	n = int(input())
	if n in l:
		for i in range(n):
			print('1 ' * n)
	else:
		i = 2
		t = n - 1
		while True:
			if i not in l and t + i in l:
				break
			i += 1
		for j in range(n):
			print('1 ' * j + str(i) + ' 1' * (n - 1 - j))


for _ in range(int(input())):
	n = int(input())
	grid = [[0] * n for _ in range(n)]
	for i in range(n):
		grid[i][i] = 1
		grid[i][-(i + 1)] = 1
	if n % 2 == 1:
		x = (n - 1) // 2
		grid[x][x + 1] = 1
		grid[x][x - 1] = 1
		grid[x - 1][x] = 1
		grid[x + 1][x] = 1
	for i in grid:
		print(*i)


cases = int(input())
for c in range(0, cases):
	size = int(input())
	matrix = [[0 for i in range(size)] for j in range(size)]
	if size % 2 != 0:
		matrix[size // 2][size // 2 + 1] = 1
		matrix[size // 2 + 1][size // 2] = 1
	for i in range(0, size):
		matrix[i][i] = 1
		matrix[i][size - 1 - i] = 1
		for j in range(0, size):
			print(matrix[i][j], end=' ')
		print('\n')


import sys

def solve(n):
	ans = []
	line = [4, 1] + [0 for _ in range(n - 2)]
	line2 = [1, 4] + [0 for _ in range(n - 2)]
	for i in range(0, n, 2):
		ans.append(line)
		ans.append(line2)
		line = [0, 0] + line[0:len(line) - 2]
		line2 = [0, 0] + line2[0:len(line2) - 2]
	if n % 2 == 1:
		ans.pop()
		ans[-2][-2] = 0
		ans[-2][-1] = 1
		ans[-1][-2] = 1
	return ans

def checkRep(n):
	for line in n:
		if sum(line) % 2 == 0 and sum(line) != 2:
			print('EERRROROR')

def prettyPrint(n):
	for i in range(len(n)):
		line = n[i]
		for ele in line:
			print(ele, end=' ')
		print()

def readinput():
	tc = int(sys.stdin.readline().rstrip())
	for _ in range(tc):
		n = int(sys.stdin.readline().rstrip())
		ans = solve(n)
		prettyPrint(ans)
readinput()


t = int(input())
for _ in range(t):
	n = int(input())
	prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 101, 103, 107, 109, 113, 137, 139, 149, 151]
	for i in prime:
		p = i - (n - 1)
		if p > 0 and p not in prime:
			x = p
			break
	j = 0
	for i in range(n):
		a = [1] * n
		a[j] = x
		for k in a:
			print(k, end=' ')
		print()
		j += 1


def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	result = set()
	for p in range(n + 1):
		if prime[p]:
			result.add(p)
	return result
primes = sieve(200000)
t = int(input())
for _ in range(t):
	n = int(input())
	c = 1
	while True:
		if c not in primes and (n - 1) * c + 1 in primes:
			break
		c += 1
	arr = [c] * n
	for i in range(n):
		arr[i] = 1
		print(' '.join(map(str, arr)))
		arr[i] = c


def printMatrix(matrix):
	for i in range(len(matrix)):
		print(' '.join(matrix[i]))

def tl_to_br(matrix, n):
	for i in range(n):
		matrix[i][i] = '1'
	return matrix

def tr_to_bl(matrix, n):
	for i in range(n):
		matrix[i][n - 1 - i] = '1'
	return matrix
t = int(input())
for _ in range(t):
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append(['0'] * n)
	matrix = tl_to_br(matrix, n)
	matrix = tr_to_bl(matrix, n)
	if n % 2 != 0:
		matrix[n // 2 - 1][n // 2] = '1'
		matrix[n // 2 + 1][n // 2] = '1'
		matrix[n // 2][n // 2 - 1] = '1'
		matrix[n // 2][n // 2 + 1] = '1'
	printMatrix(matrix)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
n = 10 ** 5 + 1
prime = []
vis = [0] * n
for i in range(2, n):
	if not vis[i]:
		vis[i] = 1
		prime.append(i)
	for j in prime:
		if i * j >= n:
			break
		vis[i * j] = 1
		if i % j == 0:
			break
sp = set(prime)

def p(arr):
	for row in arr:
		print(*row)

def solve(n):
	ans = [[1] * n for _ in range(n)]
	if n in sp:
		p(ans)
	else:
		j = bisect.bisect_left(prime, n)
		while j < len(prime) and prime[j] - (n - 1) in sp:
			j += 1
		d = prime[j] - (n - 1)
		for i in range(n):
			ans[i][i] = d
		p(ans)
t = int(input())
for _ in range(t):
	n = int(input())
	solve(n)


def solve(n):
	matrix = [[0] * n for _ in range(n)]
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i == j or i + j == n - 1:
					matrix[i][j] = 1
		return matrix
	else:
		for i in range(n):
			for j in range(n):
				if i == j or abs(i - j) == 1:
					matrix[i][j] = 1
		return matrix
t = int(input())
for test in range(t):
	n = int(input())
	matrix = solve(n)
	for row in matrix:
		print(*row)


for _ in range(int(input())):
	x = int(input())
	if x % 2 == 0:
		i = 2
		j = 0
		m = []
		while j <= (x - 2) // 2:
			l = [0] * abs(j)
			l.append(1)
			l = l + [0] * abs(x - i)
			l.append(1)
			l = l + [0] * abs(j)
			m.append(l)
			i = i + 2
			j = j + 1
		for k in m:
			print(*k, sep=' ')
		for k in m[::-1]:
			print(*k, sep=' ')
	else:
		i = 2
		j = 0
		m = []
		while j < (x - 1) // 2:
			l = [0] * abs(j)
			l.append(1)
			l = l + [0] * abs(x - i)
			l.append(1)
			l = l + [0] * abs(j)
			m.append(l)
			i = i + 2
			j = j + 1
		l = [0] * ((x - 1) // 2)
		l.append(1)
		l = l + [0] * ((x - 1) // 2)
		m.append(l)
		g = (x - 1) // 2
		m[g - 1][g] = 1
		m[g][g + 1] = 1
		for k in m:
			print(*k, sep=' ')
		m.pop(-1)
		for k in m[::-1]:
			print(*k, sep=' ')


import sys
import math

def get_line():
	return list(map(str, sys.stdin.readline()))

def in1():
	return int(input())
for _ in range(in1()):
	n = in1()
	a = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		a[i][i] = 1
	for i in range(n):
		a[i][n - 1 - i] = 1
	if n % 2 == 0:
		for i in range(n):
			print(*a[i])
	else:
		a[0][n // 2] = 1
		a[n // 2][0] = 1
		for i in range(n):
			print(*a[i])


import sys, math
from collections import defaultdict as dd
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	n = int(input())
	ans = [[0 for i in range(n)] for j in range(n)]
	c = 0
	for i in range(n):
		for j in range(2):
			if c == n:
				c = 0
			ans[i][c] = 1
			c += 1
		c -= 1
	for i in ans:
		print(*i)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for ii in range(int(input())):
	n = int(input())
	l = [1] * (n - 1)
	s = sum(l)
	l1 = []
	for i in range(1):
		j = 1
		while True:
			if isPrime(s + j) and isPrime(j) == False:
				l.append(j)
				break
			j += 1
	for i in range(n - 1):
		l1.append(l)
	s = 0
	l2 = []
	for i in range(n):
		s = 0
		for t in range(n - 1):
			s += l1[t][i]
		j = 1
		while True:
			if isPrime(s + j) and isPrime(j) == False:
				l2.append(j)
				break
			j += 1
	l1.append(l2)
	for i in range(n):
		for j in range(n):
			print(l1[i][j], end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	f = 0
	a = 0
	for i in range(2, n):
		if n % i == 0:
			f = 1
			a = i
			break
	if f == 0:
		for i in range(n):
			for i in range(n):
				print(1, end=' ')
			print()
	else:
		x = 0
		for i in range(n):
			l1 = []
			for k in range(i):
				l1.append(0)
			l1.append(4)
			x += 1
			for j in range(n - x):
				if j == 0:
					l1.append(1)
				else:
					l1.append(0)
			if i == n - 1:
				l1[0] = 1
			for i in range(n):
				print(l1[i], end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	l = [1, 1]
	l.extend([0 for _ in range(n - 2)])
	for _ in range(n):
		for i in l:
			print(i, end=' ')
		l.append(l[0])
		l.pop(0)
		print()


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from collections import Counter
from bisect import bisect_left
import sys
from queue import PriorityQueue
import operator as op
from functools import reduce
mod = 1000000007

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())

def printIn(ob):
	return stdout.write(str(ob) + '\n')

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def isPrime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def myCode():
	n = I()
	temp = 0
	for i in range(1, 101):
		if isPrime(1 + (n - 1) * i) and (not isPrime(i)):
			temp = i
			break
	print(1, end=' ')
	cnt = 1
	count = 1
	for i in range(n * n - 1):
		if cnt == n:
			print()
			cnt = 0
		if count == n + 1:
			print(1, end=' ')
			count = 0
		else:
			print(temp, end=' ')
		cnt += 1
		count += 1

def main():
	for t in range(I()):
		myCode()
main()


import sys, collections, math

def in_out():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def prime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def find(n):
	arr = []
	j = None
	for i in range(1, 10 ** 5 + 1):
		if not prime(i):
			arr.append(i)
		if len(arr) == n - 1:
			j = i + 1
			break
	while len(arr) != n:
		if not prime(j) and prime(sum(arr) + j):
			arr.append(j)
		j += 1
	return arr
t = int(input())
for i in range(t):
	n = int(input())
	ans = find(n)
	for i in range(n):
		for j in range(i, n + i):
			print(ans[j % n], end=' ')
		print()


def isprime(x):
	i = 2
	while i * i <= x:
		if x % i == 0:
			return False
		i = i + 1
	return True
for _ in range(int(input())):
	n = int(input())
	x = n
	while not isprime(x) or isprime(x - n + 1):
		x = x + 1
	for i in range(n):
		for j in range(n):
			if i == j:
				print(x - n + 1, end=' ')
			else:
				print(end='1 ')
		print()


import math
listPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

def isPrime(x):
	try:
		listPrimes.index(x)
	except ValueError:
		return False
	return True

def checkSquare(square):
	check = True
	n = len(square[0])
	for i in square:
		if not isPrime(sum(i)):
			check = False
	for x in range(n):
		localSum = 0
		for (i, v) in enumerate(square):
			localSum += v[x]
		if not isPrime(localSum):
			check = False
	return check

def returnSquare(n):
	square = []
	if n % 2 == 0:
		localList = []
		for i in range(n * n):
			localList.append(0)
			if (i + 1) % n == 0:
				square.append(localList)
				localList = []
		leftDiag = []
		rightDiag = []
		for i in range(n):
			leftDiag.append([i, i])
		for i in range(n):
			rightDiag.append([i, n - i])
		for (i, v) in enumerate(rightDiag):
			square[v[1] - 1][v[0]] = 1
			square[leftDiag[i][1] - 1][leftDiag[i][0] - 1] = 1
	elif isPrime(n):
		localList = []
		for i in range(n * n):
			localList.append(1)
			if (i + 1) % n == 0:
				square.append(localList)
				localList = []
	else:
		localList = []
		for i in range(n * n):
			localList.append(0)
			if (i + 1) % n == 0:
				square.append(localList)
				localList = []
		leftDiag = []
		lowDiag = []
		for i in range(n):
			leftDiag.append([i, i])
		for i in range(n):
			lowDiag.append([i, i + 1])
		for (i, v) in enumerate(lowDiag):
			square[v[1] - 1][v[0] - 1] = 1
			square[leftDiag[i][1] - 1][leftDiag[i][0] - 1] = 1
		square[0][1] = 1
		square[1][0] = 1
		square[n - 1][n - 2] = 1
		square[n - 2][n - 1] = 1
	return square
listN = []
for i in range(int(input())):
	listN.append(int(input()))
for i in listN:
	for v in returnSquare(i):
		sentence = ''
		for p in v:
			sentence += str(p) + ' '
		print(sentence)


def make_sq0(n):
	a = [[4 if i == j else 0 for j in range(n)] for i in range(n)]
	for i in range(n):
		a[i][n - 1 - i] = 1
		print(*a[i])

def make_sq1(n):
	a = [[4 if i == j else 0 for j in range(n)] for i in range(n)]
	for i in range(1, n):
		a[i][n - i] = 1
	(a[0][-1], a[-1][-1], a[-1][0]) = (1, 9, 1)
	for i in range(n):
		print(*a[i])
for t in range(int(input())):
	n = int(input())
	if n & 1:
		make_sq1(n)
	else:
		make_sq0(n)


def isprime(num):
	if num > 1:
		for i in range(2, num):
			if num % i == 0:
				return False
				break
		else:
			return True
	else:
		return False

def check(x):
	val = (x - 1) * 4
	v = 0
	while True:
		if isprime(val) and isprime(v) == False:
			return v
			break
		else:
			v = v + 1
			val = val + 1
t = int(input())
for i in range(t):
	n = int(input())
	k = check(n)
	ans = []
	lst = [4 for i in range(n - 1)]
	for i in range(n):
		temp = list(lst)
		temp.insert(i, k)
		ans.append(temp)
	for i in range(n):
		print(*ans[i])


t = int(input())
for _ in range(t):
	n = int(input())
	m = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j:
				m[i][j] = 1
			m[i][n - 1 - i] = 1
	if n % 2 != 0:
		i = n // 2
		m[n - 1][i] = 1
		m[i][n - 1] = 1
	for r in m:
		for c in r:
			print(c, end=' ')
		print()


import math
from collections import deque
from sys import stdin, stdout, setrecursionlimit
from string import ascii_letters
letters = ascii_letters[:26]
from collections import defaultdict
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	res = [[0 for g in range(n)] for i in range(n)]
	res[0][0] = 1
	res[0][1] = 1
	res[1][0] = 1
	for i in range(1, n - 1):
		res[i][i] = 1
		res[i + 1][i] = 1
	res[-1][-1] = 1
	res[-1][-2] = 1
	res[-2][-1] = 1
	for i in res:
		print(*i)


t = int(input())
while t:
	n = int(input())
	if n == 1:
		print(2)
	elif n == 2:
		print(1, 1)
		print(1, 1)
	else:
		a = [0 for i in range(2, n)]
		a = [1, 1] + a
		c = []
		c.append(a)
		for i in range(1, n):
			k = a[:]
			m = k[n - 1]
			del k[n - 1]
			k = [m] + k
			a = k[:]
			c.append(k)
		for i in range(n):
			for j in range(n):
				print(c[i][j], end=' ')
			print()
	t -= 1


def prime(n):
	if n <= 1:
		return False
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	if prime(n):
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	elif n % 2 != 0:
		k = 0
		for i in range(n):
			for j in range(n):
				if (j == n // 2 or i == n // 2) and (i == j or i == j - 1 or i == j + 1):
					print(1, end=' ')
				elif j == k or j == n - k - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print(' ')
			k += 1
	else:
		k = 0
		for i in range(n):
			for j in range(n):
				if j == k or j == n - k - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print(' ')
			k += 1


def solve():
	n = int(input())
	(row, col) = (n, n)
	res = ''
	for i in range(row):
		for j in range(col):
			if i == j:
				res += '1 '
			elif i == j - 1:
				res += '1 '
			elif i == j + 1:
				res += '1 '
			else:
				res += '0 '
		if i != n - 1:
			res += '\n'
	print(res)
for _ in range(int(input())):
	solve()


t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	l = [[0 for i in range(n)] for j in range(n)]
	i = 0
	j = 0
	while i < n:
		if i == n - 1:
			l[i][0] = 1
			l[i][i] = 1
		else:
			l[i][i] = 1
			l[i][i + 1] = 1
		i = i + 1
	for p in range(n):
		for q in range(n):
			print(int(l[p][q]), end=' ')
		print()


from math import *

def check(add):
	x = None
	for i in range(2, 10):
		if i == add:
			continue
		elif add % i == 0:
			x = 0
			break
		else:
			x = 1
	return x
for _ in range(int(input())):
	n = int(input())
	ns = n - 1
	add = 0
	if n <= 3:
		for i in range(n):
			for k in range(n):
				print(1, end=' ')
			print()
	else:
		while ns % 2 == 0 or ns % 3 == 0 or ns % 4 == 0 or (ns % 5 == 0) or (ns % 6 == 0) or (ns % 7 == 0) or (ns % 8 == 0) or (ns % 9 == 0) or (ns % 11 == 0) or (check(add) == 1):
			add += 1
			ns += 1
		for i in range(n):
			for k in range(n):
				if k == n - i - 1:
					print(add, end=' ')
				else:
					print(1, end=' ')
			print()


t = int(input())
s = set()
for i in range(2, 300):
	isprime = True
	for j in range(2, int(i ** 0.5) + 1):
		if i % j == 0:
			isprime = False
	if isprime:
		s.add(i)
for i in range(t):
	n = int(input())
	num = n - 1
	j = num
	while True:
		if j in s and j - num not in s:
			break
		j += 1
	sq = [[1 for x in range(n)] for m in range(n)]
	for k in range(n):
		sq[k][k] = j - num
		print(*sq[k])


t = int(input())
while t:
	mat = []
	n = int(input())
	for i in range(n - 1):
		a = [0] * n
		for j in range(i, i + 2):
			a[j] = 1
		mat.append(a)
	a = [0] * n
	a[0] = a[-1] = 1
	mat.append(a)
	for i in mat:
		for j in i:
			print(j, end=' ')
		print()
	t -= 1


import sys
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right
import math
for _ in range(int(input())):
	n = int(input())
	l = [[0 for j in range(n)] for i in range(n)]
	c1 = 0
	c2 = 0
	for i in range(n):
		l[i][i] = 1
		l[i][(i + 1) % n] = 4
	for i in range(n):
		print(*l[i])


for _ in range(int(input())):
	n = int(input())
	v = 0
	mat = [[0 for i in range(n)] for i in range(n)]
	d = 1
	for i in range(n):
		for j in range(n):
			if i == j:
				mat[i][j] = d
			elif i == (j + 1) % n:
				mat[i][j] = 1
			print(mat[i][j], end=' ')
		print('')


def prime(n):
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True
for _ in range(int(input())):
	n = int(input())
	one = [[1] * n] * n
	if prime(n):
		for i in range(n):
			print(*one[i])
	else:
		d = []
		for i in range(n):
			t = []
			for j in range(n):
				t.append(0)
			d.append(t)
		for i in range(n):
			for j in range(n):
				if i == j:
					d[i][j] = 1
				if i + j == n - 1:
					d[i][j] = 1
		if n % 2 != 0:
			d[n // 2][0] = 1
			d[0][n // 2] = 1
		for i in range(n):
			print(*d[i])


t = int(input())
for caseNr in range(1, t + 1):
	n = int(input())
	matrix = []
	for i in range(n - 1):
		line = [0] * n
		line[i] = 1
		line[i + 1] = 1
		matrix.append(line)
	matrix.append([1] + [0] * (n - 2) + [1])
	for row in matrix:
		print(*row)


am = int(input())
ms = list()
for i in range(am):
	m = int(input())
	ms.append(m)
for i in range(am):
	curm = ms[i]
	for j in range(curm):
		for k in range(curm):
			if (k - j) % curm == 0 or (k - j) % curm == 1:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print()


import sys, io, os
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(io.IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
T = int(input())
for _ in range(T):
	N = int(input())
	for i in range(N):
		A = [0] * N
		A[i] = 1
		A[(i + 1) % N] = 1
		print(*A)


for _ in range(0, int(input())):
	n = int(input())
	a = []
	for i in range(0, n):
		a.append([])
		for j in range(0, n):
			if j == i:
				a[i].append(1)
			else:
				a[i].append(0)
	for i in range(0, n):
		for j in range(0, n):
			if j == n - 1 - i:
				a[i][j] = 1
	if n % 2 == 1:
		a[int(n / 2) + 1][int(n / 2)] = 1
		a[int(n / 2)][int(n / 2) + 1] = 1
	for i in range(0, n):
		for j in range(0, n - 1):
			print(a[i][j], end=' ')
		print(a[i][n - 1])


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()

def check(n):
	if n == 2 or n == 3:
		return 1
	if n == 1:
		return 0
	i = 2
	while i * i <= n:
		if n % i == 0:
			return 0
		i += 1
	return 1
for _ in range(int(I1())):
	n = int(I1())
	a = []
	for i in range(n):
		l = []
		for j in range(n):
			l.append(1)
		a.append(l)
	if check(n) == 0:
		i = n + 1
		while True:
			if check(i) and check(i - n + 1) == 0:
				break
			i += 1
		c = i - n + 1
		for i in range(n):
			a[i][i] = c
	for x in a:
		print(*x)


t = int(input())
while t > 0:
	n = int(input())
	for i in range(n):
		for j in range(n):
			if i == j:
				print(' 1', end='')
			elif i + 1 == j:
				print(' 1', end='')
			elif i == n - 1 and j == 0:
				print(' 1', end='')
			else:
				print(' 0', end='')
		print('\r')
	print()
	t = t - 1


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
t = inp()
for _ in range(t):
	n = inp()
	a = [1, 1]
	for i in range(2, n):
		a.append(0)
	for i in range(n, 2 * n):
		a.append(a[i - n])
	for i in range(n):
		for j in range(i, i + n):
			print(a[j], end=' ')
		print()


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))

def eratosthenes(lim):
	is_p = [1] * lim
	p_list = []
	is_p[0] = 0
	is_p[1] = 0
	for i in range(2, lim):
		if is_p[i]:
			p_list.append(i)
			for j in range(i * i, lim, i):
				is_p[j] = 0
	return (is_p, p_list)
lim = 1000
(is_p, p_list) = eratosthenes(lim)
t = int(input())
for tt in range(t):
	n = int(input())
	num = 1
	while True:
		if is_p[num] != 1 and is_p[n - 1 + num] == 1:
			break
		else:
			num += 1
	for i in range(n):
		l = [1] * i + [num] + [1] * (n - i - 1)
		print(*l)


for i in range(int(input())):
	a = int(input())
	r = [[0 for j in range(a)] for j in range(a)]
	for z in range(a):
		r[z][z] = 1
		r[z][a - z - 1] = 1
	if a & 1:
		p = a // 2
		r[p][p - 1] = 1
		r[p][p + 1] = 1
		r[p - 1][p] = 1
		r[p + 1][p] = 1
	for z in range(a):
		for g in range(a):
			print(r[z][g], end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	out = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		out[i][i] = 1
	for i in range(n):
		out[i][(i + 1) % n] = 1
	for i in range(n):
		for j in range(n):
			print(out[i][j], end=' ')
		print('')


for test in range(int(input())):
	n = int(input())
	res = [['0' for x in range(n)] for x in range(n)]
	for i in range(n):
		for j in range(n):
			if i + 1 == j or i - 1 == j:
				res[i][j] = '1'
	res[0][0] = '1'
	res[n - 1][n - 1] = '1'
	for k in range(n):
		print(' '.join(res[k]))


import math
tc = int(input())

def check_prime(n):
	sqrt = math.sqrt(n)
	if n == 1 or n == 0:
		return False
	if n == 2:
		return True
	if sqrt == int(sqrt):
		return False
	else:
		sqrt = int(sqrt) + 1
	for m in range(2, sqrt):
		if n % m == 0:
			return False
	return True

def find_prime(n):
	next_prime = n
	while True:
		if check_prime(next_prime) and (not check_prime(next_prime - (n - 1))):
			return next_prime
		next_prime += 1

def print_matrix(seq):
	res = []
	size = len(seq)
	tmp = size - 1
	for i in range(size):
		for j in range(size):
			print(seq[tmp - j], end='')
			if j != size - 1:
				print(' ', end='')
			else:
				print()
		tmp -= 1

def solve(size):
	seq = []
	prime = find_prime(size)
	seq = [1] * size
	seq[-1] += prime - size
	print_matrix(seq)
for t in range(tc):
	size = int(input())
	solve(size)


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

	def In():
		return map(int, stdin.readline().split())

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main(p):
	try:
		n = I()
		if n == 1:
			print(1)
			return
		elif n == 2:
			print(1, 1)
			print(1, 1)
			return
		for i in range(n, 1000):
			if p[i - n + 1] == False and p[i] == True:
				break
		ans = i
		dp = [[1 for x in range(n)] for y in range(n)]
		for i in range(n):
			dp[i][i] = ans - (n - 1)
		for x in dp:
			print(*x)
	except:
		pass
M = 998244353
P = 1000000007
p = SieveOfEratosthenes(1000)
for _ in range(I()):
	main(p)


t = int(input().strip())
for a0 in range(t):
	n = int(input())
	l = []
	for i in range(1, n + 1):
		l1 = []
		for j in range(1, n + 1):
			if i + j == n + 1 or i == j:
				l1.append(1)
			else:
				l1.append(0)
		l.append(l1)
	if n % 2 != 0:
		a = (n - 1) // 2
		l[a - 1][a] = 1
		l[a][a - 1] = 1
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()


ans = []
pr = []
u = [0] * 100000
for i in range(2, 100000):
	if u[i] == 1:
		continue
	u[i] = 0
	pr.append(i)
	for j in range(i * 2, 100000, i):
		u[j] = 1
ind = pr.index(101)
for _ in range(int(input())):
	n = int(input())
	dd = 101
	i = ind
	while dd - n + 1 in pr:
		dd = pr[i + 1]
		i += 1
	s = '1 ' * (n - 1) + str(dd - n + 1)
	for i in range(n - 1):
		ans.append(s)
	cnt = (dd - n + 1) * (n - 1)
	i = ind
	while True:
		if pr[i] > cnt and pr[i] - cnt not in pr:
			cc = pr[i] - cnt
			break
		i += 1
	s = (str(dd - n + 1) + ' ') * (n - 1) + str(cc)
	ans.append(s)
print(*ans, sep='\n')


t = int(input())
for _ in range(t):
	n = int(input())
	x = 1
	while True:
		j = 2
		k = 2
		chk = 0
		if x == 1:
			chk = 1
		while k * k <= x:
			if x % k == 0:
				chk = 1
				break
			else:
				k += 1
		if chk == 1:
			chk1 = 0
			while j * j <= n - 1 + x:
				if (n - 1 + x) % j == 0:
					x += 1
					chk1 = 1
					break
				else:
					j += 1
			if chk1 == 0:
				break
		else:
			x += 1
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			if j == i:
				print(x, end=' ')
			else:
				print(1, end=' ')
		print()


p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
t = int(input())
while t > 0:
	n = int(input())
	l = [0] * n
	for i in range(0, n):
		ans = list(l)
		ans[i] = 1
		k = (i + 1) % n
		ans[k] = 1
		ans = [str(x) for x in ans]
		print(' '.join(ans))
	t -= 1


primes = [2]
for i in range(3, 202):
	for p in primes:
		if i % p == 0:
			break
	else:
		primes.append(i)
t = int(input())
for _ in range(t):
	n = int(input())
	for p in primes:
		if p > n:
			pri = p - n + 1
			if pri in primes:
				continue
			else:
				break
	col = [1 for i in range(n - 1)] + [p - n + 1]
	for i in range(n):
		print(' '.join(map(str, col[i:] + col[:i])))


t = int(input())
for _ in range(t):
	n = int(input())
	table = [[0 for j in range(n)] for i in range(n)]
	x = 0
	for y in range(n):
		table[y][x] = 1
		if x + 1 < n:
			table[y][x + 1] = 1
		if y % 2 == 1:
			x += 2
	if n % 2 == 1:
		table[-1][-2] = 1
		table[-2][-1] = 1
	for y in range(n):
		print(*table[y])


import sys
tests = int(sys.stdin.readline())
for _ in range(tests):
	n = int(sys.stdin.readline())
	line = '1 1' + (n - 2) * ' 0'
	for i in range(n):
		print(line)
		line = line[-1] + ' ' + line[:-2]


for tets in range(int(input())):
	n = int(input())
	for i in range(n - 1):
		t = [0] * i + [1, 1] + [0] * (n - 2 - i)
		print(*t)
	u = [1] + [0] * (n - 2) + [1]
	print(*u)


def si():
	return str(input())

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
t = int(input())
for _ in range(t):
	n = ii()
	mat = [[0] * n for i in range(n)]
	for i in range(0, n, 2):
		if i + 1 < n:
			mat[i][i] = 1
			mat[i][i + 1] = 1
			mat[i + 1][i] = 1
			mat[i + 1][i + 1] = 1
		else:
			mat[i][i] = 1
			mat[i][i - 1] = 1
			mat[i - 1][i] = 1
	for li in mat:
		print(*li)


t = int(input())
for _ in range(t):
	n = int(input())
	s0 = ['0'] * n
	for i in range(n - 1):
		s = s0[:]
		s[i] = '1'
		s[i + 1] = '1'
		print(' '.join(s))
	s0[-1] = '1'
	s0[0] = '1'
	print(' '.join(s0))


from collections import *
from functools import *
import math
import sys
inp = sys.stdin.readline
out = sys.stdout.write

def mi():
	return map(int, inp().split())

def li():
	return list(mi())

def ii():
	return int(inp())

def main():
	for _ in range(ii()):
		n = ii()
		b = [0 for _ in range(n)]
		if n % 2 == 0:
			j = 0
			for i in range(n):
				b[j] = 1
				b[j + 1] = 1
				print(*b)
				b[j] = 0
				b[j + 1] = 0
				if i % 2 == 1:
					j += 2
		else:
			j = 0
			for i in range(n - 1):
				b[j] = 1
				b[j + 1] = 1
				if i == 0:
					b[-1] = 1
				print(*b)
				b[j] = 0
				b[j + 1] = 0
				if i == 0:
					b[-1] = 0
				if i % 2 == 1:
					j += 2
			b[-1] = 1
			b[-2] = 1
			print(*b)
main()


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i + j == n - 1:
					print(1, end=' ')
					continue
				if i == j:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				if i + j == n - 1:
					print(1, end=' ')
					continue
				if i == n // 2 and j == 0:
					print(1, end=' ')
					continue
				if j == n // 2 and i == 0:
					print(1, end=' ')
					continue
				if i == n - 1 and j == n // 2:
					print(1, end=' ')
					continue
				if j == n - 1 and i == n // 2:
					print(1, end=' ')
					continue
				if i == j:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()


from sys import stdin
for i in range(int(stdin.readline())):
	n = int(stdin.readline())
	for i in range(n):
		for j in range(n):
			if i == j or i + 1 == j:
				print(1, end=' ')
			elif j == 0 and i == n - 1:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print('')


def find(n):
	if n in prime:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
		return
	for num in not_prime:
		if num + (n - 1) in prime:
			addnum = num
			break
	for i in range(n):
		for j in range(n):
			if i == j:
				print(addnum, end=' ')
			else:
				print(1, end=' ')
		print()
t = int(input())
prime = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199])
not_prime = [i for i in range(1, 100) if i not in prime]
for itr_ in range(t):
	n = int(input())
	find(n)


a = int(input())
for i in range(a):
	b = int(input())
	c = [4] * (b - 2) + [400 - 4 * (b - 2), 1] + [4] * (b - 2) + [400 - 4 * (b - 2), 1]
	for k in range(b):
		for j in range(k, k + b - 1):
			print(c[j], end=' ')
		print(c[k + b - 1])


for i in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		arr.append([0] * n)
	if n % 2 == 0:
		for i in range(0, n, 2):
			arr[i][i] = 1
			arr[i][i + 1] = 4
			arr[i + 1][i] = 4
			arr[i + 1][i + 1] = 1
	else:
		for i in range(n - 1):
			arr[i][i] = 1
			arr[i][i + 1] = 4
			arr[n - 1][0] = 4
			arr[n - 1][n - 1] = 1
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print('')


for _ in range(int(input())):
	n = int(input())
	sq = []
	for i in range(n):
		temp = ['0' for j in range(n)]
		sq.append(temp)
	for i in range(n - 1):
		sq[i][i] = '1'
		sq[i][i + 1] = '1'
	sq[n - 1][0] = '1'
	sq[n - 1][n - 1] = '1'
	for row in sq:
		print(' '.join(row))


def checkifprint(x):
	for i in range(2, int(x ** (1 / 2))):
		if x % i == 0:
			return False
	return True
l = []
for _ in range(int(input())):
	n = int(input())
	x = 4
	while checkifprint(99991 - (n - 1) * x):
		x = 2 + x
	for i in range(n):
		s = []
		for j in range(n):
			if i == j:
				s.append(str(99991 - (n - 1) * x))
			else:
				s.append(str(x))
		l.append(' '.join(s))
for i in l:
	print(i)


for t in range(int(input())):
	n = int(input())
	s = '6' + '0' * (n - 2) + '1'
	a = list(map(int, s))
	print(*a)
	for i in range(1, n):
		s = '0' * (i - 1) + '1' + '6' + '0' * (n - i - 1)
		a = list(map(int, s))
		print(*a)


for _ in range(int(input())):
	n = int(input())
	A = [4] * (n - 2) + [1] + [461 - 1 - 4 * (n - 2)]
	for i in range(-n, 0):
		print(*(A[j] for j in range(i, i + n)))


for _ in range(int(input())):
	n = int(input())
	a = [0] * (n - 2) + [1, 4]
	mat = []
	for i in range(n):
		mat.append(a)
		a = a[1:] + a[:1]
	for i in mat:
		print(*i)


testcases = int(input())
for i in range(testcases):
	n = int(input())
	mat = [[0] * n for i in range(n)]
	k = 0
	j = n - 1
	for i in range(n):
		mat[i][k] = 1
		mat[i][j] = 1
		k += 1
		j -= 1
	if n % 2 != 0:
		mat[n // 2][n // 2 + 1] = 1
		mat[n // 2 - 1][n // 2] = 1
	for i in mat:
		print(*i)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
l = [i for i in range(5, 600) if isPrime(i)]
for _ in range(int(input())):
	n = int(input())
	tol = l[n - 1]
	k = []
	for zz in range(n):
		if zz != n - 1:
			k.append(1)
			tol -= 1
		else:
			while isPrime(tol):
				tol -= 5
				k[n - 2] += 5
			k.append(tol)
	for i in range(n):
		for j in range(n):
			print(k[(n - i + j) % n], end=' ')
		print()


t = int(input())
for i in range(t):
	n = int(input())
	p = 0
	for i in range(n):
		s = [0] * n
		if i == n - 1:
			(s[0], s[i]) = (1, 1)
		else:
			(s[p], s[p + 1]) = (1, 1)
		print(*s)
		p += 1


for i in range(int(input())):
	n = int(input())
	lst = [[0 for k in range(n)] for j in range(n)]
	(st, ed) = (0, n - 1)
	for j in range(n):
		lst[j][st] = 1
		lst[j][ed] = 1
		st += 1
		ed -= 1
	if n % 2 != 0:
		lst[n // 2][n // 2 + 1] = 1
		lst[n // 2 + 1][n // 2] = 1
	for j in lst:
		print(*j)


from sys import stdin
import math
t = int(stdin.readline().strip())

def is_prime(n):
	if n == 2:
		return True
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def next_prime(n):
	if is_prime(n):
		return n
	while not is_prime(n):
		if n % 2 == 0:
			n += 1
		else:
			n += 2
	return n

def print_m(m, n):
	for i in range(n):
		for j in range(n):
			print(m[i][j], end=' ')
		print()
for _ in range(t):
	n = int(stdin.readline().strip())
	prime = next_prime(n)
	m = []
	if n == prime:
		m = [[1] * n for _ in range(n)]
	else:
		for i in range(n):
			m.append([])
			for j in range(n):
				if i == j:
					m[i].append(prime - 1)
				elif i + 1 == j or (i == n - 1 and j == 0):
					m[i].append(1)
				else:
					m[i].append(0)
	print_m(m, n)


t = int(input())
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
set_prime = set(prime)
for _ in range(t):
	n = int(input())
	v = 1
	for p in prime:
		if p >= n and p - n + 1 not in set_prime:
			v = p - n + 1
			break
	val = p - n + 1
	for i in range(n):
		for j in range(n):
			if i == j:
				print(str(v), end=' ')
			else:
				print('1', end=' ')
		print('\n')


simple = [727, 719, 709, 701, 691, 683, 677, 673, 661, 659, 653, 647, 643, 641, 631, 619, 617, 613, 607, 601, 599, 593, 587, 577, 571, 569, 563, 557, 547, 541, 523, 521, 509, 503, 499, 491, 487, 479, 467, 463, 461, 457, 449, 443, 439, 433, 431, 421, 419, 409, 401, 397, 389, 383, 379, 373, 367, 359, 353, 349, 347, 337, 331, 317, 313, 311, 307, 293, 283, 281, 277, 271, 269, 263, 257, 251, 241, 239, 233, 229, 227, 223, 211, 199, 197, 193, 191, 181, 179, 173, 167, 163, 157, 151, 149, 139, 137, 131, 127, 113, 109, 107, 103, 101, 97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2]
for i in range(int(input())):
	n = int(input())
	lst = [[0 for k in range(n)] for j in range(n)]
	for (ind, k) in enumerate(range(n)):
		if k == n - 1:
			for j in range(n):
				lst[k][j] = 4
		else:
			lst[k][ind] = 1
			lst[k][n - 1] = 4
	for j in simple:
		if j - 4 * (n - 1) not in simple:
			lst[n - 1][n - 1] = j - 4 * (n - 1)
			break
	for j in lst:
		print(*j)


n = 3000
primes = set(range(2, n + 1))
for i in range(2, int(n ** 0.5 + 1)):
	if i not in primes:
		i += 1
	else:
		remove = range(i * 2, n + 1, i)
		primes.difference_update(remove)
prime = list(primes)
q = int(input())
tmp = 0
for _ in range(q):
	n = int(input())
	for p in prime:
		x = (p - 1) % (n - 1)
		y = (p - 1) // (n - 1)
		if x == 0 and y not in primes:
			tmp = y
			break
	ans = [[tmp] * n for _ in range(n)]
	for i in range(n):
		ans[i][i] = 1
	for an in ans:
		print(' '.join(map(str, an)))


for i in range(int(input())):
	n = int(input())
	arr = [[0] * n for k in range(n)]
	for j in range(n):
		for k in range(n):
			if j == k or n - 1 - k == j:
				arr[j][k] = 1
				arr[j][n - 1 - k] = 1
			else:
				arr[j][k] = 0
	if n % 2 == 0:
		pass
	else:
		arr[n // 2][n // 2 + 1] = 1
		arr[n // 2 - 1][n // 2] = 1
	for j in arr:
		print(*j)


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
import threading
import sys
import time

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	dp = [[0 for _ in range(n)] for _ in range(n)]
	s = 0
	for i in range(n):
		dp[i][s % n] = 1
		dp[i][(s + 1) % n] = 1
		s = (s + 1) % n
	for row in dp:
		print(*row)

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


from collections import defaultdict
import sys
import math
import random
sys.setrecursionlimit(10 ** 6)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
primes = [2]
for i in range(3, 300):
	flag = False
	for j in range(2, int(math.sqrt(i)) + 1):
		if i % j == 0:
			flag = True
			break
	if not flag:
		primes.append(i)
for _ in range(int(input())):
	n = int(input())
	mat = [[1 for _ in range(n)] for _ in range(n)]
	k = -1
	for j in range(2, len(primes)):
		if primes[j] - n + 1 not in primes and primes[j] - n + 1 > 0:
			k = primes[j]
			break
	for i in range(n):
		mat[i][i] = k - n + 1
	for i in range(n):
		print(*mat[i])


for _ in range(int(input())):
	n = int(input())
	temp = 431
	if n == 1:
		print(3)
	elif n == 2:
		print(1, 1)
		print(1, 1)
	else:
		l = []
		l.append(1)
		for i in range(n - 2):
			l.append(4)
		l.append(430 - 4 * (n - 2))
		for i in range(n):
			l.append(l[i])
		for i in range(n):
			for j in range(i, i + n):
				print(l[j], end=' ')
			print()


t = int(input())
for i in range(t):
	n = int(input())
	a = [[0 for j in range(n)] for i in range(n)]
	curr = 0
	last = 1
	for i in range(n):
		a[i][curr % n] = 4
		a[i][last % n] = 1
		curr += 1
		last += 1
	for j in a:
		print(*j)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))
two_by_two = [[4, 1], [1, 4]]
three_by_three = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

def printSquare(square):
	for i in range(len(square)):
		line = ' '.join((str(j) for j in square[i]))
		print(line)
T = inp()
for _ in range(T):
	n = inp()
	if n == 2:
		printSquare(two_by_two)
	elif n == 3:
		printSquare(three_by_three)
	else:
		square = [[0 for col in range(n)] for row in range(n)]
		if n % 2 == 0:
			for i in range(0, n, 2):
				square[i][i] = two_by_two[0][0]
				square[i][i + 1] = two_by_two[0][1]
				square[i + 1][i] = two_by_two[1][0]
				square[i + 1][i + 1] = two_by_two[1][1]
		else:
			for i in range(3):
				for j in range(3):
					square[i][j] = three_by_three[i][j]
			for i in range(3, n, 2):
				square[i][i] = two_by_two[0][0]
				square[i][i + 1] = two_by_two[0][1]
				square[i + 1][i] = two_by_two[1][0]
				square[i + 1][i + 1] = two_by_two[1][1]
		printSquare(square)


def primenumber(x):
	if x == 1 or x == 2 or x == 3 or (x == 5):
		return True
	for j in range(2, x // 2 + 1):
		if x % j == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	for i in range(n):
		for j in range(n):
			if i != j:
				print(1, end=' ')
			else:
				res = n - 1
				if primenumber(n):
					print(1, end=' ')
				else:
					for j in range(2, 201):
						if primenumber(n - 1 + j) and (not primenumber(j)):
							print(j, end=' ')
							break
		print()


def IsPrime(n):
	if n == 1:
		return 1
	d = 2
	while n % d != 0:
		d += 1
	return d == n

def getNextPrime(n):
	while not IsPrime(n):
		n += 1
	return n
N = int(input())
for i in range(N):
	n = int(input())
	res = [[0 for j in range(n)] for i in range(n)]
	prime = getNextPrime(n - 1)
	while prime - n + 1 == 0 or IsPrime(prime - n + 1):
		prime = getNextPrime(prime + 1)
	border = prime - n + 1
	prime = getNextPrime((n - 1) * border)
	while prime - (n - 1) * border == 0 or IsPrime(prime - (n - 1) * border):
		prime = getNextPrime(prime + 1)
	corner = prime - (n - 1) * border
	for k in range(n - 1):
		res[n - 1][k] = border
		res[k][n - 1] = border
		for j in range(n - 1):
			res[k][j] = 1
	res[-1][-1] = corner
	for k in range(n):
		print(' '.join(map(str, res[k])))


def prime(n):
	i = 2
	while i < n:
		if n % i == 0:
			return False
		else:
			i += 1
	if i == n:
		return True
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	if prime(n) == True:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		x = 1
		while 1:
			if prime(n - 1 + x) == True and prime(x) == False:
				break
			else:
				x += 1
		y = 4
		while 1:
			if prime((n - 1) * x + y) == True and prime(y) == False:
				break
			else:
				y += 1
		for i in range(n):
			for j in range(n):
				if i != n - 1 and j != n - 1:
					print(1, end=' ')
				elif i == n - 1 and j == n - 1:
					print(y, end=' ')
				else:
					print(x, end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	a[0][n - 1] = 1
	a[0][0] = 1
	a[n - 1][0] = 1
	a[n - 1][n - 1] = 1
	for i in range(0, n - 1):
		a[i][i] = 1
		a[i][i + 1] = 1
	for line in a:
		print(*line)


from bisect import *
from collections import *
from math import gcd, ceil, sqrt, floor, inf
from heapq import *
from typing import List
from itertools import *
from operator import add, mul, sub, xor, truediv, floordiv
from functools import *
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

@lru_cache(None)
def fact(x):
	if x < 2:
		return 1
	return fact(x - 1) * x

@lru_cache(None)
def per(i, j):
	return fact(i) // fact(i - j)

@lru_cache(None)
def com(i, j):
	return per(i, j) // fact(j)

def linc(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t > f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rinc(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t < f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def ldec(f, t, l, r):
	while l < r:
		mid = (l + r) // 2
		if t < f(mid):
			l = mid + 1
		else:
			r = mid
	return l

def rdec(f, t, l, r):
	while l < r:
		mid = (l + r + 1) // 2
		if t > f(mid):
			r = mid - 1
		else:
			l = mid
	return l

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def binfun(x):
	c = 0
	for w in arr:
		c += ceil(w / x)
	return c

def lowbit(n):
	return n & -n

def inverse(a, m):
	a %= m
	if a <= 1:
		return a
	return (1 - inverse(m, a) * m) // a % m

class BIT:

	def __init__(self, arr):
		self.arr = arr
		self.n = len(arr) - 1

	def update(self, x, v):
		while x <= self.n:
			self.arr[x] += v
			x += x & -x

	def query(self, x):
		ans = 0
		while x:
			ans += self.arr[x]
			x &= x - 1
		return ans

class smt:

	def __init__(self, l, r, arr):
		self.l = l
		self.r = r
		self.value = (1 << 31) - 1 if l < r else arr[l]
		mid = (l + r) // 2
		if l < r:
			self.left = smt(l, mid, arr)
			self.right = smt(mid + 1, r, arr)
			self.value &= self.left.value & self.right.value

	def setvalue(self, x, val):
		if self.l == self.r:
			self.value = val
			return
		mid = (self.l + self.r) // 2
		if x <= mid:
			self.left.setvalue(x, val)
		else:
			self.right.setvalue(x, val)
		self.value = self.left.value & self.right.value

	def ask(self, l, r):
		if l <= self.l and r >= self.r:
			return self.value
		val = (1 << 31) - 1
		mid = (self.l + self.r) // 2
		if l <= mid:
			val &= self.left.ask(l, r)
		if r > mid:
			val &= self.right.ask(l, r)
		return val

class DSU:

	def __init__(self, n):
		self.c = [-1] * n

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def find(self, x):
		if self.c[x] < 0:
			return x
		self.c[x] = self.find(self.c[x])
		return self.c[x]

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if self.c[u] < self.c[v]:
			(u, v) = (v, u)
		self.c[u] += self.c[v]
		self.c[v] = u
		return True

	def size(self, x):
		return -self.c[self.find(x)]

class UFS:

	def __init__(self, n):
		self.parent = [i for i in range(n)]
		self.ranks = [0] * n

	def find(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def union(self, u, v):
		(pu, pv) = (self.find(u), self.find(v))
		if pu == pv:
			return False
		if self.ranks[pu] >= self.ranks[pv]:
			self.parent[pv] = pu
			if self.ranks[pv] == self.ranks[pu]:
				self.ranks[pu] += 1
		else:
			self.parent[pu] = pv

def Prime(n):
	c = 0
	prime = []
	flag = [0] * (n + 1)
	for i in range(2, n + 1):
		if not flag[i]:
			prime.append(i)
			c += 1
		for j in range(c):
			if i * prime[j] > n:
				break
			flag[i * prime[j]] = prime[j]
			if i % prime[j] == 0:
				break
	return prime

def dij(s, graph):
	d = {}
	d[s] = 0
	heap = [(0, s)]
	seen = set()
	while heap:
		(dis, u) = heappop(heap)
		if u in seen:
			continue
		for v in graph[u]:
			if v not in d or d[v] > d[u] + graph[u][v]:
				d[v] = d[u] + graph[u][v]
				heappush(heap, (d[v], v))
	return d

def isp(x):
	if x == 1:
		return False
	for y in range(2, int(sqrt(x)) + 1):
		if x % y == 0:
			return False
	return True
t = N()
for i in range(t):
	n = N()
	ans = [[1] * n for _ in range(n)]
	for x in range(n, 10000):
		if isp(x) and (not isp(x - n + 1)):
			for i in range(n):
				ans[i][i] = x - n + 1
			break
	for i in range(n):
		print(*ans[i])


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	matrix = [[str(0) for i in range(n)] for j in range(n)]
	for i in range(n):
		matrix[i][i] = '1'
	matrix[n - 1][0] = '1'
	for i in range(n - 1):
		matrix[i][i + 1] = '1'
	for p in matrix:
		print(' '.join(p))


for _ in range(int(input())):
	n = int(input())
	b = [[0 for j in range(n)] for i in range(n)]
	for j in range(n):
		for k in range(n):
			if j == k:
				b[j][k] = 1
				b[j][k - 1] = 1
	for j in b:
		print(*j)


from collections import Counter
import string
import math
import sys
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))

def printDivisors(n):
	divisors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				divisors.append(i)
			else:
				divisors.extend((i, n // i))
		i = i + 1
	return divisors

def countTotalBits(num):
	binary = bin(num)[2:]
	return len(binary)

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(vary(1)):
	n = vary(1)
	if isPrime(n):
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		ft = n - 1
		pt = 4
		while 1:
			if isPrime(pt) == False:
				if isPrime(pt + ft) == True:
					break
				else:
					pt += 1
			else:
				pt += 1
		kt = 0
		for i in range(n):
			kt += 1
			for j in range(n):
				if j == kt - 1:
					print(pt, end=' ')
				else:
					print(1, end=' ')
			print()


import sys
for i in range(0, int(sys.stdin.readline())):
	nxn = int(sys.stdin.readline())
	j = [0] * nxn
	for n in range(0, nxn - 1):
		u = [0] * nxn
		u[n] = 4
		u[n + 1] = 1
		sys.stdout.write(' '.join(map(str, u)))
		sys.stdout.write('\n')
	j[nxn - 1] = 4
	j[0] = 1
	sys.stdout.write(' '.join(map(str, j)))
	sys.stdout.write('\n')


pn = int(input())
nn = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157]
for i in range(pn):
	n = int(input())
	if nn.count(n) == 1:
		ans = [1 for i in range(n)]
		for i in range(n):
			print(*ans)
	else:
		ans = [1 for i in range(n)]
		for i in nn:
			if i > n and nn.count(1 + i - n) == 0:
				for j in range(n):
					ans = [1 for i in range(n)]
					ans[j] = 1 + i - n
					print(*ans)
				break


for _ in range(int(input())):
	n = int(input())
	l = [1, 4] + [0] * (n - 2)
	for x in range(n):
		l = [l[-1]] + l[:n - 1]
		print(*l)


t = int(input())
for _ in range(t):
	n = int(input())
	x = n - 2
	l = []
	for i in range(n):
		m = [0 for j in range(n)]
		m[i] = 1
		m[(i + 1) % n] = 1
		l.append(m)
	for i in l:
		print(*i)


from __future__ import division, print_function
import bisect
import math
import heapq
import itertools
import sys
from collections import deque
from atexit import register
from collections import Counter
from functools import reduce
if sys.version_info[0] < 3:
	from io import BytesIO as stream
else:
	from io import StringIO as stream
if sys.version_info[0] < 3:

	class dict(dict):

		def items(self):
			return dict.iteritems(self)

		def keys(self):
			return dict.iterkeys(self)

		def values(self):
			return dict.itervalues(self)
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def sync_with_stdio(sync=True):
	global input, flush
	if sync:
		flush = sys.stdout.flush
	else:
		sys.stdin = stream(sys.stdin.read())
		input = lambda : sys.stdin.readline().rstrip('\r\n')
		sys.stdout = stream()
		register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))

def main():
	for _ in range(int(input())):
		n = int(input())
		for i in range(n):
			for j in range(n):
				if i != j and (i + 1) % n != j:
					print(0, end=' ')
				else:
					print(1, end=' ')
			print()
sync_with_stdio(False)
main()


def prime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
while t:
	t += -1
	n = int(input())
	ans = []
	for i in range(n - 1):
		tmp = [1] * (n - 1)
		ans.append(tmp)
	pm = 0
	for i in range(n, 100005):
		if prime(i):
			if prime(i - n + 1) == 0:
				pm = i - n + 1
				break
	tt = pm * (n - 1) + 1
	pm2 = 0
	for i in range(tt, 100005):
		if prime(i):
			if prime(i - tt + 1) == 0:
				pm2 = i - tt + 1
				break
	for i in range(n - 1):
		ans[i].append(pm)
	for i in ans:
		print(*i)
	for i in range(n - 1):
		print(pm, end=' ')
	print(pm2)


test = int(input())
for _ in range(test):
	n = int(input())
	for i in range(n):
		for j in range(n):
			if i == n - 1 and j == 0:
				print(1, end=' ')
			elif j == i:
				print(1, end=' ')
			elif j - 1 == i:
				print(1, end=' ')
			else:
				print(0, end=' ')
		print('')


import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
from collections import deque

def main():
	try:
		for _ in range(inp()):
			n = inp()
			a = [1, 1]
			a.extend([0] * (n - 2))
			a = deque(a)
			if n == 2:
				print('1 1')
				print('1 1')
				continue
			for i in range(n):
				print(*list(a))
				a.rotate()
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for i in range(t):
	n = int(input())
	x = n - 1
	z = 0
	for i in range(x + 1, 100000):
		if isPrime(i + x) and (not isPrime(i)):
			z = i
			break
	for i in range(n):
		for j in range(n):
			if j == i:
				print(z, end=' ')
			else:
				print(1, end=' ')
		print()


def is_com(n):
	if n == 0 or n == 1:
		return False
	if n == 2:
		return True
	for i in range(2, int(n ** 0.5) + 2):
		if n % i == 0:
			return False
	return True
ints = list('1234567890')

def main():
	n = int(input())
	a = [[0] * n for i in range(n)]
	for i in range(n - 1):
		a[i][i] = 1
		a[i][n - 1] = 1
		a[n - 1][i] = 1
	i = 0
	while 1:
		if not is_com(i) and is_com(n - 1 + i):
			a[n - 1][n - 1] = i
			break
		i += 1
	for i in a:
		for j in i:
			print(j, end=' ')
		print('')
rw = int(input())
for wewq in range(rw):
	main()


def solve():
	a = [1, 1]
	n = int(input())
	for _ in range(n - 2):
		a.append(0)
	for i in range(n):
		for j in range(n):
			print(a[(i + j) % n], end=' ')
		print('')
t = int(input())
while t > 0:
	t -= 1
	solve()


x = int(input())
for h in range(0, x):
	n = int(input())
	matrix = []
	ar = [0] * n
	i = 0
	while i < n - 1:
		ar[i] = 1
		ar[i + 1] = 1
		i = i + 1
		matrix.append(ar)
		ar = [0] * n
	ar[0] = 1
	ar[n - 1] = 1
	matrix.append(ar)
	for j in range(0, n):
		print(*matrix[j])


t = int(input())
for case in range(t):
	n = int(input())
	for i in range(n):
		a = [0] * n
		a[i] = 4
		a[(i + 1) % n] = 9
		print(*a)


for t in range(int(input())):
	n = int(input())
	a = [[0] * n for _ in range(n)]
	for i in range(n):
		a[i][n - i - 1] = 1
		a[i][n - i - 2] = 1
	for i in a:
		print(*i)


from math import *
from sys import *

def prime(x):
	for i in range(2, int(sqrt(x)) + 1):
		if x % i == 0:
			return 0
	if x == 1:
		return 0
	return 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	for i in range(100, 1001):
		if prime(i):
			z = i - (n - 1)
			if prime(z) == 0:
				a = z
				break
	m = []
	for i in range(n):
		m.append([])
		for j in range(n):
			m[i].append(1)
	for i in range(n):
		m[i][i] = a
	for i in range(n):
		print(*m[i])


import sys
import math

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()

def isprime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(10000):
		if isprime(i):
			a.append(i)
	z = [[1 for i in range(n)] for j in range(n)]
	b = n - 1
	ans1 = 0
	for k in a:
		if k > b and isprime(k - b) == False:
			ans1 = k - b
			break
	ans2 = 0
	p = ans1 * (n - 1)
	for k in a:
		if k > p and isprime(k - p) == False:
			ans2 = k - p
			break
	for l in range(n):
		z[l][n - 1] = ans1
		z[n - 1][l] = ans1
		z[n - 1][n - 1] = ans2
	for i in range(n):
		print(*z[i])


for _ in range(int(input())):
	n = int(input())
	result = [[0] * n for i in range(n)]
	(s, ind) = (('1 4' + ' 0' * (n - 2) + ' ') * 2, 0)
	for i in range(n):
		result[i] = list(map(int, s[ind:ind + 2 * (n - 1) + 1].split()))
		ind += 2
	for elem in result:
		print(*elem)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	a = n - 1
	b = n
	l = -1
	for i in range(1, 10 ** 5 + 1):
		if isPrime(i + a) and isPrime(i) == False:
			l = i
			break
	c = [1] * b
	for i in range(b):
		d = c[:]
		d[i] = l
		print(*d)


def shift(a):
	l = len(a)
	t = a[l - 1]
	for i in range(l - 1, 0, -1):
		a[i] = a[i - 1]
	a[0] = t
	return a
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
a = [0 for i in range(102)]
for i in prime:
	a[i] = i
t = int(input())
while t > 0:
	n = int(input())
	a = [0 for i in range(n)]
	(a[0], a[1]) = (4, 1)
	for i in range(n):
		print(*a)
		a = shift(a)
	t -= 1


from math import sqrt
from itertools import count, islice
from functools import lru_cache

def is_prime(n):
	return n > 1 and all((n % i for i in islice(count(2), int(sqrt(n) - 1))))

@lru_cache(None)
def nextPrime(n):
	n += 1
	while not is_prime(n):
		n += 1
	return n

def solve(n):
	sq = [[1] * n for _ in range(n)]
	for i in range(n):
		if not is_prime(n):
			p = nextPrime(n)
			while is_prime(1 + p - n):
				p = nextPrime(p)
			sq[i][i] = 1 + p - n
	return sq
T = int(input())
for _ in range(T):
	n = int(input())
	res = solve(n)
	for row in res:
		print(' '.join(list(map(str, row))))


prime = [True for i in range(10 ** 5 + 1)]
p = 2
while p * p <= 10 ** 5:
	if prime[p] == True:
		for i in range(p * p, 10 ** 5 + 1, p):
			prime[i] = False
	p += 1
prime[0] = False
prime[1] = False
for _ in range(int(input())):
	n = int(input())
	if prime[n] == False:
		i = 1
		pos = 0
		while i <= 10 ** 5 + 1:
			i += 1
			if prime[i + (n - 1)] == True and prime[i] == False:
				pos = i
				break
		for i in range(n):
			l = []
			for j in range(n):
				if i == j:
					l.append(pos)
				else:
					l.append(1)
			print(*l)
	else:
		for i in range(n):
			l = []
			for j in range(n):
				l.append(1)
			print(*l)


for u in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		d = []
		for j in range(n):
			d.append(0)
		l.append(d)
	if n % 2 == 1:
		l[n // 2][n // 2] = 1
		l[n // 2][n - 1] = 1
		l[n - 1][n // 2] = 1
		l[n // 2][0] = 1
		l[0][n // 2] = 1
	for i in range(n):
		l[i][i] = 1
		l[i][n - 1 - i] = 1
	for i in l:
		print(*i)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
import string
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
primes_ = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
for _ in range(ii()):
	n = ii()
	if n in primes_:
		for i in range(n):
			print(*[1] * n)
	else:
		for i in range(n - 1):
			print('0 ' * i + '1 ' * 2 + '0 ' * (n - i - 2))
		print('1 ' + '0 ' * (n - 2) + '1')


from sys import stdin, stdout

def INI():
	return int(stdin.readline())

def INL():
	return [int(_) for _ in stdin.readline().split()]

def INS():
	return stdin.readline()

def OPS(ans):
	stdout.write(str(ans) + '\n')

def OPL(ans):
	[stdout.write(str(_) + ' ') for _ in ans]
	stdout.write('\n')
for _ in range(INI()):
	n = int(input())
	X = [1, 1] + [0] * (n - 2)
	for _ in range(n):
		print(*X[_:n], end=' ')
		print(*X[:_], end=' ')
		print()


import sys
from collections import defaultdict
from sys import maxsize

def ini():
	return int(sys.stdin.readline())

def inp():
	return map(int, sys.stdin.readline().strip().split())

def li():
	return list(map(int, sys.stdin.readline().strip().split()))

def isPrime(n):
	if n <= 1:
		return False
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for i in range(ini()):
	n = ini()
	if isPrime(n):
		x = [[1] * n] * n
		for i in range(n):
			print(*x[i])
	else:
		x = n * 6 + 1
		while not isPrime(x):
			x += 6
		t = x - 6 * (n - 2) - 1
		for i in range(n):
			for j in range(n):
				if i == j:
					print(t, end=' ')
				elif (i + 1) % n == j:
					print(1, end=' ')
				else:
					print(6, end=' ')
			print()


from sys import stdin, stdout
for t in range(int(stdin.readline())):
	n = int(stdin.readline())
	a = [[0 for _ in range(n)] for _ in range(n)]
	for i in range(n):
		a[i][i] = 1
	for i in range(1, n):
		a[i][i - 1] = 1
	a[0][n - 1] = 1
	for v in a:
		for l in v:
			stdout.write(str(l) + ' ')
		print()


def isprime(n):
	if n <= 1:
		return False
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
	l = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		l[i][i] = 1
	for i in range(n - 1):
		l[i][i + 1] = 10
	l[-1][0] = 10
	for i in l:
		print(*i)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c for j in range(b)] for i in range(a)]

def list3d(a, b, c, d):
	return [[[d for k in range(c)] for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** (-10)
for _ in range(INT()):
	N = INT()
	grid = list2d(N, N, 0)
	for i in range(N):
		grid[i][i] = 1
		grid[i][N - i - 1] = 1
	if N % 2 == 1:
		grid[N // 2][0] = 1
		grid[0][N // 2] = 1
	for i in range(N):
		print(*grid[i])


for _ in range(int(input())):
	n = int(input())
	l = []
	for i in range(n):
		k = [0] * n
		l.append(k)
	if n % 2 != 0:
		m = n - 1
		l[n - 1][n - 1] = 1
		l[n - 2][n - 1] = 1
		l[n - 1][n - 2] = 1
	else:
		m = n
	for i in range(m):
		for j in range(m):
			if i == j:
				l[i][i] = 1
	(a, b) = (0, m - 1)
	for i in range(m):
		l[a][b] = 1
		a += 1
		b -= 1
	for i in l:
		print(*i)


def testcase():
	n = int(input())
	for i in range(n):
		li = [0] * n
		li[i] = 1
		li[(i + 1) % n] = 1
		print(*li)
	return
import sys, os
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)
t = int(input())
for _ in range(t):
	testcase()


a = int(input())
for i in range(0, a):
	b = int(input())
	c = []
	o = ''
	k = ''
	if b % 2 == 0:
		for i in range(0, b // 2 + 1):
			for j in range(0, b):
				c.append('0')
			c[i] = 1
			c[-i - 1] = 1
			a = ' '.join(map(str, c))
			print(a)
			c = []
		for i in range(b - b // 2 + 1, b):
			for j in range(0, b):
				c.append('0')
			c[i] = 1
			c[-i - 1] = 1
			a = ' '.join(map(str, c))
			print(a)
			c = []
	else:
		for i in range(0, b // 2 - 1):
			for j in range(0, b):
				c.append('0')
			c[i] = 1
			c[-i - 1] = 1
			a = ' '.join(map(str, c))
			print(a)
			c = []
		k = '0 ' * (b // 2 - 1) + '1 1 1' + ' ' + '0 ' * (b // 2 - 1)
		for i in range(0, 3):
			print(k)
		for i in range(b - b // 2 + 1, b):
			for j in range(0, b):
				c.append('0')
			c[i] = 1
			c[-i - 1] = 1
			a = ' '.join(map(str, c))
			print(a)
			c = []


for _ in range(int(input())):
	N = int(input())
	ans = [1, 1]
	ans += [0] * (N - 2)
	for i in range(N):
		for j in range(N):
			print(ans[(i + j) % N], end=' ')
		print()


T = int(input())
for t in range(T):
	n = int(input())
	for i in range(n):
		res = []
		for j in range(n):
			if j == i or j == (i + 1) % n:
				res.append(1)
			else:
				res.append(0)
		print(' '.join(map(str, res)))


MAXPRIME = 10000
isPrime = [0 for _ in range(MAXPRIME + 1)]
isPrime[0] = -1
isPrime[1] = -1
for i in range(2, MAXPRIME // 2 + 1):
	if isPrime[i] == 0:
		for multiple in range(i * i, MAXPRIME + 1, i):
			if isPrime[multiple] == 0:
				isPrime[multiple] = i
primeNumberArr = []
for i in range(len(isPrime)):
	if isPrime[i] == 0:
		primeNumberArr.append(i)

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
t = int(input())
for _ in range(t):
	n = int(input())
	for p in primeNumberArr:
		if p > n - 1 and isPrime[p - (n - 1)] != 0:
			side = p - (n - 1)
			break
	for p in primeNumberArr:
		if p > side * (n - 1) and isPrime[p - side * (n - 1)] != 0:
			corner = p - side * (n - 1)
			break
	arr = []
	for i in range(n - 1):
		arr.append([1] * (n - 1) + [side])
	arr.append([side] * (n - 1) + [corner])
	multiLineArrayOfArraysPrint(arr)


n = int(input())
for i in range(n):
	a = int(input())
	l = []
	for i in range(a):
		for j in range(a):
			if i == j or i == j - 1 or (i == a - 1 and j == 0):
				l.append(1)
			else:
				l.append(0)
	for j in range(a):
		for i in range(0 + a * j, a + a * j):
			print(l[i], end=' ')
		print('\n')


def rightRotate(lists, num):
	output_list = []
	for item in range(len(lists) - num, len(lists)):
		output_list.append(lists[item])
	for item in range(0, len(lists) - num):
		output_list.append(lists[item])
	return output_list
for _ in range(int(input())):
	n = int(input())
	l = [0] * (n - 1)
	l[0] = 1
	l.append(162)
	for i in range(n):
		for j in l:
			print(j, end=' ')
		print()
		l = rightRotate(l, 1)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(input())):
	n = int(input())
	for i in range(1, 10 ** 5):
		if not isPrime(i):
			sm = i * (n - 1) + 1
			if isPrime(sm):
				break
	i = str(i)
	i += ' '
	for j in range(n):
		s = i * j + '1 ' + i * (n - j - 1)
		print(s)


for i in range(int(input())):
	x = int(input())
	arr = [[0] * x for i in range(x)]
	for i in range(x):
		for j in range(x):
			if i == j:
				arr[i][j] = 1
			if j == x - i - 1:
				arr[i][x - i - 1] = 1
	if x % 2 != 0:
		arr[x // 2][x // 2 + 1] = 1
		arr[x // 2 - 1][x // 2] = 1
	for item in arr:
		print(*item)


for _ in range(int(input())):
	n = int(input())
	if n == 2:
		print(1, 1)
		print(1, 1)
	else:
		for i in range(n - 1):
			s = (n - i - 2) * '0' + 2 * '1' + '0' * i
			s = list(s)
			print(*s)
		a = '1' + (n - 2) * '0' + '1'
		a = list(a)
		print(*a)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
n = inp()
for _ in range(n):
	m = inp()
	ans = []
	for i in range(m):
		ans.append([])
		for j in range(m):
			ans[i].append(0)
		ans[i][i] = 1
		ans[i][-1 - i] = 1
	if m % 2 == 1:
		ans[i // 2][i // 2 + 1] = 1
		ans[i // 2 + 1][i // 2] = 1
	for i in range(m):
		res = ''
		for x in ans[i]:
			res += str(x) + ' '
		print(res)


for q in range(int(input())):
	x = int(input())
	n = x
	if x == 2:
		print(1, 1)
		print(1, 1)
	else:
		x -= 3
		a = [(98 - x) * 4] + x * [4] + [8] + [1]
		for i in range(n):
			print(*a)
			a = a[1:] + [a[0]]


for i in range(int(input())):
	n = int(input())
	p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973]
	if n > 3:
		x = n - 1
		c = [1] * x
		for i in p:
			if i - x not in p and i - x > 0:
				c.append(i - x)
				break
		for i in range(n - 1):
			print(*c)
		y = [c[-1]] * (n - 1)
		k = sum(y)
		for i in p:
			if i - k not in p and i - k > 0:
				y.append(i - k)
				break
		print(*y)
	if n == 3:
		print(1, 1, 1)
		print(1, 1, 1)
		print(1, 1, 1)
	if n == 2:
		print(1, 1)
		print(1, 1)


t = int(input())
l = [1, 1, 4, 1, 6, 1, 4, 9, 4, 1, 6, 1, 4, 8, 4, 1, 6, 1, 10, 9, 6, 1, 6, 4, 4, 6, 4, 1, 8, 1, 10, 6, 6, 4, 6, 1, 4, 6, 4, 1, 18, 1, 4, 8, 4, 1, 6, 4, 4, 8, 6, 1, 14, 8, 6, 6, 4, 1, 12, 1, 6, 6, 6, 4, 8, 1, 4, 6, 4, 1, 8, 1, 4, 8, 8, 6, 6, 1, 4, 8, 6, 1, 6, 4, 12, 12, 4, 1, 12, 6, 6, 9, 4, 10, 6, 1, 4, 9, 4]
for _ in range(t):
	n = int(input())
	x = l[n - 2]
	ans = []
	for i in range(n):
		ans.append([x] * n)
		ans[-1][i] = 1
	for i in ans:
		print(*i)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n = mint()
	for i in range(n, 10000):
		bad = False
		for j in range(2, i + 1):
			if j * j > i:
				break
			if i % j == 0:
				bad = True
		x = i - (n - 1)
		if x > 1:
			prime = True
			for j in range(2, x + 1):
				if j * j > x:
					break
				if x % j == 0:
					prime = False
			if prime:
				bad = True
		if not bad:
			for j in range(n):
				print(*[1 if k != j else x for k in range(n)])
			break
for i in range(mint()):
	solve()


l = []
n = 10 ** 5
prime = [True for i in range(n + 1)]
p = 2
while p * p <= n:
	if prime[p] == True:
		for i in range(p * p, n + 1, p):
			prime[i] = False
	p += 1
for p in range(2, n + 1):
	if prime[p]:
		l.append(p)
from bisect import bisect_left
for i in range(int(input())):
	n = int(input())
	a = l[bisect_left(l, n)]
	if prime[n]:
		l1 = [1] * n
		for j in range(n):
			print(*l1)
	else:
		x = l[a] - n + 1
		z = -1
		if prime[x]:
			z = 0
			x += 1
		if z < 0:
			l1 = [1] * (n - 1) + [l[a] - n + 1]
		else:
			l1 = [1] * (n - 2) + [0] + [x]
		for j in range(n):
			l2 = l1[j:] + l1[:j]
			print(*l2)


def isprime(x):
	if x < 1:
		return False
	if x == 1:
		return False
	for i in range(2, int(x ** 0.5) + 1):
		if x % i == 0:
			return False
	return True
t = int(input())
while t:
	t -= 1
	n = int(input())
	ans = [[1] * n for i in range(n)]
	cur = 1
	while True:
		if not isprime(cur) and isprime(cur + n - 1):
			break
		cur += 1
	end = 1
	while True:
		if not isprime(end) and isprime(end + (n - 1) * cur):
			break
		end += 1
	for i in range(n):
		for j in range(n):
			if i == n - 1 and j == n - 1:
				print(end)
			elif i == n - 1:
				print(cur, end=' ')
			elif j == n - 1:
				print(cur)
			else:
				print('1', end=' ')


for _ in range(int(input())):
	n = int(input())
	ans = [[0 for i in range(n)] for j in range(n)]
	i = 0
	while i < n:
		ans[i][i] = 1
		i += 1
	(i, j) = (0, n - 1)
	while i < n and j >= 0:
		ans[i][j] = 1
		i += 1
		j -= 1
	i = n // 2
	if n % 2 == 1:
		ans[i][0] = 1
		ans[0][i] = 1
	for i in range(n):
		for j in range(n):
			print(ans[i][j], end=' ')
		print('')


def check(num):
	if num > 1:
		for i in range(2, num):
			if num % i == 0:
				return False
				break
		else:
			return True
	else:
		return False
for _ in range(int(input())):
	n = int(input())
	f = 1 * n
	if check(f):
		for i in range(0, n):
			for j in range(0, n):
				print(1, end=' ')
			print(end='\n')
	else:
		for i in range(n, 10000):
			foo = 6 * i + 1
			if check(foo):
				fin = foo - 1 * (n - 1)
				if not check(fin):
					for i in range(0, n):
						for j in range(0, n):
							if i == j:
								print(fin, end=' ')
							else:
								print(1, end=' ')
						print(end='\n')
					break


def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
primenos = SieveOfEratosthenes(100)
arr = []
for (i, num) in enumerate(primenos):
	if num:
		arr.append(i)

def closest_left(arr, target):
	l = -1
	r = len(arr)
	m = (l + r) // 2
	while l + 1 < r:
		if arr[m] <= target:
			l = m
			m = (l + r) // 2
		else:
			r = m
			m = (l + r) // 2
	return arr[l]
for __ in range(int(input())):
	N = int(input())
	req = closest_left(arr, N)
	zeros = N - req
	ones = req
	to_print = ['1'] * ones
	to_print += ['0'] * zeros
	for i in range(N):
		print(' '.join(to_print[i:] + to_print[0:i]))


a = int(input())

def isPrime(b):
	if b == 1:
		return False
	for i in range(2, b):
		if b % i == 0:
			return False
	return True

def np(b, a):
	while True:
		if isPrime(b) and (not isPrime(b - a + 1)):
			return b
		b = b + 1
for i in range(0, a):
	b = int(input())
	n = np(b, b)
	c = n - b + 1
	arr = [[-1 for i in range(b)] for j in range(b)]
	for i in range(b):
		for j in range(b):
			if i == j:
				arr[i][j] = c
			else:
				arr[i][j] = 1
	for i in arr:
		print(' '.join(list(map(str, i))))


def isPrime(n):
	if n == 1 or n == 0:
		return False
	for i in range(2, int(n ** (1 / 2)) + 1):
		if n % i == 0:
			return False
	return True
t = int(input())
for i in range(t):
	n = int(input())
	ls = []
	for r in range(n - 1):
		row = []
		for k in range(n - 1):
			row.append(1)
		ls.append(row)
	for j in range(n - 1):
		for num in range(1, 100001):
			if isPrime(num) == False and isPrime(sum((tmp for tmp in ls[j])) + num) == True:
				ls[j].append(num)
				break
	temp_list = []
	for j in range(n - 1):
		for num in range(1, 100001):
			if isPrime(num) == False and isPrime(sum((tmp[j] for tmp in ls)) + num) == True:
				temp_list.append(num)
				break
	ls.append(temp_list)
	for num in range(1, 100001):
		if isPrime(num) == False and isPrime(sum(ls[n - 1]) + num) == True and (isPrime(sum((tmp[n - 1] for tmp in ls[:n - 1])) + num) == True):
			ls[n - 1].append(num)
			break
	for x in range(n):
		for y in range(n):
			print(ls[x][y], end=' ')
		print()


t = int(input())
for p in range(t):
	n = int(input())
	for i in range(n + 5, 10000000):
		z = False
		k = 2
		x = 0
		while k <= i:
			if i % k == 0:
				if k == i:
					x = i
					z = True
				else:
					break
			k += 1
		if z:
			break
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if j == i:
					print(x - n + 1, end=' ')
				else:
					print(1, end=' ')
			print('')
	else:
		for i in range(n):
			for j in range(n):
				if j == i:
					print(x - n - 2, end=' ')
				elif j == i + 1:
					print(4, end=' ')
				elif j == 0 and i == n - 1:
					print(4, end=' ')
				else:
					print(1, end='')
				print('')


def is_prime(l):
	if l <= 1 or l % 1 > 0:
		return False
	for z in range(2, l // 2 + 1):
		if l % z == 0:
			return False
	return True
t = int(input())
for i in range(t):
	n = int(input())
	q = 1
	while True:
		if is_prime(n - 1 + q * q):
			break
		q += 1
	seq = []
	for j in range(n - 1):
		seq.append(1)
	seq.append(q * q)
	ans = ''
	for j in range(n):
		s = ''
		for k in range(n):
			s += str(seq[(k + j) % n])
			if k != n - 1:
				s += ' '
		ans += s
		if j != n - 1:
			ans += '\n'
	print(ans)


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
import math

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(math.sqrt(n) + 1), 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def nextPrime(N):
	if N <= 1:
		return 2
	prime = N
	found = False
	while not found:
		prime = prime + 1
		if isPrime(prime) == True:
			found = True
	return prime
rr = sieve(500)

def main():
	r = rr
	for _ in range(ii()):
		n = ii()
		np = 4 * (n - 1)
		while np <= 100000.0:
			np = nextPrime(np)
			if not prime(np - 4 * (n - 1)) and prime(4 * (n - 1) + (np - 4 * (n - 1))):
				break
		s = [4] * (n - 1)
		np = np - 4 * (n - 1)
		for i in range(n):
			s.insert(i, np)
			print(*s)
			s.pop(i)
main()


t = int(input())
mass = [1] * 200
mass[0] = 0
mass[1] = 0
for i in range(2, 101):
	if mass[i]:
		for j in range(i + i, 200, i):
			mass[j] = 0
for q in range(t):
	n = int(input())
	if mass[n]:
		for i in range(n):
			print(*[1] * n)
	else:
		x = 0
		for i in range(1, 1000):
			if mass[n + i] and (not mass[i + 1]):
				x = i
				break
		for i in range(n):
			arr = [1] * i + [x + 1] + [1] * (n - i - 1)
			print(*arr)


import sys, os.path
from collections import *
from heapq import *
from copy import *
import math
mod = 10 ** 9 + 7
read = lambda : map(int, input().split())
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for t in range(int(input())):
	n = int(input())
	res = []
	for i in range(n):
		if i == 0 or i == 1:
			res.append(1)
		else:
			res.append(0)
	mat = []
	mat.append(res)
	for i in range(n - 1):
		l = deepcopy(mat[-1])
		a = l.pop()
		new = [a] + l
		mat.append(new)
	for i in mat:
		print(*i)


def isPrime(n):
	if n <= 1:
		return False
	for i in range(2, n):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	x = isPrime(n)
	if x:
		for i in range(n):
			k = []
			for j in range(n):
				k.append(1)
			print(*k)
	else:
		c = 0
		k = [1, 1]
		for i in range(2, n):
			k.append(0)
		for i in range(n):
			print(*k)
			l = []
			l.append(k[-1])
			l += k[:-1]
			k = l


t = int(input())
p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
while t > 0:
	t -= 1
	n = int(input())
	if n in p:
		arr = [1] * n
		for i in range(n):
			print(*arr)
		continue
	else:
		for i in range(len(p)):
			if p[i] > n:
				ans = p[i]
				break
		arr = [0] * n
		arr[0] = ans - 1
		arr[1] = 1
		for i in range(n - 2):
			print(*arr)
			(arr[i + 1], arr[i + 2]) = (arr[i + 2], arr[i + 1])
			(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
		print(*arr)
		(arr[0], arr[-1]) = (arr[-1], arr[0])
		(arr[-1], arr[-2]) = (arr[-2], arr[-1])
		print(*arr)


def test_prime(n):
	if n == 1:
		return False
	elif n == 2:
		return True
	else:
		for x in range(2, n):
			if n % x == 0:
				return False
		return True
q = int(input())
for x in range(q):
	n = int(input())
	lt = []
	for i in range(n):
		temp = []
		for j in range(n):
			temp.append(1)
		lt.append(temp)
	flag = 0
	tlt = lt[0]
	while flag == 0:
		temp = sum(tlt)
		temp2 = tlt[0]
		if test_prime(temp) and test_prime(temp2) == False:
			flag = 1
		else:
			tlt[0] = tlt[0] + 1
	add = tlt[0]
	for i in range(n):
		for j in range(n):
			if i == j:
				lt[i][j] = add
	for i in range(n):
		print(*lt[i])


import sys
t = int(input())
for tdx in range(t):
	n = int(input())
	for idx in range(n):
		for jdx in range(n):
			if jdx == idx:
				sys.stdout.write('1')
			elif jdx == idx + 1:
				sys.stdout.write('1')
			elif jdx == 0 and idx == n - 1:
				sys.stdout.write('1')
			else:
				sys.stdout.write('0')
			sys.stdout.write(' ')
		print('')


t = int(input())
while t:
	t += -1
	n = int(input())
	m = n
	ans = []
	for i in range(n):
		ans.append([0] * m)
	c = 0
	for i in range(n):
		for j in range(2):
			ans[i][c % m] = 1
			c -= -1
	for i in ans:
		print(*i, sep=' ')


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
from itertools import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = inp()
while t:
	t -= 1
	n = inp()
	r = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		r[i][i] = 1
	for i in range(n):
		r[i][n - i - 1] = 1
	if n % 2:
		r[n // 2 + 1][n // 2] = 1
		r[n // 2][-1] = 1
	for i in r:
		print(*i)


def solve():
	n = int(input())
	grid = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i - 1 == j or j - 1 == i:
				print('1', end=' ')
			else:
				print('0', end=' ')
		print('\n')
t = int(input())
while t:
	solve()
	t -= 1


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log2, ceil
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from bisect import insort
from collections import Counter
from collections import deque
from heapq import heappush, heappop, heapify
from itertools import permutations, combinations
mod = int(1000000000.0) + 7
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())
ips = lambda : stdin.readline().rstrip()
out = lambda x: stdout.write(str(x) + '\n')

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
prime = SieveOfEratosthenes(int(100000.0))
pr = []
for i in range(2, 100001):
	if prime[i]:
		pr.append(i)
t = ip()
for _ in range(t):
	n = ip()
	ans = [[1] * n for i in range(n)]
	ch = sum(ans[0])
	ele = None
	prime[1] = False
	for i in pr:
		if i > ch:
			ele = i - ch + 1
			if not prime[ele]:
				break
	for i in range(n):
		ans[i][i] = ele
	for i in ans:
		print(*i)


t = int(input())
while t:
	size = int(input())
	foo = 0
	array = [[foo for i in range(size)] for j in range(size)]
	array[0][0] = 1
	array[0][size - 1] = 1
	array[size - 1][0] = 1
	array[size - 1][size - 1] = 1
	for i in range(0, size - 1):
		for j in range(i, i + 2):
			array[i][j] = 1
	for i in range(size):
		for j in range(size):
			print(array[i][j], end=' ')
		print()
	t -= 1


def isPrime(num):
	if num <= 1:
		return False
	if num <= 3:
		return True
	if num % 2 == 0 or num % 3 == 0:
		return False
	i = 5
	while i * i <= num:
		if num % i == 0 or num % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(input())):
	n = int(input())
	if isPrime(n):
		l = [[1 for j in range(n)] for i in range(n)]
	else:
		l = [[1 if i == j or i + j == n - 1 else 0 for j in range(n)] for i in range(n)]
		if n % 2 == 1:
			mid = n // 2
			(i, j) = (mid, mid)
			l[i + 1][j] = 1
			l[i][j - 1] = 1
	for i in range(n):
		print(*l[i])


for _ in range(int(input())):
	a = int(input())
	b = 1
	if a % 2 == 0:
		while b <= a * a:
			if (b + (b - 1) // a) % a == 0:
				print(104 - a)
				b = b + 1
			else:
				print(1)
				b = b + 1
	else:
		while b <= a * a:
			if (b + (b - 1) // a) % a == 0:
				print(105 - a)
				b = b + 1
			elif (b + (b - 1) // a) % a == 1:
				print(4)
				b = b + 1
			else:
				print(1)
				b = b + 1


m = 10 ** 5
t = [False for i in range(m + 1)]
t[0] = True
t[1] = True
i = 2
while i * i <= m:
	for j in range(2 * i, m + 1, i):
		t[j] = True
	i += 1
p = [i for i in range(1, m + 1) if not t[i]]
for _ in range(int(input())):
	n = int(input())
	ans = 0
	for i in range(len(p)):
		if p[i] > n:
			ans = p[i - 1]
			break
	arr = []
	x = ans
	y = n - ans
	for i in range(y):
		t = [0] * i + [1] * x + [0] * (y - i)
		arr.append(t)
	for i in range(x):
		t = [1] * i + [0] * y + [1] * (x - i)
		arr.append(t)
	for i in arr:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	k = -1
	primes = []
	for a in range(2, 10002):
		for b in range(2, a):
			if a % b == 0:
				break
		else:
			if a >= n and a - n + 1 not in primes:
				k = a
				break
			else:
				primes.append(a)
	for i in range(n):
		temp = [1] * n
		temp[i] = k - n + 1
		temp = list(map(str, temp))
		s = ' '.join(temp)
		print(s)


def linear_seive(n):
	is_composite = [False] * n
	is_composite[0] = is_composite[1] = True
	primes = []
	for i in range(2, n):
		if is_composite[i] == False:
			primes.append(i)
		j = 0
		while j < len(primes) and i * primes[j] < n:
			is_composite[i * primes[j]] = True
			if i % primes[j] == 0:
				break
			j += 1
	return primes
primes = linear_seive(int(100000.0 + 1))
isprimes = set(primes)
for _ in range(int(input())):
	n = int(input())
	ans = [[1] * n for _ in range(n)]
	t = n - 1
	from bisect import bisect_right
	it = bisect_right(primes, t)
	for i in range(it, len(primes)):
		if primes[i] - t not in isprimes:
			for j in range(n - 1):
				ans[j][-1] = primes[i] - t
				ans[-1][j] = primes[i] - t
			break
	t = ans[-1][-2] * (n - 1)
	it = bisect_right(primes, t)
	for i in range(it, len(primes)):
		if primes[i] - t not in isprimes:
			ans[-1][-1] = primes[i] - t
			break
	for i in ans:
		print(*i)


def solve(N):
	for i in range(N):
		if i == 0:
			a = [1, 1] + [0] * (N - 2)
		elif i == N - 1:
			a = [0] * (N - 2) + [1, 1]
		else:
			t = i - 1
			a = [0] * t + [1, 1, 1] + [0] * (N - t - 3)
		print(' '.join(map(str, a)))
T = int(input())
for _ in range(T):
	N = int(input())
	solve(N)


lst = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(int(input())):
	n = int(input())
	b = [1] * (n - 1)
	for i in range(len(lst)):
		if lst[i] - sum(b) > 0 and lst[i] - sum(b) not in lst:
			break
	b.append(lst[i] - sum(b))
	for i in range(len(b)):
		c = b[i:] + b[:i]
		print(*c, sep=' ')


from math import ceil, sqrt

def prime(n):
	if n == 2:
		return True
	if n == 1:
		return False
	for x in range(2, ceil(sqrt(n)) + 1):
		if n % x == 0:
			return False
	return True
for i in range(int(input())):
	n = int(input())
	b = 1 * (n - 1)
	j = 0
	while True:
		j += 1
		if not prime(j) and prime(j + b):
			break
	square = [['1' for x in range(n)] for y in range(n)]
	for x in range(n):
		square[x][x] = str(j)
		print(' '.join(square[x]))


test_cases = range(int(input()))
for test_case in test_cases:
	n = int(input())
	for i in range(n - 1):
		print(' '.join(['0' for j in range(i)] + ['1', '1'] + ['0' for j in range(n - i - 2)]))
	print(' '.join(['1'] + ['0' for i in range(n - 2)] + ['1']))


from os import path
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input().rstrip()

def mi():
	return map(int, input().split())

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('a')
file = 1

def solve():
	prime = [1] * 100000
	f = []
	for i in range(2, 100000):
		if prime[i]:
			f.append(i)
			for j in range(2 * i, 100000, i):
				prime[j] = 0
	for _ in range(ii()):
		n = ii()
		x = 1
		for i in f:
			if i >= n and prime[i - n + 1] == 0:
				x = i - n + 1
				break
		ans = [[1] * n for i in range(n)]
		for i in range(n):
			ans[i][i] = x
		for i in range(n):
			print(*ans[i])
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output1.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


import math

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(math.sqrt(n) + 1), 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def nextPrime(N):
	if N <= 1:
		return 2
	prime = N
	found = False
	while not found:
		prime = prime + 1
		if isPrime(prime) == True:
			found = True
	return prime
for i in range(int(input())):
	n = int(input())
	nn = n
	np = nextPrime(nn)
	while isPrime(np - n + 1) == True:
		nn = np + 1
		np = nextPrime(nn)
	for i in range(n):
		for j in range(n):
			if i == j:
				print(1 + np - n, end=' ')
			else:
				print(1, end=' ')
		print()


t = int(input())
while t:
	t = t - 1
	n = int(input())
	a = {}
	prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
	for x in prime:
		if x not in a:
			a[x] = 1
	newL = [[4, 98], [6, 96], [8, 94], [9, 93], [10, 92], [12, 90], [14, 88], [15, 87], [16, 86], [18, 84], [20, 82], [21, 81], [22, 80], [24, 78], [25, 77], [26, 76], [27, 75], [28, 74], [30, 72], [32, 70], [33, 69], [34, 68], [35, 69], [36, 66], [38, 64], [39, 63], [40, 62], [42, 60], [44, 58], [45, 57], [46, 56], [48, 54], [49, 55], [50, 52], [51, 51], [52, 50], [54, 48], [55, 49], [56, 46], [57, 45], [58, 44], [60, 42], [62, 40], [63, 39], [64, 38], [65, 39], [66, 36], [68, 34], [69, 33], [70, 32], [72, 30], [74, 28], [75, 27], [76, 26], [77, 25], [78, 24], [80, 22], [81, 21], [82, 20], [84, 18], [85, 25], [86, 16], [87, 15], [88, 14], [90, 12], [91, 49], [92, 10], [93, 9], [94, 8], [95, 9], [96, 6], [98, 4], [99, 9], [100, 4]]
	for x in newL:
		a[x[0]] = x[1]
	ans = [[1 for x in range(n)] for y in range(n)]
	for i in range(len(ans)):
		for j in range(len(ans)):
			if i == j:
				print(a[n], end=' ')
			else:
				print('1', end=' ')
		print()


arr = [1 for i in range(151)]

def sieve(n):
	i = 2
	while i * i <= 150:
		if arr[i] == 1:
			for j in range(i * i, 150, i):
				arr[j] = 0
		i += 1
sieve(150)
from collections import *
al = defaultdict(int)
for i in range(1, 101):
	if i == 1:
		arr[i] = 1
		continue
	if arr[i] == 1:
		al[i] = 1
	elif arr[i] == 1:
		arr[i] = 1
	else:
		j = 1
		while 1:
			if arr[j] == 0 and arr[j + i - 1] == 1:
				al[i] = j
				break
			else:
				j += 1
a = int(input())
for i in range(a):
	x = int(input())
	for i in range(x):
		for j in range(x):
			if i == j:
				print(al[x], end=' ')
			else:
				print(1, end=' ')
		print(' ')


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	if y == 0:
		return 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(moves, n):
	val = (moves + 1) // 2
	rem = moves - val
	sol = (val + 1) * (rem + 1)
	return sol < n

def sortSecond(val):
	return val[1]
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect
import functools as fnt
from decimal import Decimal

def solve():
	n = scanner()
	ans = []
	for i in range(n):
		val = []
		for j in range(n):
			if i == j or j == (i + 1) % n:
				val.append(1)
			else:
				val.append(0)
		ans.append(val)
	for x in ans:
		print(*x)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
t = scanner()
for i in range(t):
	solve()


import math
import sys
from collections import OrderedDict

class Graph:

	def __init__(self, Nodes, is_directed=False):
		self.nodes = Nodes
		self.adj_list = {}
		self.is_directed = is_directed
		for node in self.nodes:
			self.adj_list[node] = []

	def add_edge(self, u, v):
		self.adj_list[u].append(v)
		if self.is_directed == False:
			self.adj_list[v].append(u)

	def print_graph(self):
		for node in self.nodes:
			print(node, '->', self.adj_list[node])

	def degree_node(self, node):
		return len(self.adj_list[node])

	def dfsUTIL(self, v, visited, parents=[]):
		visited[v] = True
		for i in self.adj_list[v]:
			if visited[i] == False:
				self.dfsUTIL(i, visited, parents)
				parents.append(i)

	def dfs(self, v):
		visited = [False] * (max(self.adj_list) + 1)
		parents = [v]
		self.dfsUTIL(v, visited, parents)
		return parents

def factorial(n):
	if n == 0 or n == 1:
		return 1
	ans = 1
	for i in range(2, n + 1):
		ans *= i
	return ans

def dict_sort(ans):
	ans = sorted(ans.items(), reverse=True, key=lambda kv: kv[1])

def is_prime(n):
	if n == 1:
		return 0
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def seive_primes(n):
	flag = [0] * (n + 2)
	flag[1] = flag[0] = 1
	i = 2
	while i * i <= n + 1:
		if flag[i] == 0:
			j = i * i
			while j <= n + 1:
				flag[j] = 1
				j += i
		i += 1
	return flag

def factors(n):
	d = {}
	while n % 2 == 0:
		if 2 in d:
			d[2] += 1
		else:
			d[2] = 1
		n /= 2
	for i in range(3, int(n ** 0.5) + 1, 2):
		while n % i == 0:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
			n /= i
	n = int(n)
	if n > 1:
		d[n] = 1
	return d

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def alphabates():
	return 'abcdefghijklmnopqrstuvwxyz'

def binary_search(ls, n, flag):
	low = 0
	hi = n - 1
	while low <= hi:
		mid = (low + hi) // 2
		if ls[mid] == flag:
			return mid
		elif ls[mid] > flag:
			hi = mid - 1
		else:
			low = mid + 1
	return -1

def qdrt(a, b, c):
	chk = b * b - 4 * a * c
	if chk >= 0:
		ans1 = (-b + chk ** 0.5) / (2 * a)
		ans2 = (-b - chk ** 0.5) / (2 * a)
		return [int(ans1), int(ans2)]
	return -1

def permutation(n, r):
	if n < r:
		return 0
	ans = 1
	for i in range(n - r + 1, n + 1):
		ans *= i
	return ans

def combination(n, r):
	if n < r:
		return 0
	ans = 1
	for i in range(r):
		ans *= n - i
	div = 1
	for i in range(2, r + 1):
		div *= i
	return ans // div

def ispow2(n):
	ans = int(math.log(n, 2))
	if 2 ** ans == n:
		return True
	return False

def _2dmat(n):
	flag = [[i + 1 for i in range(n)]]
	for i in range(2, n + 1):
		new = []
		for j in range((i - 1) * n + 1, i * n + 1):
			new.append(j)
		flag.append(new)
	return flag

def sumn(n):
	return n * (n + 1) // 2

def arrinp():
	ls = list(map(int, input().split()))
	return ls

def mult_inp():
	return map(int, input().split())

def main():
	primes = seive_primes(103)
	n = int(input())
	if primes[n] == 0:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	elif n % 2 == 0:
		dp = []
		for i in range(n):
			ans = []
			for j in range(n):
				if i - j == 0 or n - i - 1 - j == 0:
					ans.append(1)
				else:
					ans.append(0)
			dp.append(ans)
		for i in dp:
			for j in i:
				print(j, end=' ')
			print()
	else:
		dp = []
		for i in range(n):
			ans = []
			for j in range(n):
				if (i == n // 2 and (j == 0 or j == n - 1) or (i == 0 and j == n // 2) or (i == n - 1 and j == n // 2)) or i - j == 0 or n - i - 1 - j == 0:
					ans.append(1)
				else:
					ans.append(0)
			dp.append(ans)
		for i in dp:
			for j in i:
				print(j, end=' ')
			print()
for _ in range(int(input())):
	main()


t = int(input())
while t > 0:
	n = int(input())
	ans = [[0] * n for i in range(n)]
	i = 0
	if n % 2 == 1:
		for i in range(3):
			for j in range(3):
				ans[i][j] = 1
		i = 3
	while i < n:
		for k in range(2):
			for j in range(2):
				ans[i + k][i + j] = 1
		i += 2
	for i in range(n):
		for j in range(n):
			print(ans[i][j], end=' ')
		print()
	t -= 1


for _ in range(int(input())):
	a = []
	n = int(input())
	if n == 2:
		for _ in range(2):
			print(' '.join([str(i) for i in [1, 1]]))
	elif n == 3:
		for _ in range(3):
			print(' '.join([str(i) for i in [1, 1, 1]]))
	else:
		if n % 2 == 0:
			for i in range(n):
				a.append([1 if j == 0 + i or j == n - 1 - i else 0 for j in range(n)])
		else:
			for i in range(n - 1):
				a.append([1 if j == i or j == n - 2 - i else 0 for j in range(n - 1)])
			a.append([1 if j == n - 2 else 0 for j in range(n - 1)])
			for i in range(n):
				if i == n - 1 or i == n - 2:
					a[i].append(1)
				else:
					a[i].append(0)
		for i in range(n):
			print(' '.join([str(j) for j in a[i]]))


t = int(input())
for i in range(t):
	n = int(input())
	l = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
	if n in l:
		for i in range(n):
			print('1 ' * n)
	else:
		x = []
		for i in range(n):
			y = []
			for j in range(n):
				if i == j:
					y.append(1)
				else:
					y.append(0)
			x.append(y)
		i = 0
		j = n - 1
		while i < n and j >= 0:
			x[i][j] = 1
			i += 1
			j -= 1
		if n % 2 != 0:
			x[0][n // 2] = 1
			x[n // 2][0] = 1
		for i in range(n):
			for j in range(n):
				print(x[i][j], end=' ')
			print()


m = 100001
primes = [True] * m
for i in range(2, m):
	if primes[i]:
		j = i * i
		while j < m:
			primes[j] = False
			j += i
primes[0] = False
primes[1] = False
T = int(input())
for t in range(T):
	n = int(input())
	if primes[n]:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		k = 1
		l = 0
		while k < m:
			if not primes[k]:
				if primes[n - 1 + k]:
					l = k
					break
			k += 1
		for i in range(n):
			for j in range(n):
				if i == j:
					print(l, end=' ')
				else:
					print(1, end=' ')
			print()


t = int(input())
for _ in range(t):
	n = int(input())
	c = 0
	for i in range(2, n):
		if n % i == 0 and n != i:
			c = 1
			break
	if c == 1:
		for i in range(n):
			for j in range(n):
				if i - j == 1 or i - j == -1:
					print(1, end=' ')
				elif i == 0 and j == 0:
					print(1, end=' ')
				elif i == n - 1 and j == n - 1:
					print(1, end=' ')
				else:
					print(0, end=' ')
			print()
	else:
		for i in range(n):
			for j in range(n):
				print('1', end=' ')
			print()


import sys, copy
sys.setrecursionlimit(10 ** 5 + 5)
from collections import defaultdict as dft, deque

def ml():
	return list(map(int, input().split()))

def mp():
	return map(int, input().split())
mxn = 10 ** 5 + 5
arr = [1] * mxn
arr[1] = 0
for i in range(2, mxn):
	if arr[i]:
		j = 2
		while i * j < mxn:
			arr[i * j] = 0
			j += 1
ls = [[], [], [1, 1]]
sm = 2
for i in range(3, 101):
	ls.append(ls[i - 1][:])
	for j in range(1, mxn):
		if arr[j] == 0:
			if arr[j + sm] == 1:
				sm += j
				ls[i].append(j)
				break

def solve():
	n = int(input())
	res = ls[n][:]
	ans = []
	for i in range(n):
		ans.append(copy.deepcopy(res))
		temp = res.pop()
		res = [temp] + res
	for nm in ans:
		print(*nm)
t = int(input())
for _ in range(t):
	solve()


import math

def isPrime(num):
	if num < 2:
		return False
	elif num == 2:
		return True
	elif num % 2 == 0:
		return False
	for i in range(3, math.floor(math.sqrt(num)) + 1, 2):
		if num % i == 0:
			return False
	return True

def callIsPrime(input_num=1000):
	numbers = []
	for i in range(1, input_num):
		if isPrime(i):
			numbers.append(i)
	return numbers
for _ in range(int(input())):
	t = int(input())
	A = [[0] * t for i in range(t)]
	for i in range(t):
		A[t - 1][i] = 1
		A[t - 2][i] = 1
		A[i][t - 1] = 1
		A[i][t - 2] = 1
	P = callIsPrime(1500)
	tmp = t
	while tmp not in P or 1 + tmp - t in P:
		tmp += 1
	A[t - 1][t - 2] += tmp - t
	A[t - 2][t - 1] += tmp - t
	tmp = sum(A[t - 1])
	while tmp not in P or A[t - 1][t - 1] + tmp - sum(A[t - 1]) in P:
		tmp += 1
	A[t - 1][t - 1] += tmp - sum(A[t - 1])
	for h in range(t):
		print(' '.join(map(str, A[h])))


for _ in range(int(input())):
	n = int(input())
	count = 0
	for i in range(n):
		list_s = [0] * n
		if i != n - 1:
			(list_s[count], list_s[count + 1]) = (1, 1)
		else:
			(list_s[0], list_s[i]) = (1, 1)
		print(*list_s)
		count = count + 1


t = int(input())

def isprime(n):
	if n == 1:
		return False
	if n == 2:
		return True
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def nextnonprime(n):
	n += 1
	while isprime(n):
		n += 1
	return n
for i in range(t):
	n = int(input())
	if isprime(n):
		for j in range(n):
			for k in range(n):
				print('1', end=' ')
			print()
	else:
		np = 1
		while True:
			if isprime(np * (n - 1) + 1):
				if isprime(np + n - 1):
					for j in range(n):
						for k in range(n):
							if j == n - 1 and k == 0:
								print('1', end=' ')
							elif j == n - 1 or k == 0:
								print(str(np), end=' ')
							else:
								print('1', end=' ')
						print()
					break
			np = nextnonprime(np)


import sys
from collections import deque
from copy import *
from itertools import permutations as prm, product

def eprint(*args):
	print(*args, file=sys.stderr)
zz = 1
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def string(s):
	return ''.join(s)

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

def bo(i):
	return ord(i) - ord('A')

def li():
	return [int(xx) for xx in input().split()]

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

def mi():
	return map(int, input().split())

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
d = {2: 1}
for i in range(3, 1001):
	j = 2
	flag = 0
	while j * j <= i:
		if i % j == 0:
			flag = 1
			break
		j += 1
	if flag == 0:
		d[i] = 1
p = {}
for i in d:
	for j in range(1, 101):
		if i - j + 1 not in d:
			p[j] = i
while t > 0:
	t -= 1
	n = fi()
	p[1] = 1
	a = [1] * (n - 1)
	a.append(p[n] - n + 1)
	a = a[:n]
	for i in range(n):
		u = a[i:] + a[:i]
		print(*u)


import os
from sys import stdin, stdout

def solve(tc):
	n = int(stdin.readline().strip())
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if i == j or i + j == n - 1:
				ans[i][j] = 1
	if n % 2 == 1:
		ans[n // 2][n // 2 + 1] = 1
		ans[n // 2 + 1][n // 2] = 1
	for i in range(n):
		print(' '.join(map(lambda x: str(x), ans[i])))
tcs = 1
tcs = int(stdin.readline().strip())
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [[0 for x in range(n)] for y in range(n)]
	a = 0
	b = n - 1
	for i in range(n):
		arr[i][i] = 1
		arr[a][b] = 1
		a += 1
		b -= 1
	if n % 2 != 0:
		arr[0][n // 2] = arr[n - 1][n // 2] = arr[n // 2][0] = arr[n // 2][n - 1] = 1
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))
is_prime = [True] * 5001
is_prime[0] = False
is_prime[1] = False
for i in range(2, 5001):
	if not is_prime[i]:
		continue
	for j in range(i * 2, 5001, i):
		is_prime[j] = False
primes = []
for i in range(2, 5001):
	if is_prime[i]:
		primes.append(i)
s = set(primes)
t = read_int()
for case_num in range(t):
	n = read_int()
	left = n - 1
	last = 0
	for prime in primes:
		if prime > left and prime - left not in s:
			last = prime - left
			break
	a = [[1 for _ in range(n)] for _ in range(n)]
	for i in range(n):
		a[i][n - 1] = last
		a[n - 1][i] = last
	left = last * (n - 1)
	for prime in primes:
		if prime > left and prime - left not in s:
			a[n - 1][n - 1] = prime - left
			break
	for i in range(n):
		print(' '.join(map(str, a[i])))


def sakurajima(n):
	if n < 9:
		return [0, 1, 1, 0, 1, 0, 1, 0]
	l = [1] * n
	for i in range(2, int(n ** 0.5) + 1):
		if l[i - 1] == 1:
			for j in range(i ** 2, n + 1, i):
				if j % i == 0:
					l[j - 1] = 0
	l[0] = 0
	return l

def panda(n, l):
	if l[n - 1] == 1:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		cnt = 0
		for i in l[n - 1:]:
			cnt += 1
			if i == 1:
				if l[cnt - 1] == 1:
					continue
				else:
					break
		for i in range(n):
			for j in range(n):
				if j == i:
					print(cnt, end=' ')
				else:
					print(1, end=' ')
			print()
l = sakurajima(200)
for _ in range(int(input())):
	n = int(input())
	panda(n, l)


for _ in range(int(input())):
	n = int(input())
	lt = []
	(l, r) = (-1, 0)
	for i in range(n):
		k = []
		l += 1
		r += 1
		r = r % n
		for j in range(n):
			if j == l or j == r:
				k.append(1)
			else:
				k.append(0)
		lt.append(k)
	for i in lt:
		print(*i)


def simple(a):
	arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109]
	if arr.count(a) != 0:
		return True
	else:
		return False
testes = int(input())
for counter in range(testes):
	n = int(input())
	output = []
	for i in range(n):
		output.append(1)
	syper = 104 - n
	s = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
	i1 = 0
	while simple(syper):
		syper = s[i1] + 1 - n
		i1 += 1
	for i in range(n - 1, -1, -1):
		output[i] = syper
		print(*output)
		output[i] = 1


from sys import stdin

class Input:

	def __init__(self):
		self.it = iter(stdin.readlines())

	def line(self):
		return next(self.it).strip()

	def array(self, sep=' ', cast=int):
		return list(map(cast, self.line().split(sep=sep)))

def testcases(unknown=False):
	inpt = Input()

	def testcases_decorator(func):
		(cases,) = [float('Inf')] if unknown else inpt.array()
		while cases > 0:
			try:
				func(inpt)
				cases -= 1
			except StopIteration:
				break
	return testcases_decorator

@testcases()
def solve(inpt):
	(n,) = inpt.array()
	two = [[1, 1], [1, 1]]
	three = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
	a = [[0] * n for i in range(n)]
	while n:
		if n == 3:
			n -= 3
			a[n][n:n + 3] = [1, 1, 1]
			a[n + 1][n:n + 3] = [1, 1, 1]
			a[n + 2][n:n + 3] = [1, 1, 1]
		else:
			n -= 2
			a[n][n:n + 2] = [1, 1]
			a[n + 1][n:n + 2] = [1, 1]
	for x in a:
		print(*x)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	a = [[1, 1]]
	for j in range(2, n):
		a[0].append(0)
	for j in range(1, n):
		q = a[-1]
		p = [q[-1]]
		p = p + q[:n - 1]
		a.append(p)
	for j in range(0, n):
		print(' '.join(map(str, a[j])))


import os
import sys
from io import BytesIO, IOBase
_str = str
BUFSIZE = 8192

def str(x=b''):
	return x if type(x) is bytes else _str(x).encode()

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def inp():
	return sys.stdin.readline().rstrip()

def mpint():
	return map(int, sys.stdin.readline().split(' '))

def itg():
	return int(sys.stdin.readline())
DATA = [1, 4, 6, 8, 10, 12, 18, 20, 22, 26, 30, 34, 36, 42, 44, 46, 50, 52, 60, 66, 72, 74, 76, 78, 80, 82, 102, 108, 114, 116, 118, 126, 128, 132, 136, 138, 144, 146, 150, 154, 158, 162, 166, 170, 174, 186, 196, 198, 210, 222, 228, 236, 240, 244, 246, 254, 270, 280, 282, 300, 306, 312, 318, 320, 322, 326, 334, 336, 354, 356, 370, 374, 378, 382, 386, 396, 406, 410, 412, 422, 426, 430, 432, 438, 444, 450, 456, 462, 476, 484, 488, 490, 498, 504, 506, 510, 514, 516, 522, 528, 534, 536, 546]
DATA = tuple(map(_str, DATA))
from collections import deque

def main():
	n = itg()
	dq = deque(DATA[:n])
	for _ in range(n):
		print(' '.join(dq))
		dq.rotate(1)
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
for _ in range(itg()):
	main()


for i in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		a.append([0] * n)
	for i in range(1, n):
		a[i][i] = 1
		a[i - 1][i] = 1
	a[0][0] = 1
	a[n - 1][0] = 1
	for i in range(n):
		print(*a[i])


import sys
import math
from collections import Counter, defaultdict
LI = lambda : list(map(int, input().split()))
MAP = lambda : map(int, input().split())
IN = lambda : int(input())
S = lambda : input()
prime = [True for i in range(1000 + 1)]

def SieveOfEratosthenes(n):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1

def case():
	n = IN()
	x = n - 1
	for j in range(1, 1000):
		if prime[x + j] and (not prime[j]):
			break
	for i in range(n):
		print('1 ' * i + str(j) + ' 1' * (n - i - 1))
for _ in range(IN()):
	SieveOfEratosthenes(1000)
	case()


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i += 6
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	t = []
	for i in range(n):
		a = []
		for j in range(n):
			a.append(1)
		t.append(a)
	x = isPrime(n)
	if x == True:
		for i in range(n):
			for j in range(n):
				t[i][j] = 1
	else:
		s = n * (n - 1)
		maxx = 1000000000
		for i in range(1, maxx):
			f1 = isPrime(s + i)
			f2 = isPrime(i)
			if f1 == True and f2 == False:
				maxx = i
				break
		for i in range(n):
			for j in range(n):
				if i == j:
					t[i][j] = maxx
				else:
					t[i][j] = n
	for i in range(n):
		for j in range(n):
			print(t[i][j], end=' ')
		print()


import sys
input = sys.stdin.buffer.readline

def solution():
	n = int(input())
	x = n
	while True:
		flag = 0
		for i in range(2, int(x ** 0.5) + 1):
			if x % i == 0:
				flag = 1
				break
		if flag == 0:
			f = 0
			for i in range(2, int((1 + x - n) ** 0.5) + 1):
				if (x + 1 - n) % i == 0:
					f = 1
					break
			if f == 1:
				break
		x += 1
	y = x - n
	for i in range(n):
		for j in range(n):
			if i == j:
				print(y + 1, end=' ')
			else:
				print(1, end=' ')
		print()
t = int(input())
for _ in range(t):
	solution()


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def is_prime(n):
	if n == 1:
		return False
	i = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def gift():
	for _ in range(t):
		n = int(input())
		for i in range(n):
			posA = i
			posB = n - 1 - i
			if n % 2 == 0 or (n % 2 and abs(posA - posB) > 2):
				thisRow = min(posA, posB) * [0] + [1] + (abs(posA - posB) - 1) * [0] + [1] + (n - 1 - max(posA, posB)) * [0]
			else:
				posMid = n // 2
				thisRow = (posMid - 1) * [0] + [1, 1, 1] + (n - 1 - posMid - 1) * [0]
			yield ' '.join((str(x) for x in thisRow))
t = int(input())
ans = gift()
print(*ans, sep='\n')


import math

def isPrime(x):
	if x == 1 or (not x % 2 and x != 2):
		return False
	for i in range(3, int(math.sqrt(x)) + 1, 2):
		if not x % i:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	ans = []
	for i in range(n, 10 ** 5 + 1):
		if isPrime(i) and (not isPrime(i - n + 1)):
			break
	for _ in range(n - 1):
		ans.append([1] * (n - 1) + [i - n + 1])
	ans.append([i - n + 1] * (n - 1) + [0])
	for j in range((i - n + 1) * (n - 1) + 1, 10 ** 5 + 1):
		if isPrime(j) and (not isPrime(j - (i - n + 1) * (n - 1))):
			ans[-1][-1] = j - (i - n + 1) * (n - 1)
			break
	for a in ans:
		print(*a)


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(input())):
	n = int(input())
	for i in range(n, int(100000.0 + 1)):
		if not isPrime(i - (n - 1)) and isPrime(i):
			ans = []
			for k in range(n):
				ans.append([0] * n)
			for k in range(n):
				for j in range(n):
					if k == j:
						ans[k][j] = i - (n - 1)
					else:
						ans[k][j] = 1
			for d in ans:
				print(' '.join(map(str, d)))
			break


def prime(n):
	if n < 3:
		return n == 2
	i = 2
	while i * i <= n:
		if n % i == 0:
			return 0
		i += 1
	else:
		return 1
for _ in range(int(input())):
	n = int(input())
	k = n - 1
	while not prime(k) or prime(k - n + 1):
		k += 1
	for i in range(n):
		for j in range(n):
			if i == j:
				print(k - n + 1, end=' ')
			else:
				print(1, end=' ')
		print()


def prime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	if prime(n):
		num = [1 for i in range(n)]
		for i in range(n):
			print(*num)
	else:
		num = [[0 for i in range(n)] for j in range(n)]
		for i in range(n):
			num[i][i] = 1
			num[i][(i + 1) % n] = 1
		for i in num:
			print(*i)


from sys import stdin, exit
from bisect import bisect_left as bl, bisect_right as br
input = lambda : stdin.readline()[:-1]
intput = lambda : int(input())
sinput = lambda : input().split()
intsput = lambda : map(int, sinput())

def dprint(*args, **kwargs):
	if debugging:
		print(*args, **kwargs)
debugging = 1
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749]
t = intput()
for _ in range(t):
	n = intput()
	for prime in primes:
		if prime >= n - 1 and prime - (n - 1) not in primes:
			break
	row = [1] * (n - 1) + [prime - n + 1]
	for offset in range(n):
		for i in range(n):
			print(row[(i + offset) % n], end=' ')
		print()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		if n % 2 == 0:
			for i in range(n):
				s = ''
				for j in range(n):
					if j == i or j == n - 1 - i:
						s += '1 '
					else:
						s += '0 '
				print(s)
		else:
			mid = n // 2
			for i in range(n):
				s = ''
				for j in range(n):
					if abs(j - mid) < 2 and abs(i - mid) < 2:
						s += '1 '
					elif j == i or j == n - 1 - i:
						s += '1 '
					else:
						s += '0 '
				print(s[:-1])
main()


import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())
from sys import maxsize

def solve():
	for _ in range(get_int()):
		n = get_int()
		temp = [1, 1] + [0] * (n - 2)
		for i in range(n):
			print(*temp)
			temp = temp[-1:] + temp[:-1]
solve()


t = int(input())
for _ in range(t):
	n = int(input())
	k = n - 1
	isPrime = False
	while not isPrime:
		k += 1
		for i in range(2, k):
			if k % i == 0 and k / i > 0:
				break
			if i == k - 1:
				isPrime = True
		if isPrime:
			found = False
			for i in range(2, k - n + 1):
				if (k - n + 1) % i == 0 and (k - n + 1) / i > 0:
					found = True
					break
			if found:
				break
			else:
				isPrime = False
	p = k - n + 1
	ansrow = [1 for _ in range(n - 1)]
	ansrow.append(p)
	for i in range(n):
		print(' '.join(map(str, ansrow[n - i - 1:] + ansrow[:n - i - 1])))


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if j == i:
					print(4, end=' ')
				elif j + i == n - 1:
					print(9, end=' ')
				else:
					print(0, end=' ')
			print()
	else:
		k = n // 2 + 1
		l = n // 2
		for i in range(n):
			for j in range(n):
				if i == l - 1 and j == k - 1 or (i == l and j == k):
					print(10, end=' ')
				elif j + i == n - 1:
					print(9, end=' ')
				elif j == i:
					print(4, end=' ')
				else:
					print(0, end=' ')
			print()


from __future__ import print_function
from collections import Counter, OrderedDict
from itertools import permutations as perm
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
import math
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(n,) = gil()
	ans = [[0 for _ in range(n)] for _ in range(n)]
	for i in range(n):
		ans[i][i] = 1
		ans[i][(i + 1) % n] = 1
	for a in ans:
		print(*a)


P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151]
t = int(input())
i = 0
while i < t:
	n = int(input())
	simpl = 0
	index = 0
	while index < 27:
		if n < P[index]:
			simpl = P[index]
			break
		index += 1
	Q = simpl - n + 1
	while Q in P:
		index += 1
		simpl = P[index]
		Q = simpl - n + 1
	index = 0
	answer = ''
	poz = 0
	while index < n:
		j = 0
		while j < n:
			if j == poz:
				answer += str(Q) + ' '
			else:
				answer += '1 '
			j += 1
		poz += 1
		answer += '\n'
		index += 1
	print(answer)
	i += 1


t = int(input())
for q in range(t):
	n = int(input())
	a = [1, 1]
	for i in range(n - 2):
		a.append(0)
	for i in range(n):
		print(*a[i:], *a[:i])


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def findx(n):
	a = n * (n - 1)
	z = 1
	while True:
		if isPrime(z + a) and (not isPrime(z)):
			return z
		else:
			z += 1
for i in range(int(input())):
	n = int(input())
	if isPrime(n):
		arr = [[1 for i in range(n)] for j in range(n)]
	else:
		arr = [[0 for i in range(n)] for j in range(n)]
		x = findx(n)
		for i in range(n):
			for j in range(n):
				if i == j:
					arr[i][j] = x
				else:
					arr[i][j] = n
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()


import math

def isprime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def prime(n):
	for i in range(n, n + 1000):
		if isprime(i) and isprime(i - (n - 1)) == False:
			return i
t = int(input())
for w in range(t):
	n = int(input())
	if isprime(n):
		for i in range(n):
			print(*[1] * n)
	else:
		for i in range(n):
			l = [1] * i + [prime(n) - (n - 1)] + [1] * (n - i - 1)
			print(*l)


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691]
while t:
	n = int(input())
	for i in prime:
		if i > n and i - n + 1 not in prime:
			break
	arr = [1] * (n - 1)
	arr.append(i - n + 1)
	ll = []
	for i in range(n):
		l = []
		for j in range(i, n):
			l.append(arr[j])
		for j in range(0, i):
			l.append(arr[j])
		ll.append(l)
	for i in ll:
		print(*i)
	t -= 1


t = int(input())
ps = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 173, 179, 181, 191, 193, 197, 199]

def process():
	n = int(input())
	p0 = 0
	for p in ps:
		if p < n:
			continue
		if p - n + 1 not in ps:
			p0 = p
			break
	ans = [['1' for i in range(n)] for j in range(n)]
	for i in range(n):
		ans[i][i] = str(p0 - n + 1)
	for i in range(n):
		print(' '.join(ans[i]))
for it in range(t):
	process()


from collections import defaultdict
import math

def SieveOfEratosthenes(n):
	l = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			l.append(p)
	return l
l = SieveOfEratosthenes(100000)
for _ in range(int(input())):
	(n,) = map(int, input().split())
	q = [[1 for i in range(n)] for j in range(n)]
	for i in l:
		if i > n - 1 and i - n + 1 not in l:
			break
	for j in range(n):
		q[j][-1] = i - n + 1
		if j == n - 1:
			for k in range(n):
				q[j][k] = i - n + 1
	for k in l:
		if k > (n - 1) * (i - n + 1) and k - (n - 1) * (i - n + 1) not in l and (k - (n - 1) * (i - n + 1) > 0):
			break
	q[-1][-1] = k - (n - 1) * (i - n + 1)
	for i in q:
		print(*i)


import sys
from collections import defaultdict
input = sys.stdin.readline
pr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157]
pr.reverse()
for _ in range(int(input())):
	n = int(input())
	lis = []
	for i in range(n):
		lis.append([1] * n)
	f = 0
	for i in range(2, n):
		if n % i == 0:
			f = 1
			break
	if f == 1:
		f = 0
		for i in pr:
			x = i - (n - 1)
			for j in range(2, x):
				if x % j == 0:
					f = 1
					break
			if f == 1:
				break
		for i in range(n):
			lis[i][i] = x
	for l in lis:
		print(*l)


primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
import math

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(math.sqrt(n) + 1), 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	if n in primes:
		for i in range(1, n + 1):
			for j in range(1, n + 1):
				print(1, end=' ')
			print()
		continue
	else:
		matrix = [[1 for i in range(1, n + 1)] for j in range(1, n + 1)]
		r = n - 1
		nextP = 0
		i = 0
		while True:
			if isPrime(i) == False and isPrime(r + i) == True:
				nextP = i
				break
			i += 1
		for i in range(n):
			for j in range(n):
				if i == j:
					matrix[i][j] = nextP
		for i in matrix:
			print(*i)


for _ in range(int(input())):
	n = int(input())
	a = [[0] * n for i in range(n)]
	for i in range(1, n):
		a[i][i - 1] = 1
		a[i][i] = 1
	a[0][0] = 1
	a[0][n - 1] = 1
	for i in range(n):
		print(' '.join((str(j) for j in a[i])))


import sys
import math
from collections import defaultdict, Counter
l = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199])
t = int(input())
for i in range(t):
	n = int(input())
	if n in l:
		a = [1] * n
		for j in range(n):
			print(*a)
	else:
		p = 100
		while p in l or p + n - 1 not in l:
			p -= 1
		for j in range(n):
			for k in range(n):
				if j == k:
					print(p, end=' ')
				else:
					print(1, end=' ')
			print()


def main():
	p = int(input())
	t = p
	while t > 0:
		n = int(input())
		for i in range(n):
			for j in range(n):
				if i == j or i == j + 1 or i == j - 1:
					print('1', end=' ')
				else:
					print('0', end=' ')
			print()
		t -= 1
main()


def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
for _ in range(int(input())):
	n = int(input())
	if n % 2 == 1:
		k = 0
		kk = n - 1
		for i in range(n):
			for j in range(n):
				if j == k or j == kk:
					print(1, end=' ')
				elif i == n // 2 - 1 and j == (n + 1) // 2 - 1 or (i == (n + 1) // 2 - 1 and j == (n + 1) // 2):
					print(1, end=' ')
				else:
					print(0, end=' ')
			k += 1
			kk -= 1
			print()
	else:
		k = 0
		kk = n - 1
		for i in range(n):
			for j in range(n):
				if j == k or j == kk:
					print(1, end=' ')
				else:
					print(0, end=' ')
			k += 1
			kk -= 1
			print()


from math import *
from random import *
from bisect import *
from math import *
for t in range(int(input())):
	n = int(input())
	if n <= 3:
		for i in range(n):
			for j in range(n):
				print('1', end=' ')
			print()
		continue
	mas = [0 for i in range(n)]
	mas[1] = 1
	mas[0] = 4
	for i in range(n):
		j = i
		for sas in range(n):
			print(mas[j], end=' ')
			j = (j + 1) % n
		print()


for tt in range(int(input())):
	n = int(input())
	mat = [[0 for i in range(n)] for j in range(n)]
	if n % 2 == 0:
		for i in range(n):
			for j in range(n):
				if i == j:
					mat[i][j] = 1
					mat[i][n - j - 1] = 1
	else:
		for i in range(n):
			for j in range(n):
				if i == j:
					mat[i][j] = 1
					mat[i][n - j - 1] = 1
		mat[n // 2][n // 2 + 1] = 1
		mat[n // 2 + 1][n // 2] = 1
	for i in mat:
		print(*i)


t = int(input())
x = [2]
n = 10000
for i in range(3, n + 1, 2):
	if i > 10 and i % 10 == 5:
		continue
	for j in x:
		if j * j - 1 > i:
			x.append(i)
			break
		if i % j == 0:
			break
	else:
		x.append(i)
while t:
	n = int(input())
	s = [[1] * n] * n
	for i in range(1, 100):
		a = n - 1 + i
		if a in x and i not in x:
			q = i
			break
	for i in range(n):
		s[i][n - 1] = q
	s[n - 1] = [q] * n
	p = (n - 1) * q
	for i in range(1, 10000):
		q = p + i
		if q in x and i not in x:
			q = i
			break
	s[n - 1][n - 1] = q
	for i in range(n):
		if i != 0:
			print()
		for j in range(n):
			print(s[i][j], end=' ')
	print()
	t -= 1


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=300006, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] > k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data

def prime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
for ik in range(int(input())):
	n = int(input())
	l = [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
	for i in l:
		if prime(i + 1 - n) == False:
			e = i + 1
			break
	a = [[1 for i in range(n)] for j in range(n)]
	for i in range(n):
		a[i][i] = e - n
		print(*a[i])


from sys import stdin, stdout

def II():
	return int(stdin.readline())

def solve():
	n = II()
	a = ['0' for i in range(n - 2)] + ['1', '4'] + ['0' for i in range(n - 2)] + ['1', '4']
	for i in range(n):
		ans = [a[i] for i in range(i, i + n)]
		sans = ' '.join(ans) + '\n'
		stdout.write(sans)
t = II()
for _ in range(t):
	solve()


def reorder():
	test_cases = int(input())
	for t in range(test_cases):
		n = int(input())
		for i in range(n):
			for j in range(n):
				print('1' if abs(i - j) <= 1 else '0', end=' ')
			print()
reorder()


def solve():
	n = int(input())
	m = []
	for i in range(n):
		l = [0] * n
		m.append(l)
	for i in range(n):
		m[i][i] = 1
		if i + 1 < n:
			m[i][i + 1] = 4
	m[n - 1][0] = 4
	for i in range(n):
		print(*m[i])

def main():
	for _ in range(int(input())):
		solve()
main()


def main():
	n = int(input())
	grid = [['0' for _ in range(n)] for _ in range(n)]
	if n % 2:
		grid[(n - 1) // 2][0] = '1'
		grid[0][(n - 1) // 2] = '1'
	for i in range(n):
		grid[i][i] = '1'
		grid[i][n - i - 1] = '1'
	for i in range(n):
		print(*grid[i])
t = int(input())
for _ in range(t):
	main()


import math
import os
import random
import re
import sys
for _ in range(int(input())):
	n = int(input())
	p = [['0' for col in range(n)] for row in range(n)]
	x = 0
	for r in p:
		(r[x], r[x - 1]) = ('1', '1')
		x += 1
	for r in p:
		print(' '.join(r))


from sys import stdin
cas = int(stdin.readline().strip())
for caso in range(cas):
	n = int(stdin.readline().strip())
	mat = [[0 for i in range(n)] for j in range(n)]
	x = 0
	while x < n:
		lim = 2
		if n - x == 3:
			lim = 3
		for i in range(lim):
			for j in range(lim):
				mat[x + i][x + j] = 1
		if n - x == 3:
			x += 3
		else:
			x += 2
	for i in mat:
		print(*i)


def isprime(x):
	if x == 1:
		return False
	for i in range(2, x):
		if x % i == 0:
			return False
	return True

def f(n):
	for p in range(1, 2 * n):
		if not isprime(p) and isprime(n - 1 + p):
			break
	for i in range(n):
		s = []
		for j in range(n):
			if j == i:
				s.append(p)
			else:
				s.append(1)
		print(' '.join([str(t) for t in s]))
T = int(input())
for case in range(T):
	n = int(input())
	f(n)


t = int(input())
for i in range(t):
	l = int(input())
	for i in range(l):
		for j in range(l):
			if abs(j - i) <= 1:
				print('1 ', end='')
			else:
				print('0 ', end='')
		print()


t = int(input())
prime = [True] * (10 ** 5 + 1)
for i in range(2, 10 ** 5 + 1):
	if prime[i] == True:
		for j in range(i ** 2, 10 ** 5 + 1, i):
			prime[j] = False
for x in range(t):
	n = int(input())
	ans = [[1 for i in range(n)] for j in range(n)]
	if prime[n] == True:
		for i in range(len(ans)):
			print(*ans[i])
	else:
		for i in range(n, len(prime)):
			if prime[i] == True:
				if prime[i - n + 1] == False:
					next1 = i
					break
		for i in range(len(ans)):
			ans[i][i] = next1 - n + ans[i][i]
		for i in range(len(ans)):
			print(*ans[i])


t = int(input())
while t != 0:
	n = int(input())
	net = [1, 1]
	for i in range(n - 2):
		net.append(0)
	for i in range(n):
		print(*net)
		k = [0] * n
		for j in range(n):
			k[j] = net[(j + 1) % n]
		net = k
	t -= 1


def check_prime(i):
	if i == 2 or i == 3:
		return True
	elif i % 2 == 0 or i % 3 == 0:
		return False
	else:
		s = 5
		while s * s <= i:
			if i % s == 0 or i % (s + 2) == 0:
				return False
			s += 6
		return True
for i in range(int(input())):
	n = int(input())
	if n == 1:
		print(1)
	else:
		a = 4
		while True:
			if check_prime(n - 1 + a):
				break
			else:
				while True:
					a += 1
					if check_prime(a) == False:
						break
		b = 4
		while True:
			if check_prime(a * (n - 1) + b):
				break
			else:
				while True:
					b += 1
					if check_prime(b) == False:
						break
		m = [1] * (n - 1)
		m.append(a)
		r = [a] * (n - 1)
		l = []
		for i in range(n):
			if i == n - 1:
				r.append(b)
				l.append(r)
			else:
				l.append(m)
		for i in range(n):
			for j in range(n):
				print(l[i][j], end=' ')
			print('\n')


import sys
from bisect import bisect

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
for _ in range(iinput()):
	n = iinput()
	if n in primes:
		for _ in range(n):
			print(*[1 for _ in range(n)])
	else:
		ans = [[0 for _ in range(n)] for _ in range(n)]
		for j in range(n - 2):
			ans[-2][j] = ans[-1][j] = 1
		for i in range(n - 2):
			ans[i][-2] = ans[i][-1] = 1
		s = n - 2
		x = 0
		temp = -1
		while 1:
			if x + s in primes and x not in primes:
				temp = x
				break
			x += 1
		ans[-1][-1] = ans[-2][-2] = temp
		for item in ans:
			print(*item)


li = []
n = 100000
prime = [True for i in range(n + 1)]
p = 2
while p * p <= n:
	if prime[p] == True:
		for i in range(p * p, n + 1, p):
			prime[i] = False
	p += 1
for i in range(2, 100001):
	if prime[i] == True:
		li.append(i)
t = int(input())
for hh in range(0, t):
	n = int(input())
	mx = 151
	for i in range(0, len(li)):
		val = li[i]
		prv = val + 1 - n
		if prv > 0 and prime[prv] == False:
			mx = prv
			break
	for i in range(0, n):
		for j in range(0, n):
			if i == j:
				print(mx, end=' ')
			else:
				print(1, end=' ')
		print()


def is_prime(x):
	p = [True] * (x + 1)
	p[0] = 0
	p[1] = 0
	t = 2
	while t ** 2 <= x:
		if p[t] == 1:
			for j in range(t ** 2, x + 1, t):
				p[j] = 0
		t += 1
	return p
l = is_prime(101)
for u in range(int(input())):
	n = int(input())
	if n == 1:
		print(1)
	elif l[n]:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print('\n')
	else:
		x = []
		for i in range(n):
			x.append([0] * n)
		for i in range(n - 1):
			for j in range(n):
				if j == i or j == i + 1:
					x[i][j] = 1
		x[0][1] = 1
		x[-1][-1] = 1
		x[-1][0] = 1
		for i in range(n):
			print(*x[i])


import os

def main():
	test_cases = int(input())
	comps = [1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 132, 133, 134, 135, 136, 138, 140, 141, 142, 143, 144, 145, 146, 147, 148, 150, 152, 153, 154, 155, 156, 158, 159, 160, 161, 162, 164, 165, 166, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 180, 182, 183, 184, 185, 186, 187, 188, 189, 190, 192, 194, 195, 196, 198, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 224, 225, 226, 228, 230, 231, 232, 234, 235, 236, 237, 238, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 252, 253, 254, 255, 256, 258, 259, 260, 261, 262, 264, 265, 266, 267, 268, 270, 272, 273, 274, 275, 276, 278, 279, 280, 282, 284, 285, 286, 287, 288, 289, 290, 291, 292, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 308, 309, 310, 312, 314, 315, 316]
	comps_s = list(map(str, [1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 132, 133, 134, 135, 136, 138, 140, 141, 142, 143, 144, 145, 146, 147, 148, 150, 152, 153, 154, 155, 156, 158, 159, 160, 161, 162, 164, 165, 166, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 180, 182, 183, 184, 185, 186, 187, 188, 189, 190, 192, 194, 195, 196, 198, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 224, 225, 226, 228, 230, 231, 232, 234, 235, 236, 237, 238, 240, 242, 243, 244, 245, 246, 247, 248, 249, 250, 252, 253, 254, 255, 256, 258, 259, 260, 261, 262, 264, 265, 266, 267, 268, 270, 272, 273, 274, 275, 276, 278, 279, 280, 282, 284, 285, 286, 287, 288, 289, 290, 291, 292, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 308, 309, 310, 312, 314, 315, 316]))
	primes = set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133, 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223, 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313, 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429, 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529, 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639, 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733, 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859, 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957, 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071, 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171, 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279, 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393, 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491, 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617, 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731, 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831, 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933, 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037, 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119, 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241, 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343, 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437, 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527, 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613, 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713, 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823, 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923, 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009, 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127, 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229, 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337, 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457, 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577, 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687, 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759, 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877, 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967, 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083, 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221, 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347, 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447, 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551, 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653, 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747, 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831, 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939, 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073, 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161, 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269, 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349, 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443, 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559, 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649, 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749, 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859, 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959, 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069, 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187, 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301, 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421, 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529, 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649, 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747, 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883, 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981, 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077, 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191, 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321, 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401, 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491, 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599, 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729, 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839, 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939, 17957, 17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047, 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133, 18143, 18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233, 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329, 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439, 18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539, 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691, 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797, 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959, 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079, 19081, 19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211, 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309, 19319, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423, 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483, 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583, 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727, 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841, 19843, 19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949, 19961, 19963, 19973, 19979, 19991, 19993, 19997, 20011, 20021, 20023, 20029, 20047, 20051, 20063, 20071, 20089, 20101, 20107, 20113, 20117, 20123, 20129, 20143, 20147, 20149, 20161, 20173, 20177, 20183, 20201, 20219, 20231, 20233, 20249, 20261, 20269, 20287, 20297, 20323, 20327, 20333, 20341, 20347, 20353, 20357, 20359, 20369, 20389, 20393, 20399, 20407, 20411, 20431, 20441, 20443, 20477, 20479, 20483, 20507, 20509, 20521, 20533, 20543, 20549, 20551, 20563, 20593, 20599, 20611, 20627, 20639, 20641, 20663, 20681, 20693, 20707, 20717, 20719, 20731, 20743, 20747, 20749, 20753, 20759, 20771, 20773, 20789, 20807, 20809, 20849, 20857, 20873, 20879, 20887, 20897, 20899, 20903, 20921, 20929, 20939, 20947, 20959, 20963, 20981, 20983, 21001, 21011, 21013, 21017, 21019, 21023, 21031, 21059, 21061, 21067, 21089, 21101, 21107, 21121, 21139, 21143, 21149, 21157, 21163, 21169, 21179, 21187, 21191, 21193, 21211, 21221, 21227, 21247, 21269, 21277, 21283, 21313, 21317, 21319, 21323, 21341, 21347, 21377, 21379, 21383, 21391, 21397, 21401, 21407, 21419, 21433, 21467, 21481, 21487, 21491, 21493, 21499, 21503, 21517, 21521, 21523, 21529, 21557, 21559, 21563, 21569, 21577, 21587, 21589, 21599, 21601, 21611, 21613, 21617, 21647, 21649, 21661, 21673, 21683, 21701, 21713, 21727, 21737, 21739, 21751, 21757, 21767, 21773, 21787, 21799, 21803, 21817, 21821, 21839, 21841, 21851, 21859, 21863, 21871, 21881, 21893, 21911, 21929, 21937, 21943, 21961, 21977, 21991, 21997, 22003, 22013, 22027, 22031, 22037, 22039, 22051, 22063, 22067, 22073, 22079, 22091, 22093, 22109, 22111, 22123, 22129, 22133, 22147, 22153, 22157, 22159, 22171, 22189, 22193, 22229, 22247, 22259, 22271, 22273, 22277, 22279, 22283, 22291, 22303, 22307, 22343, 22349, 22367, 22369, 22381, 22391, 22397, 22409, 22433, 22441, 22447, 22453, 22469, 22481, 22483, 22501, 22511, 22531, 22541, 22543, 22549, 22567, 22571, 22573, 22613, 22619, 22621, 22637, 22639, 22643, 22651, 22669, 22679, 22691, 22697, 22699, 22709, 22717, 22721, 22727, 22739, 22741, 22751, 22769, 22777, 22783, 22787, 22807, 22811, 22817, 22853, 22859, 22861, 22871, 22877, 22901, 22907, 22921, 22937, 22943, 22961, 22963, 22973, 22993, 23003, 23011, 23017, 23021, 23027, 23029, 23039, 23041, 23053, 23057, 23059, 23063, 23071, 23081, 23087, 23099, 23117, 23131, 23143, 23159, 23167, 23173, 23189, 23197, 23201, 23203, 23209, 23227, 23251, 23269, 23279, 23291, 23293, 23297, 23311, 23321, 23327, 23333, 23339, 23357, 23369, 23371, 23399, 23417, 23431, 23447, 23459, 23473, 23497, 23509, 23531, 23537, 23539, 23549, 23557, 23561, 23563, 23567, 23581, 23593, 23599, 23603, 23609, 23623, 23627, 23629, 23633, 23663, 23669, 23671, 23677, 23687, 23689, 23719, 23741, 23743, 23747, 23753, 23761, 23767, 23773, 23789, 23801, 23813, 23819, 23827, 23831, 23833, 23857, 23869, 23873, 23879, 23887, 23893, 23899, 23909, 23911, 23917, 23929, 23957, 23971, 23977, 23981, 23993, 24001, 24007, 24019, 24023, 24029, 24043, 24049, 24061, 24071, 24077, 24083, 24091, 24097, 24103, 24107, 24109, 24113, 24121, 24133, 24137, 24151, 24169, 24179, 24181, 24197, 24203, 24223, 24229, 24239, 24247, 24251, 24281, 24317, 24329, 24337, 24359, 24371, 24373, 24379, 24391, 24407, 24413, 24419, 24421, 24439, 24443, 24469, 24473, 24481, 24499, 24509, 24517, 24527, 24533, 24547, 24551, 24571, 24593, 24611, 24623, 24631, 24659, 24671, 24677, 24683, 24691, 24697, 24709, 24733, 24749, 24763, 24767, 24781, 24793, 24799, 24809, 24821, 24841, 24847, 24851, 24859, 24877, 24889, 24907, 24917, 24919, 24923, 24943, 24953, 24967, 24971, 24977, 24979, 24989, 25013, 25031, 25033, 25037, 25057, 25073, 25087, 25097, 25111, 25117, 25121, 25127, 25147, 25153, 25163, 25169, 25171, 25183, 25189, 25219, 25229, 25237, 25243, 25247, 25253, 25261, 25301, 25303, 25307, 25309, 25321, 25339, 25343, 25349, 25357, 25367, 25373, 25391, 25409, 25411, 25423, 25439, 25447, 25453, 25457, 25463, 25469, 25471, 25523, 25537, 25541, 25561, 25577, 25579, 25583, 25589, 25601, 25603, 25609, 25621, 25633, 25639, 25643, 25657, 25667, 25673, 25679, 25693, 25703, 25717, 25733, 25741, 25747, 25759, 25763, 25771, 25793, 25799, 25801, 25819, 25841, 25847, 25849, 25867, 25873, 25889, 25903, 25913, 25919, 25931, 25933, 25939, 25943, 25951, 25969, 25981, 25997, 25999, 26003, 26017, 26021, 26029, 26041, 26053, 26083, 26099, 26107, 26111, 26113, 26119, 26141, 26153, 26161, 26171, 26177, 26183, 26189, 26203, 26209, 26227, 26237, 26249, 26251, 26261, 26263, 26267, 26293, 26297, 26309, 26317, 26321, 26339, 26347, 26357, 26371, 26387, 26393, 26399, 26407, 26417, 26423, 26431, 26437, 26449, 26459, 26479, 26489, 26497, 26501, 26513, 26539, 26557, 26561, 26573, 26591, 26597, 26627, 26633, 26641, 26647, 26669, 26681, 26683, 26687, 26693, 26699, 26701, 26711, 26713, 26717, 26723, 26729, 26731, 26737, 26759, 26777, 26783, 26801, 26813, 26821, 26833, 26839, 26849, 26861, 26863, 26879, 26881, 26891, 26893, 26903, 26921, 26927, 26947, 26951, 26953, 26959, 26981, 26987, 26993, 27011, 27017, 27031, 27043, 27059, 27061, 27067, 27073, 27077, 27091, 27103, 27107, 27109, 27127, 27143, 27179, 27191, 27197, 27211, 27239, 27241, 27253, 27259, 27271, 27277, 27281, 27283, 27299, 27329, 27337, 27361, 27367, 27397, 27407, 27409, 27427, 27431, 27437, 27449, 27457, 27479, 27481, 27487, 27509, 27527, 27529, 27539, 27541, 27551, 27581, 27583, 27611, 27617, 27631, 27647, 27653, 27673, 27689, 27691, 27697, 27701, 27733, 27737, 27739, 27743, 27749, 27751, 27763, 27767, 27773, 27779, 27791, 27793, 27799, 27803, 27809, 27817, 27823, 27827, 27847, 27851, 27883, 27893, 27901, 27917, 27919, 27941, 27943, 27947, 27953, 27961, 27967, 27983, 27997, 28001, 28019, 28027, 28031, 28051, 28057, 28069, 28081, 28087, 28097, 28099, 28109, 28111, 28123, 28151, 28163, 28181, 28183, 28201, 28211, 28219, 28229, 28277, 28279, 28283, 28289, 28297, 28307, 28309, 28319, 28349, 28351, 28387, 28393, 28403, 28409, 28411, 28429, 28433, 28439, 28447, 28463, 28477, 28493, 28499, 28513, 28517, 28537, 28541, 28547, 28549, 28559, 28571, 28573, 28579, 28591, 28597, 28603, 28607, 28619, 28621, 28627, 28631, 28643, 28649, 28657, 28661, 28663, 28669, 28687, 28697, 28703, 28711, 28723, 28729, 28751, 28753, 28759, 28771, 28789, 28793, 28807, 28813, 28817, 28837, 28843, 28859, 28867, 28871, 28879, 28901, 28909, 28921, 28927, 28933, 28949, 28961, 28979, 29009, 29017, 29021, 29023, 29027, 29033, 29059, 29063, 29077, 29101, 29123, 29129, 29131, 29137, 29147, 29153, 29167, 29173, 29179, 29191, 29201, 29207, 29209, 29221, 29231, 29243, 29251, 29269, 29287, 29297, 29303, 29311, 29327, 29333, 29339, 29347, 29363, 29383, 29387, 29389, 29399, 29401, 29411, 29423, 29429, 29437, 29443, 29453, 29473, 29483, 29501, 29527, 29531, 29537, 29567, 29569, 29573, 29581, 29587, 29599, 29611, 29629, 29633, 29641, 29663, 29669, 29671, 29683, 29717, 29723, 29741, 29753, 29759, 29761, 29789, 29803, 29819, 29833, 29837, 29851, 29863, 29867, 29873, 29879, 29881, 29917, 29921, 29927, 29947, 29959, 29983, 29989, 30011, 30013, 30029, 30047, 30059, 30071, 30089, 30091, 30097, 30103, 30109, 30113, 30119, 30133, 30137, 30139, 30161, 30169, 30181, 30187, 30197, 30203, 30211, 30223, 30241, 30253, 30259, 30269, 30271, 30293, 30307, 30313, 30319, 30323, 30341, 30347, 30367, 30389, 30391, 30403, 30427, 30431, 30449, 30467, 30469, 30491, 30493, 30497, 30509, 30517, 30529, 30539, 30553, 30557, 30559, 30577, 30593, 30631, 30637, 30643, 30649, 30661, 30671, 30677, 30689, 30697, 30703, 30707, 30713, 30727, 30757, 30763, 30773, 30781, 30803, 30809, 30817, 30829, 30839, 30841, 30851, 30853, 30859, 30869, 30871, 30881, 30893, 30911, 30931, 30937, 30941, 30949, 30971, 30977, 30983, 31013, 31019, 31033, 31039, 31051, 31063, 31069, 31079, 31081, 31091, 31121, 31123, 31139, 31147, 31151, 31153, 31159, 31177, 31181, 31183, 31189, 31193, 31219, 31223, 31231, 31237, 31247, 31249, 31253, 31259, 31267, 31271, 31277, 31307, 31319, 31321, 31327, 31333, 31337, 31357, 31379, 31387, 31391, 31393, 31397, 31469, 31477, 31481, 31489, 31511, 31513, 31517, 31531, 31541, 31543, 31547, 31567, 31573, 31583, 31601, 31607, 31627, 31643, 31649, 31657, 31663, 31667, 31687, 31699, 31721, 31723, 31727, 31729, 31741, 31751, 31769, 31771, 31793, 31799, 31817, 31847, 31849, 31859, 31873, 31883, 31891, 31907, 31957, 31963, 31973, 31981, 31991, 32003, 32009, 32027, 32029, 32051, 32057, 32059, 32063, 32069, 32077, 32083, 32089, 32099, 32117, 32119, 32141, 32143, 32159, 32173, 32183, 32189, 32191, 32203, 32213, 32233, 32237, 32251, 32257, 32261, 32297, 32299, 32303, 32309, 32321, 32323, 32327, 32341, 32353, 32359, 32363, 32369, 32371, 32377, 32381, 32401, 32411, 32413, 32423, 32429, 32441, 32443, 32467, 32479, 32491, 32497, 32503, 32507, 32531, 32533, 32537, 32561, 32563, 32569, 32573, 32579, 32587, 32603, 32609, 32611, 32621, 32633, 32647, 32653, 32687, 32693, 32707, 32713, 32717, 32719, 32749, 32771, 32779, 32783, 32789, 32797, 32801, 32803, 32831, 32833, 32839, 32843, 32869, 32887, 32909, 32911, 32917, 32933, 32939, 32941, 32957, 32969, 32971, 32983, 32987, 32993, 32999, 33013, 33023, 33029, 33037, 33049, 33053, 33071, 33073, 33083, 33091, 33107, 33113, 33119, 33149, 33151, 33161, 33179, 33181, 33191, 33199, 33203, 33211, 33223, 33247, 33287, 33289, 33301, 33311, 33317, 33329, 33331, 33343, 33347, 33349, 33353, 33359, 33377, 33391, 33403, 33409, 33413, 33427, 33457, 33461, 33469, 33479, 33487, 33493, 33503, 33521, 33529, 33533, 33547, 33563, 33569, 33577, 33581, 33587, 33589, 33599, 33601, 33613, 33617, 33619, 33623, 33629, 33637, 33641, 33647, 33679, 33703, 33713, 33721, 33739, 33749, 33751, 33757, 33767, 33769, 33773, 33791, 33797, 33809, 33811, 33827, 33829, 33851, 33857, 33863, 33871, 33889, 33893, 33911, 33923, 33931, 33937, 33941, 33961, 33967, 33997, 34019, 34031, 34033, 34039, 34057, 34061, 34123, 34127, 34129, 34141, 34147, 34157, 34159, 34171, 34183, 34211, 34213, 34217, 34231, 34253, 34259, 34261, 34267, 34273, 34283, 34297, 34301, 34303, 34313, 34319, 34327, 34337, 34351, 34361, 34367, 34369, 34381, 34403, 34421, 34429, 34439, 34457, 34469, 34471, 34483, 34487, 34499, 34501, 34511, 34513, 34519, 34537, 34543, 34549, 34583, 34589, 34591, 34603, 34607, 34613, 34631, 34649, 34651, 34667, 34673, 34679, 34687, 34693, 34703, 34721, 34729, 34739, 34747, 34757, 34759, 34763, 34781, 34807, 34819, 34841, 34843, 34847, 34849, 34871, 34877, 34883, 34897, 34913, 34919, 34939, 34949, 34961, 34963, 34981, 35023, 35027, 35051, 35053, 35059, 35069, 35081, 35083, 35089, 35099, 35107, 35111, 35117, 35129, 35141, 35149, 35153, 35159, 35171, 35201, 35221, 35227, 35251, 35257, 35267, 35279, 35281, 35291, 35311, 35317, 35323, 35327, 35339, 35353, 35363, 35381, 35393, 35401, 35407, 35419, 35423, 35437, 35447, 35449, 35461, 35491, 35507, 35509, 35521, 35527, 35531, 35533, 35537, 35543, 35569, 35573, 35591, 35593, 35597, 35603, 35617, 35671, 35677, 35729, 35731, 35747, 35753, 35759, 35771, 35797, 35801, 35803, 35809, 35831, 35837, 35839, 35851, 35863, 35869, 35879, 35897, 35899, 35911, 35923, 35933, 35951, 35963, 35969, 35977, 35983, 35993, 35999, 36007, 36011, 36013, 36017, 36037, 36061, 36067, 36073, 36083, 36097, 36107, 36109, 36131, 36137, 36151, 36161, 36187, 36191, 36209, 36217, 36229, 36241, 36251, 36263, 36269, 36277, 36293, 36299, 36307, 36313, 36319, 36341, 36343, 36353, 36373, 36383, 36389, 36433, 36451, 36457, 36467, 36469, 36473, 36479, 36493, 36497, 36523, 36527, 36529, 36541, 36551, 36559, 36563, 36571, 36583, 36587, 36599, 36607, 36629, 36637, 36643, 36653, 36671, 36677, 36683, 36691, 36697, 36709, 36713, 36721, 36739, 36749, 36761, 36767, 36779, 36781, 36787, 36791, 36793, 36809, 36821, 36833, 36847, 36857, 36871, 36877, 36887, 36899, 36901, 36913, 36919, 36923, 36929, 36931, 36943, 36947, 36973, 36979, 36997, 37003, 37013, 37019, 37021, 37039, 37049, 37057, 37061, 37087, 37097, 37117, 37123, 37139, 37159, 37171, 37181, 37189, 37199, 37201, 37217, 37223, 37243, 37253, 37273, 37277, 37307, 37309, 37313, 37321, 37337, 37339, 37357, 37361, 37363, 37369, 37379, 37397, 37409, 37423, 37441, 37447, 37463, 37483, 37489, 37493, 37501, 37507, 37511, 37517, 37529, 37537, 37547, 37549, 37561, 37567, 37571, 37573, 37579, 37589, 37591, 37607, 37619, 37633, 37643, 37649, 37657, 37663, 37691, 37693, 37699, 37717, 37747, 37781, 37783, 37799, 37811, 37813, 37831, 37847, 37853, 37861, 37871, 37879, 37889, 37897, 37907, 37951, 37957, 37963, 37967, 37987, 37991, 37993, 37997, 38011, 38039, 38047, 38053, 38069, 38083, 38113, 38119, 38149, 38153, 38167, 38177, 38183, 38189, 38197, 38201, 38219, 38231, 38237, 38239, 38261, 38273, 38281, 38287, 38299, 38303, 38317, 38321, 38327, 38329, 38333, 38351, 38371, 38377, 38393, 38431, 38447, 38449, 38453, 38459, 38461, 38501, 38543, 38557, 38561, 38567, 38569, 38593, 38603, 38609, 38611, 38629, 38639, 38651, 38653, 38669, 38671, 38677, 38693, 38699, 38707, 38711, 38713, 38723, 38729, 38737, 38747, 38749, 38767, 38783, 38791, 38803, 38821, 38833, 38839, 38851, 38861, 38867, 38873, 38891, 38903, 38917, 38921, 38923, 38933, 38953, 38959, 38971, 38977, 38993, 39019, 39023, 39041, 39043, 39047, 39079, 39089, 39097, 39103, 39107, 39113, 39119, 39133, 39139, 39157, 39161, 39163, 39181, 39191, 39199, 39209, 39217, 39227, 39229, 39233, 39239, 39241, 39251, 39293, 39301, 39313, 39317, 39323, 39341, 39343, 39359, 39367, 39371, 39373, 39383, 39397, 39409, 39419, 39439, 39443, 39451, 39461, 39499, 39503, 39509, 39511, 39521, 39541, 39551, 39563, 39569, 39581, 39607, 39619, 39623, 39631, 39659, 39667, 39671, 39679, 39703, 39709, 39719, 39727, 39733, 39749, 39761, 39769, 39779, 39791, 39799, 39821, 39827, 39829, 39839, 39841, 39847, 39857, 39863, 39869, 39877, 39883, 39887, 39901, 39929, 39937, 39953, 39971, 39979, 39983, 39989, 40009, 40013, 40031, 40037, 40039, 40063, 40087, 40093, 40099, 40111, 40123, 40127, 40129, 40151, 40153, 40163, 40169, 40177, 40189, 40193, 40213, 40231, 40237, 40241, 40253, 40277, 40283, 40289, 40343, 40351, 40357, 40361, 40387, 40423, 40427, 40429, 40433, 40459, 40471, 40483, 40487, 40493, 40499, 40507, 40519, 40529, 40531, 40543, 40559, 40577, 40583, 40591, 40597, 40609, 40627, 40637, 40639, 40693, 40697, 40699, 40709, 40739, 40751, 40759, 40763, 40771, 40787, 40801, 40813, 40819, 40823, 40829, 40841, 40847, 40849, 40853, 40867, 40879, 40883, 40897, 40903, 40927, 40933, 40939, 40949, 40961, 40973, 40993, 41011])
	for _ in range(test_cases):
		n = int(input())
		a = ['ERROR']
		for i in range(150):
			if sum(comps[i:i + n]) in primes:
				a = comps_s[i:i + n]
				break
		for i in range(n):
			os.write(1, (' '.join(a[i:] + a[:i]) + '\n').encode())
main()


for _ in range(int(input())):
	n = int(input())
	if n % 2 == 0:
		for i in range(1, n + 1):
			arr = [0] * n
			arr[n - i] = 1
			arr[i - 1] = 1
			for item in arr:
				print(item, end=' ')
			print('')
	else:
		for i in range(1, n + 1):
			arr = [0] * n
			arr[n - i] = 1
			arr[i - 1] = 1
			if i == n // 2:
				arr[n // 2] = 1
			if i == n // 2 + 1:
				arr[n // 2] = 1
				arr[n // 2 + 1] = 1
			for item in arr:
				print(item, end=' ')
			print('')


for _ in range(int(input())):
	n = int(input())
	p = 4231
	for i in range(n):
		row = [0] * n
		row[i] = p - 15
		row[(i + 1) % n] = 15
		print(*row)


t = int(input())
for tests in range(t):
	n = int(input())
	a = 401 - (n - 2) * 4 - 1
	square = [[a if c == r else 4 if c + 1 != r else 1 for c in range(n)] for r in range(n)]
	square[0][-1] = 1
	for row in square:
		print(' '.join(map(str, row)))


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	t = int(input())
	n = 10 ** 5
	primenumber = [2, 3]
	for i in range(5, n + 1, 2):
		flag = False
		x = int(i ** 0.5)
		for j in primenumber:
			if j > x:
				flag = True
				break
			if i % j == 0:
				flag = False
				break
		if flag:
			primenumber.append(i)
	s = set(primenumber)
	for i in range(t):
		nn = int(input())
		ans = [[0] * nn for i in range(nn)]
		x = 1
		for p in primenumber:
			if (p - 1) % (nn - 1) == 0 and nn - 1 != 1 and ((p - 1) // (nn - 1) not in s):
				x = p // (nn - 1)
				break
		for i in range(nn):
			for j in range(nn):
				if i == j:
					ans[i][j] = 1
				else:
					ans[i][j] = x
		for a in ans:
			print(' '.join([str(s) for s in a]))
main()


import math

def isPrime(n):
	if n == 2:
		return True
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return n > 1
primeSet = set((i for i in range(2, 10000) if isPrime(i)))
for _ in range(int(input())):
	n = int(input())
	arr = [[0 for i in range(n)] for j in range(n)]
	for i in range(n - 1):
		arr[i][-1] = 4
		arr[-1][i] = 4
	current_sum = 4 * (n - 1)
	last_ele = 0
	while current_sum + last_ele not in primeSet or last_ele in primeSet:
		last_ele += 1
	for i in range(n - 1):
		arr[i][i] = 1
	arr[-1][-1] = last_ele
	for row in arr:
		print(*row)


def prime(n):
	if n == 1 or n == 2 or n == 3 or (n == 5):
		return True
	for i in range(2, n // 2 + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	for j in range(n):
		for i in range(n):
			if i != j:
				print(1, end=' ')
			else:
				ans = n - 1
				if prime(n):
					print(1, end=' ')
				else:
					for i in range(2, 201):
						if prime(n - 1 + i) and (not prime(i)):
							print(i, end=' ')
							break
		print()


import sys
import bisect

def input():
	return sys.stdin.readline().rstrip()

def getMinPrimeDivisor(limit):
	arr = [0] * (limit + 1)
	for i in range(2, len(arr)):
		if arr[i] == 0:
			for j in range(i, len(arr), i):
				arr[j] = i
	return arr

def getAllPrimes(limit):
	minPrimeDivisor = getMinPrimeDivisor(limit)
	primes = []
	for i in range(2, len(minPrimeDivisor)):
		if minPrimeDivisor[i] == i:
			primes.append(i)
	return (primes, minPrimeDivisor)
t = int(input())
(primes, minPrimeDivisor) = getAllPrimes(100000)
for i in range(t):
	n = int(input())
	a_index = bisect.bisect_left(primes, n - 1)
	a = primes[a_index] - (n - 1)
	while minPrimeDivisor[a] == a:
		a_index += 1
		a = primes[a_index] - (n - 1)
	b_index = bisect.bisect_left(primes, a * (n - 1))
	b = primes[b_index] - a * (n - 1)
	while minPrimeDivisor[b] == b:
		b_index += 1
		b = primes[b_index] - a * (n - 1)
	ans1 = ['1'] * (n - 1)
	ans1.append(str(a))
	ans2 = [str(a)] * (n - 1)
	ans2.append(str(b))
	for j in range(1, n):
		print(' '.join(ans1))
	print(' '.join(ans2))


import math
from collections import Counter, defaultdict, deque

def isPrime(i):
	d = 2
	isPrime = True
	while d * d <= i:
		if i % d == 0:
			isPrime = False
			break
		d += 1
	return isPrime

def f(n):
	i = n + 2
	while True:
		if isPrime(i) and (not isPrime(i - n + 1)):
			break
		i += 1
	ans = []
	r = [1 for i in range(n)]
	for row in range(n):
		tr = r.copy()
		tr[row] = i - (n - 1)
		ans.append(tr)
	return ans
t = int(input())
result = []
for i in range(t):
	n = int(input())
	result.append(f(n))
for i in range(t):
	for r in range(len(result[i])):
		for c in range(len(result[i])):
			print('{}'.format(result[i][r][c]), end=' ')
		print()


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2, floor
from bisect import bisect_right, bisect_left, bisect
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 9 + 7
LOGN = 30
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = [[0 for j in range(n)] for i in range(n)]
	a[0][0] = 1
	for i in range(n):
		for j in range(n):
			if i == j or (i + 1) % n == j:
				a[i][j] = 1
	for i in a:
		print(*i)
	print()


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		z = deque([0] * (n - 2) + [1, 1])
		print(*z)
		for i in range(n - 1):
			k = z[-1]
			z.pop()
			z.appendleft(k)
			print(*z)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import io
import os
from collections import Counter, defaultdict, deque

def solve(N):
	grid = [[0 for i in range(N)] for j in range(N)]
	for i in range(N):
		grid[i][i] = 1
		grid[N - 1 - i][i] = 1
	if N % 2 == 1:
		m = N // 2
		for i in [m - 1, m, m + 1]:
			for j in [m - 1, m, m + 1]:
				grid[i][j] = 1
	return '\n'.join((' '.join(map(str, row)) for row in grid))
if False:
	for i in range(2, 10):
		print(i, solve(i))
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	ans = solve(N)
	print(ans)


for _ in range(int(input())):
	n = int(input())
	ans = [[-1] * n for i in range(n)]
	a = [0 for i in range(n - 2)]
	a.extend([1, 1])
	for z in range(n):
		ans[z] = a[z:n] + a[0:z]
	for z in range(n):
		print(*ans[z])


for _ in range(int(input())):
	n = int(input())
	arr = [1] + [4] + [0] * (n - 2)
	for i in range(n):
		print(*arr)
		arr = arr[-1:] + arr[:-1]


def isprime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def solve():
	n = int(input())
	ans = [[1] * n for i in range(n)]
	flag = True
	for p in range(10 ** 5 + 1):
		if flag == False:
			break
		if isprime(p) == False:
			(a, b) = (p + n - 1, n * p - p + 1)
			if isprime(a) and isprime(b):
				for i in range(n - 1):
					ans[0][i] = p
					ans[i + 1][-1] = p
				flag = False
	assert flag == False
	for i in range(n):
		print(*ans[i])
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


import sys
from array import array

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def get_primes(n: int):
	from itertools import chain
	from array import array
	primes = {2, 3}
	is_prime = array('b', (0, 0, 1, 1, 0, 1, 0)) + array('b', (1, 0, 0, 0, 1, 0)) * ((n - 1) // 6)
	for i in chain.from_iterable((range(5, n + 1, 6), range(7, n + 1, 6))):
		if is_prime[i]:
			primes.add(i)
			for j in range(i * 3, n + 1, i * 2):
				is_prime[j] = 0
	return (is_prime, primes)
(_, primes) = get_primes(10 ** 5 - 100)
t = int(input())
for _ in range(t):
	n = int(input())
	matrix = [[1] * n for _ in range(n)]
	p = 1
	for x in primes:
		if x - n + 1 > 1 and x - n + 1 not in primes:
			p = x
			break
	for i in range(n):
		matrix[i][i] = p - n + 1
		print(*matrix[i])


def isPrime(n):
	s = 0
	for i in range(2, n + 1):
		if n % i == 0:
			s += 1
	if s == 1:
		return True
	else:
		return False

def Print(a):
	for i in a:
		print(i, end=' ')
	print()

def find(n, x):
	if isPrime(n + x - 1) == True and isPrime(x) == False:
		return x
	else:
		return find(n, x + 1)
for _ in range(int(input())):
	n = int(input())
	if isPrime(n) == True:
		temp = [1] * n
		for __ in range(n):
			Print(temp)
	else:
		x = find(n, 2)
		for t in range(n):
			for k in range(n):
				if k == t:
					print(x, end=' ')
				else:
					print(1, end=' ')
			print()


T = int(input())
for t in range(T):
	n = int(input())
	A = [0 for i in range(n)]
	for i in range(n):
		B = A.copy()
		if i == 0:
			B[0] = 10
			B[1] = 1
		elif i == n - 1:
			B[n - 1] = 10
			B[n - 2] = 1
		else:
			B[i - 1] = 1
			B[i] = 9
			B[i + 1] = 1
		for j in range(n):
			print(B[j], end=' ')
		print()


import sys
input = sys.stdin.readline
num = [1, 9, 15, 21, 25, 27, 33, 35, 39, 42, 45, 49, 51, 55, 57, 63, 65, 69, 75, 77]

def isprime(n):
	for i in range(2, int(n ** 0.5 + 1)):
		if n % i == 0:
			return False
	return True

def main():
	n = int(input())
	ans = [4] * (n - 1)
	total = 4 * (n - 1)
	for i in num:
		if isprime(total + i):
			ans.append(i)
			break
	for _ in range(n):
		print(*ans)
		ans = [ans[-1]] + ans[:-1]
for _ in range(int(input())):
	main()


primes = [0]
composite = [0]

def erato(n):
	global primes, composite
	m = n * n
	indices = [True] * (m + 1)
	indices[1] = False
	for i in range(2, n + 1):
		if indices[i]:
			for j in range(i * i, m + 1, i):
				indices[j] = False
	c = p = 0
	for i in range(1, m + 1):
		if indices[i]:
			p += 1
		else:
			c += 1
	primes = [-1] * p
	composite = [-1] * c
	c = p = 0
	for i in range(1, m + 1):
		if indices[i]:
			primes[p] = i
			p += 1
		else:
			composite[c] = i
			c += 1
erato(100)

def get_square(n):
	row = [1] * n
	s = sum(row)
	while not s in primes:
		row[0] = composite[composite.index(row[0]) + 1]
		s = sum(row)
	mas = [row.copy()]
	magic = row[0]
	for i in range(n - 1):
		row[i] = 1
		row[i + 1] = magic
		mas.append(row.copy())
	return mas
t = int(input())
for i in range(t):
	square = get_square(int(input()))
	print('\n'.join([' '.join(map(str, row)) for row in square]))


t = int(input())

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
prime = SieveOfEratosthenes(2000)
for _ in range(t):
	n = int(input())
	diagonal = 1
	if not prime[n]:
		i = n - 1
		number = 1
		while not prime[i + number] or prime[number]:
			number += 1
		diagonal = number
	matrix = [[1 for j in range(n)] for k in range(n)]
	index = 0
	for row in range(n):
		matrix[row][index] = diagonal
		index += 1
	for elem in matrix:
		print(*elem)


for _ in range(int(input())):
	n = int(input())
	out = [[0] * n for i in range(n)]
	if n == 2:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		out[0][0] = 1
		out[0][1] = 1
		out[n - 1][n - 1] = 1
		out[n - 1][n - 2] = 1
		for i in range(1, n - 1):
			out[i][i - 1] = 1
			out[i][i + 1] = 1
		for i in range(n):
			for j in range(n):
				print(out[i][j], end=' ')
			print()


from math import *

def prime(n):
	if n == 1:
		return False
	if n == 2:
		return True
	if n % 2 == 0 and n > 2:
		return False
	for i in range(3, int(sqrt(n)) + 1, 2):
		if n % i == 0:
			return False
	return True

def nearest_prime(n):
	a = n
	while not prime(a):
		a += 1
	return a
t = int(input())
for i in range(t):
	n = int(input())
	m = nearest_prime(n)
	while prime(m - n + 1):
		m = nearest_prime(m + 1)
	a = [[1 for i in range(n)] for j in range(n)]
	if prime(n):
		for i in range(n):
			for j in range(n):
				print(a[i][j], end=' ')
				if j == n - 1:
					print(end='\n')
		continue
	for i in range(n):
		a[i][i] = m - n + 1
	for i in range(n):
		for j in range(n):
			print(a[i][j], end=' ')
			if j == n - 1:
				print(end='\n')


t = int(input())
while t:
	t -= 1
	n = int(input())
	temp = n
	prime = n
	f = 0
	while f == 0:
		n = prime
		f = 1
		for i in range(2, n):
			if prime % i == 0:
				prime += 1
				f = 0
				break
		if f:
			c = 0
			for i in range(2, prime + 1 - temp):
				if (prime + 1 - temp) % i == 0:
					c = 1
					break
			if c != 1:
				f = 0
				prime += 1
	n = temp
	for i in range(n):
		for j in range(n):
			if i == j:
				print(prime + 1 - n, end=' ')
			else:
				print(1, end=' ')
		print()


x = [1, 1, 1, 4, 1, 6, 1, 4, 9, 4, 1, 6, 1, 4, 9, 4, 1, 6, 1, 4, 9, 8, 1, 6, 35, 4, 15, 4, 1, 8, 1, 6, 9, 4, 9, 6, 1, 4, 9, 4, 1, 6, 1, 4, 9, 8, 1, 6, 25, 4, 9, 8, 1, 6, 25, 4, 15, 4, 1, 8, 1, 6, 9, 4, 9, 6, 1, 4, 15, 4, 1, 8, 1, 6, 9, 4, 21, 6, 1, 4, 9, 8, 1, 6, 25, 4, 15, 10, 1, 8, 49, 6, 9, 4, 9, 6, 1, 4, 9, 4]
y = [1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 21, 1, 25, 1, 1, 143, 1, 49, 1, 1, 1, 1, 25, 25, 25, 1, 1, 1, 1, 25, 1, 1, 25, 1, 1, 1, 49, 1, 1, 1, 1, 49, 1, 1, 35, 49, 9, 143, 1, 1, 15, 1, 1, 35, 25, 1, 49, 1, 1, 1, 1, 1, 1, 1, 1, 25, 49, 1, 1, 1, 1, 49, 25, 1, 1, 121, 9, 1, 49, 1, 15, 187, 1, 1, 1, 35, 1, 1, 1, 1, 1]
for _ in range(int(input())):
	n = int(input())
	for i in range(n - 1):
		for j in range(n - 1):
			print(1, end=' ')
		print(x[n - 1])
	for i in range(n - 1):
		print(x[n - 1], end=' ')
	print(y[n - 1])


t = int(input())
prime = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271]
while t:
	t -= 1
	n = int(input())
	arr = []
	for i in range(n):
		arr.append([1] * n)
	if n in prime:
		for i in arr:
			print(' '.join(map(str, i)))
	else:
		for p in prime:
			k = p - n + 1
			if k > 0 and k not in prime:
				break
		for i in range(n):
			arr[i][i] = k
		for i in arr:
			print(' '.join(map(str, i)))


for _ in range(int(input())):
	n = int(input())
	l = [[0 for _ in range(n)] for _ in range(n)]
	a = 1
	ans = []
	flag = 0
	if n & 1:
		flag = 1
	if n == 1:
		print(1)
	else:
		j = n - 1
		mid = n // 2
		for i in range(n):
			if i == mid and flag == 1:
				l[i][i] = 4
				l[i - 1][i] = 1
				l[i][i + 1] = 1
			else:
				l[i][i] = 1
				l[j][i] = 1
			j -= 1
	for i in l:
		print(*i)


for i1 in range(int(input())):
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		a[i][i] = 8
	if n % 2 != 0:
		for i in range(n - 1):
			a[i][i + 1] = 9
		a[n - 1][0] = 9
	else:
		for i in range(0, n - 1, 2):
			a[i][i + 1] = 9
			a[i + 1][i] = 9
	for i in range(n):
		for j in range(n):
			print(a[i][j], end=' ')
		print()


t = int(input())
for i in range(t):
	n = int(input())
	b = [[0] * n for i in range(n)]
	for i in range(n):
		b[i][i] = 1
		if i < n - 1:
			b[i][i + 1] = 1
		b[n - 1][0] = 1
	for i in range(n):
		print(*b[i])


def solve():
	n = int(input())
	a = []
	for i in range(n):
		if i < 2:
			a.append('1')
		else:
			a.append('0')
	for i in range(n):
		for x in a:
			print(x, end=' ')
		print()
		a.append(a[0])
		del a[0]

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


def isPrime(n):
	if n < 2:
		return False
	elif n == 2:
		return True
	elif n % 2 == 0:
		return False
	else:
		p = int(pow(n, 1 / 2))
		for i in range(3, p + 1, 2):
			if n % i == 0:
				return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	if isPrime(n):
		print(('1 ' * n + '\n') * n)
	else:
		x = 1
		while True:
			if isPrime(x) == False and isPrime(x + n - 1) == True:
				break
			x += 1
		x = str(x)
		for i in range(n):
			print('1 ' * i + x + ' ' + '1 ' * (n - i - 1))


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	board = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		board[i][i] = 4
		if i < n - 1:
			board[i][i + 1] = 1
		else:
			board[i][0] = 1
	for i in board:
		print(*i)


for __ in range(int(input())):
	n = int(input())
	ar = [0] * (n - 2)
	ar.append(1)
	ar.append(1)
	for i in range(n):
		x = i
		count = 0
		while count < n:
			count += 1
			print(ar[x % n], end=' ')
			x += 1
		print()


import sys, math
input = sys.stdin.readline
t = int(input())
for r in range(t):
	n = int(input())
	mat = []
	for i in range(n):
		temp = []
		for j in range(n):
			if i == j:
				temp.append(1)
			else:
				temp.append(0)
		mat.append(temp)
	for i in range(n):
		for j in range(n):
			if i == j:
				mat[(i + 1) % n][j] = 4
	for i in mat:
		for j in i:
			print(j, end=' ')
		print()


import math

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	for i in range(5, int(math.sqrt(n) + 1), 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def nextPrime(N):
	if N <= 1:
		return 2
	prime = N
	found = False
	while not found:
		prime = prime + 1
		if isPrime(prime) == True:
			found = True
	return prime

def solve(n):
	if isPrime(n):
		res = [1] * n
		for _ in range(n):
			print(*res)
		return
	row1n = [1] * (n - 1)
	nxtPrime = nextPrime(n - 1)
	res = nxtPrime - n + 1
	while isPrime(res):
		nxtPrime = nextPrime(nxtPrime)
		res = nxtPrime - n + 1
	row1n.append(res)
	rown = [res] * (n - 1)
	nxtPrime = nextPrime(sum(rown))
	res = nxtPrime - sum(rown)
	while isPrime(res):
		nxtPrime = nextPrime(nxtPrime)
		res = nxtPrime - sum(rown)
	rown.append(res)
	for _ in range(n - 1):
		print(*row1n)
	print(*rown)
	return
t = int(input())
for _ in range(t):
	n = int(input())
	solve(n)


from math import sqrt, ceil

def prime():
	prime = [True] * 10 ** 5
	for i in range(2, 10 ** 5):
		if prime[i]:
			for j in range(i * i, 10 ** 5, i):
				prime[j] = False
	s = set()
	for i in range(2, 10 ** 5):
		if prime[i]:
			s.add(i)
	return s

def main():
	p = prime()
	for _ in range(int(input())):
		n = int(input())
		m = -1
		for i in range(1, 10 ** 5):
			if i * (n - 1) + 1 in p and i not in p:
				m = i
				break
		l = [[m] * n for i in range(n)]
		for i in range(n):
			l[i][i] = 1
		for i in l:
			print(*i)
main()


def isPrime(n):
	for x in range(2, n // 2 + 1):
		if n % x == 0:
			return False
	return True
for t in range(int(input())):
	n = int(input())
	res = [[1 for x in range(n)] for y in range(n)]
	primes = set()
	for i in range(2, 101):
		if isPrime(i):
			primes.add(i)
	if isPrime(n):
		for row in res:
			print(*row)
	else:
		k = n - 1
		for i in range(2, 101):
			if i not in primes:
				if isPrime(i + k):
					t = i
					break
		for i in range(n):
			res[i][i] = t
		for row in res:
			print(*row)


import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		row = [0] * n
		arr.append(row)
	start = 0
	end = n - 1
	for i in range(n):
		arr[i][start] = 1
		arr[i][end] = 1
		start += 1
		end -= 1
	if n % 2 == 1:
		row_no = int(n / 2)
		arr[row_no][0] = 1
		arr[row_no][-1] = 1
		arr[0][row_no] = 1
		arr[-1][row_no] = 1
	for i in arr:
		print(*i)


import sys
sys.setrecursionlimit(311111)
ikimasu = sys.stdin.buffer.readline
ini = lambda : int(ins())
ina = lambda : list(map(int, ikimasu().split()))
ins = lambda : ikimasu().strip()

def is_prime(n):
	if n == 2 or n == 3:
		return True
	if n < 2 or n % 2 == 0:
		return False
	if n < 9:
		return True
	if n % 3 == 0:
		return False
	r = int(n ** 0.5)
	f = 5
	while f <= r:
		print('\t', f)
		if n % f == 0:
			return False
		if n % (f + 2) == 0:
			return False
		f += 6
	return True
t = ini()
for _ in range(t):
	n = ini()
	x = 0
	for i in range(n - 1):
		ans = []
		for j in range(n - 1):
			if j == x:
				ans.append(1)
				ans.append(1)
			else:
				ans.append(0)
		print(*ans)
		x += 1
	ans = [1]
	for i in range(n - 2):
		ans.append(0)
	ans.append(1)
	print(*ans)


def prime(x):
	if x < 2:
		return 0
	for a in range(2, int(x ** 0.5) + 1):
		if x % a == 0:
			return 0
	return 1
t = int(input())
for i in range(t):
	n = int(input())
	for j in range(1, 100000000):
		if prime(j) == 0 and prime(j + n - 1):
			for k in range(n):
				cur = ['1'] * n
				cur[k] = str(j)
				print(' '.join(cur))
			break


for _ in range(int(input())):
	n = int(input())
	f1 = 0
	for i in range(2, n, 1):
		if n % i == 0:
			f1 = 1
			break
	if f1 == 0:
		for i in range(n):
			for j in range(n):
				print(1, end=' ')
			print()
	else:
		num = [[0 for i in range(n)] for j in range(n)]
		num[n - 1][0] = 1
		num[n - 1][-1] = 1
		for i in range(n - 1):
			num[i][i] = 1
			num[i][i + 1] = 1
		for i in num:
			print(*i)


import math
MAX_N = 100000
arr = [0, 0, 1, 0, 0, 1, 0, 1, 0, 9, 4, 1, 0, 1, 0, 9, 4, 1, 0, 1, 0, 9, 8, 1, 0, 35, 4, 15, 4, 1, 0, 1, 0, 9, 4, 9, 6, 1, 0, 9, 4, 1, 0, 1, 0, 9, 8, 1, 0, 25, 4, 9, 8, 1, 0, 25, 4, 15, 4, 1, 0, 1, 0, 9, 4, 9, 6, 1, 0, 15, 4, 1, 0, 1, 0, 9, 4, 21, 6, 1, 0, 9, 8, 1, 0, 25, 4, 15, 10, 1, 0, 49, 6, 9, 4, 9, 6, 1, 0, 9, 4]

def solve(n):
	x = arr[n]
	for i in range(n):
		for j in range(n):
			if i == j:
				print(x, end='')
			else:
				print(1, end='')
			if j == n - 1:
				print()
			else:
				print(' ', end='')
tests = int(input())
for _ in range(tests):
	n = int(input())
	solve(n)


from math import *
from sys import *
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())

	def p(s):
		for i in range(2, int(sqrt(s)) + 1):
			if s % i == 0:
				return 0
		if s == 1:
			return 0
		return 1
	c = n - 1
	for i in range(n, 1001):
		if p(i) == 1:
			if p(i - c) == 0:
				f = 1
				a = i - c
				break
	m = []
	for i in range(n):
		m.append([])
		for j in range(n):
			m[i].append(1)
	for i in range(n):
		m[i][i] = a
	for i in range(n):
		print(*m[i])


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	res = [[1 for i in range(n)] for j in range(n)]
	if n % 2:
		for i in range(n):
			res[i][i] = 103 - n
			res[i][(i + 1) % n] = 0
		for i in range(n):
			print(*res[i])
	elif n != 100:
		for i in range(n):
			res[i][i] = 102 - n
		for i in range(n):
			print(*res[i])
	else:
		for i in range(n):
			res[i][i] = 104 - n
		for i in range(n):
			print(*res[i])


from collections import Counter
from collections import deque

def seive(n):
	arr = [True for i in range(n + 1)]
	(arr[0], arr[1], p, prime) = (False, False, 2, [])
	while p < n:
		if arr[p] == True:
			prime.append(p)
			for j in range(p * 2, n + 1, p):
				arr[j] = False
		p += 1
	return prime
prime = seive(10 ** 5 + 5)
d = Counter(prime)
t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(20):
		if d.get((n - 1) * i + 1) and d.get(i) == None:
			temp = [i] * (n - 1)
			temp.append(1)
			break
	(i, j) = (n - 2, n - 1)
	for _ in range(n):
		print(*temp)
		(temp[i], temp[j]) = (temp[j], temp[i])
		i -= 1
		j -= 1


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def lcm(a, b):
	return abs(a // gcd(a, b) * b)

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def comb(n, r):
	if n < r:
		return 0
	else:
		return fac[n] * (finv[r] * finv[n - r] % mod) % mod

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
from collections import defaultdict
sys.setrecursionlimit(300000)

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24

def yes():
	print('Yes')

def no():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')

def solve():
	n = N()
	ar = [[0] * n for _ in range(n)]
	for i in range(n):
		for j in range(n):
			if j - i == 1 or j - i == 0:
				ar[i][j] = 1
	ar[0][0] = 4
	ar[n - 1][n - 1] = 4
	ar[1][0] = 1
	ar[n - 1][n - 2] = 1
	for i in ar:
		print(*i)
testcase(int(inp()))


from sys import stdin, stdout
from collections import *
from math import *
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	n = inp()
	l = [[0 for i in range(n)] for j in range(n)]
	if n & 1:
		l[0][n // 2] = 1
		l[n // 2][0] = 1
		l[n // 2][n // 2] = 1
	for i in range(n // 2):
		l[i][i] = 1
		l[n - 1 - i][i] = 1
		l[i][n - i - 1] = 1
		l[n - i - 1][n - i - 1] = 1
	for i in l:
		print(*i)
for _ in range(inp()):
	solve()


from math import sqrt, floor

def isPrime(n):
	for i in range(2, floor(sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	if isPrime(n):
		mat = [[1 for i in range(n)] for j in range(n)]
		for i in mat:
			print(*i)
	else:
		mat = []
		if n % 2 == 0:
			k = 2
		elif n % 3 == 0:
			k = 3
		elif n % 5 == 0:
			k = 5
		elif n % 7 == 0:
			k = 7
		for i in range(n // k):
			l = [0 for i in range(n)]
			for j in range(k * i, k * i + k):
				l[j] = 1
			for m in range(k):
				mat.append(l)
		for i in mat:
			print(*i)