Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

Chef has an integer $N$ and he wants to generate a matrix $M$ with $N$ rows (numbered $1$ through $N$) and $N$ columns (numbered $1$ through $N$). He thinks that $M$ would be *delicious* if:
Each element of this matrix is an integer between $1$ and $N^{2}$ inclusive.
All the elements of the matrix are pairwise distinct.
For each square submatrix containing cells in rows $r$ through $r+a$ and in columns $c$ through $c+a$ (inclusive) for some valid integers $r$, $c$ and $a ≥ 0$:
- $M_{r,c} + M_{r+a,c+a}$ is even
- $M_{r,c+a} + M_{r+a,c}$ is even

Can you help Chef generate a delicious matrix? It can be proved that a solution always exists. If there are multiple solutions, you may find any one.

------  Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains a single integer $N$.

------  Output ------
For each test case, print $N$ lines describing a delicious matrix $M$. For each valid $i$, the $i$-th of these lines should contain $N$ space-separated integers $M_{i,1}, M_{i,2}, \ldots, M_{i,N}$.

------  Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{3}$
the sum of $N$ over all test cases does not exceed $10^{3}$

------  Subtasks ------
Subtask #1 (100 points): original constraints

----- Sample Input 1 ------ 
1

2
----- Sample Output 1 ------ 
1 2

4 3
----- explanation 1 ------ 
Example case 1: The matrix $M$ has five square submatrices. Four of them ($[1]$, $[2]$, $[4]$, $[3]$) have $a=0$, so they obviously satisfy all conditions. The last square submatrix is the whole matrix $M$, with $r=c=a=1$, and we can see that $M_{1,1} + M_{2,2} = 1 + 3 = 4$ and $M_{1,2} + M_{2,1} = 2 + 4 = 6$ are both even.

Source: codechef | Difficulty: EASY
Tags: Data structures


Solutions:
for _ in range(int(input())):
	n = int(input())
	a = []
	x = 1
	for i in range(n):
		m = []
		for j in range(n):
			m.append(x)
			x += 1
		if i % 2 == 1:
			a.append(m[::-1])
		else:
			a.append(m)
	for i in a:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	s = 0
	for i in range(1, n * n + 1, n):
		l = [i for i in range(i, n + i)]
		if s == 1:
			l.sort(reverse=True)
			print(*l)
		else:
			print(*l)
		if l[-1] % 2 == 0:
			s = 1
		else:
			s = 0


for i in range(int(input())):
	n = int(input())
	a = list()
	fill = 1
	for j in range(1, n + 1):
		b = list()
		for k in range(1, n + 1):
			b.append(fill)
			fill += 1
		if j % 2 == 0:
			a.append(b[::-1])
		else:
			a.append(b)
	for k in a:
		print(*k)


for _ in range(int(input())):
	n = int(input())
	matrix = []
	fill = 1
	for i in range(1, n + 1):
		temp = []
		for j in range(1, n + 1):
			temp.append(fill)
			fill += 1
		if i % 2 == 0:
			matrix.append(temp[::-1])
		else:
			matrix.append(temp)
	for i in matrix:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	ans = []
	k = 1
	for i in range(n):
		temp = []
		if i % 2 == 0:
			for j in range(n):
				temp.append(k)
				k += 1
		else:
			k += n - 1
			for j in range(n):
				temp.append(k)
				k -= 1
			k = temp[0] + 1
		ans.append(temp)
	for i in range(n):
		for j in range(n):
			print(ans[i][j], end=' ')
		print('')


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 1:
		for i in range(n):
			ans = ''
			for j in range(n):
				ans += str(j + i * n + 1) + ' '
			print(ans[:-1])
	else:
		for i in range(n):
			ans = ''
			if i % 2 == 0:
				for j in range(n):
					ans += str(j + i * n + 1) + ' '
			elif i % 2 == 1:
				j = n - 1
				while j >= 0:
					ans += str(j + i * n + 1) + ' '
					j -= 1
			print(ans[:-1])


t = int(input())
for i in range(t):
	n = int(input())
	a = 0
	for i in range(n):
		temp = []
		for j in range(n):
			if i % 2 == 0:
				a += 1
			temp.append(a)
			if i % 2 != 0:
				a -= 1
		print(*temp)
		a = a + n


t = int(input())
for i in range(t):
	n = int(input())
	a = 0
	for j in range(n):
		l = []
		for k in range(n):
			if j % 2 == 0:
				a += 1
			l.append(a)
			if j % 2 != 0:
				a -= 1
		print(*l)
		a = a + n


t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(n):
		if i % 2 != 0:
			a = i + 1
			a = a * n
		else:
			a = i * n + 1
		for j in range(n):
			if i % 2 == 0:
				print(a + j, end=' ')
			else:
				print(a - j, end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	a = [i for i in range(1, n * n + 1)]
	count = 0
	condition = n * n
	while condition > 0:
		print(*a[count:count + n])
		count += n
		condition -= n
		b = a[count:count + n]
		b.reverse()
		count += n
		print(*b)
		condition -= n


for _ in range(int(input())):
	n = int(input())
	num = 0
	while num < n:
		if not num % 2:
			for i in range(num * n, num * n + n):
				print(i + 1, end=' ')
		else:
			for i in range(num * n + n, num * n, -1):
				print(i, end=' ')
		print()
		num += 1


t = int(input().strip())
for x in range(t):
	n = int(input().strip())
	if n % 2 == 1:
		for i in range(n):
			for j in range(n):
				print(i * n + j + 1, end=' ')
			print()
	else:
		flag = 0
		for i in range(n):
			if flag == 0:
				flag = 1
				for j in range(n):
					print(i * n + j + 1, end=' ')
			else:
				flag = 0
				for j in range(n - 1, -1, -1):
					print(i * n + j + 1, end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	arr = [[-1 for i in range(n)] for j in range(n)]
	oddArr = []
	evenArr = []
	for i in range(1, n ** 2 + 1):
		if i % 2 == 1:
			oddArr.append(i)
		else:
			evenArr.append(i)
	for i in range(n):
		for j in range(n):
			if (i + j) % 2 == 0 and arr[i][j] == -1:
				arr[i][j] = oddArr.pop()
			else:
				arr[i][j] = evenArr.pop()
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())

	def row(y, f):
		x = y - n
		ans = [i for i in range(x + 1, y + 1)]
		if f:
			return ans
		else:
			return ans[::-1]
	m = [row(n * i, i % 2 == 1) for i in range(1, n + 1)]
	for r in m:
		print(*r)


for _ in range(int(input())):
	n = int(input())
	even = 1
	odd = 2
	l = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			if (i + j) % 2 == 0:
				l[i][j] = even
				even += 2
			if (i + j) % 2 == 1:
				l[i][j] = odd
				odd += 2
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	(c1, c2) = (1, 1)
	for i in range(n):
		for j in range(n):
			if (i + j) % 2 == 0:
				print(2 * c1 - 1, end=' ')
				c1 += 1
			else:
				print(c2 << 1, end=' ')
				c2 += 1
		print()


t = int(input())
for i in range(t):
	n = int(input())
	s = 1
	while s <= n:
		if s % 2 != 0:
			c = n * s - n + 1
			for j in range(n):
				print(c, end=' ')
				c += 1
			print()
		else:
			c = n * s
			for j in range(n):
				print(c, end=' ')
				c -= 1
			print()
		s += 1


def run():
	T = int(input())
	for _ in range(T):
		input_ = int(input())
		print(f(input_))

def f(N):
	ans = [[r * N + c + 1 for c in range(N)] for r in range(N)]
	if N % 2 == 0:
		for i in range(1, N, 2):
			ans[i] = ans[i][::-1]
	ans = [' '.join(map(str, row)) for row in ans]
	ans = '\n'.join(ans)
	return ans
run()


for _ in range(int(input())):
	n = int(input())
	lines = 1
	while lines <= n:
		if lines % 2 == 0:
			c = n * lines
			for i in range(n):
				print(c, end=' ')
				c -= 1
			print()
		else:
			c = n * lines - (n - 1)
			for i in range(n):
				print(c, end=' ')
				c += 1
			print()
		lines += 1


T = int(input(''))
for tc in range(T):
	N = int(input(''))
	a = []
	for i in range(N):
		l = []
		for j in range(N):
			l.append(0)
		a.append(l)
	k = 0
	l = 0
	m = N
	n = N
	j = 1
	while k < m and l < n:
		for i in range(l, n):
			a[k][i] = j
			j = j + 1
		k += 1
		for i in range(k, m):
			a[i][n - 1] = j
			j = j + 1
		n -= 1
		if k < m:
			for i in range(n - 1, l - 1, -1):
				a[m - 1][i] = j
				j = j + 1
			m -= 1
		if l < n:
			for i in range(m - 1, k - 1, -1):
				a[i][l] = j
				j = j + 1
			l += 1
	for i in range(N):
		for j in range(N):
			print(a[i][j], end=' ')
		print()


tc = int(input())
for _ in range(tc):
	n = int(input())
	res = []
	flag = False
	if n % 2 == 0:
		flag = True
	for i in range(n):
		x = list(range(i * n + 1, (i + 1) * n + 1))
		if flag and i % 2 == 1:
			x.reverse()
		res.append(x)
	for i in res:
		print(*i, sep=' ')


def matrix(n):
	bool = True
	if n % 2 == 1:
		for i in range(1, n ** 2 + 1, n):
			for k in range(n):
				print(i + k, end=' ')
			print()
	else:
		for i in range(1, n ** 2 + 1, n):
			if bool:
				for k in range(n):
					print(i + k, end=' ')
				print()
				bool = not bool
			else:
				for k in range(i + n - 1, i - 1, -1):
					print(k, end=' ')
				print()
				bool = not bool
for _ in range(int(input())):
	n = int(input())
	matrix(n)


def matr(n):
	if n % 2 != 0:
		k = 1
		for i in range(n):
			for j in range(n):
				print(k, end=' ')
				k += 1
			print()
	else:
		eve = [l + 1 for l in range(n * n) if l % 2 == 0]
		od = [l + 1 for l in range(n * n) if l % 2 != 0]
		p = 0
		t = 0
		for i in range(n):
			for j in range(n):
				if (i + j) % 2 == 0:
					print(eve[p], end=' ')
					p += 1
				else:
					print(od[t], end=' ')
					t += 1
			print()
for q in range(int(input())):
	matr(int(input()))


t = int(input())
for i in range(t):
	n = int(input())
	for i in range(n):
		t = i * n
		if i % 2 == 0:
			for k in range(1, n + 1):
				print(k + t, end=' ')
		if i % 2 != 0:
			for k in range(n, 0, -1):
				print(k + t, end=' ')
		print(sep='')


T = int(input())
for i in range(T):
	n = int(input())
	a = 1
	if n % 2 == 1:
		for row in range(n):
			for col in range(n):
				print(a, end=' ')
				a += 1
			print()
	else:
		for row in range(n):
			for col in range(n):
				if row % 2 == 1:
					if col % 2 == 0:
						print(a + 1, end=' ')
					else:
						print(a - 1, end=' ')
				else:
					print(a, end=' ')
				a += 1
			print()


for t in range(int(input())):
	n = int(input())
	a = 1
	if n % 2 == 1:
		for row in range(n):
			for col in range(n):
				print(a, end=' ')
				a += 1
			print()
	else:
		for row in range(n):
			for col in range(n):
				if row % 2 == 1:
					if col % 2 == 0:
						print(a + 1, end=' ')
					else:
						print(a - 1, end=' ')
				else:
					print(a, end=' ')
				a += 1
			print()


T = int(input())
while T > 0:
	N = int(input())

	def zeros(N):
		lst = []
		for i in range(N):
			inner_lst = []
			for j in range(N):
				inner_lst.append(0)
			lst.append(inner_lst)
		return lst
	M = zeros(N)
	accum = 1
	for i in range(N):
		if i % 2 == 0:
			for j in range(N):
				M[i][j] = accum
				accum += 1
		else:
			for j in sorted(list(range(N)), reverse=True):
				M[i][j] = accum
				accum += 1
	for i in range(N):
		for j in range(N):
			print(M[i][j], end=' ')
		print('')
	T = T - 1


import sys
import time
start_time = time.time()
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
input = sys.stdin.readline
T = int(input())
for _ in range(int(T)):
	N = int(input())
	for i in range(N):
		for j in range(N):
			if i % 2:
				print(i * N + j + 1, end=' ')
			else:
				print(i * N + N - j, end=' ')
		print()
end_time = time.time()
sys.stderr.write('Time: ' + str(end_time - start_time))


T = int(input())
for _ in range(T):
	N = int(input())
	for i in range(N):
		for j in range(N):
			if i % 2:
				print(i * N + j + 1, end=' ')
			else:
				print(i * N + N - j, end=' ')
		print()


z = int(input())
for i in range(0, z):
	b = int(input())
	l = []
	k = 1
	for i in range(0, b):
		a = []
		if i % 2 == 0:
			for j in range(0, b):
				a.append(k)
				k += 1
		else:
			for j in range(0, b):
				a.append(k)
				k += 1
			a.reverse()
		l.append(a)
	for i in range(0, b):
		for j in range(0, b):
			print(l[i][j], end=' ')
		print()


t = int(input())
for _ in range(t):
	n = int(input())
	boo = True
	for i in range(1, n * n + 1, n):
		if boo:
			print(*[x + i for x in range(0, n)])
		else:
			print(*[x + i for x in range(n - 1, -1, -1)])
		boo = not boo


t = int(input())
if 1 <= t <= 10:
	for i in range(t):
		n = int(input())
		mtx = []
		k = 1
		for i in range(n):
			a = []
			if i % 2 == 0:
				for j in range(n):
					a.append(k)
					k += 1
			else:
				for j in range(n):
					a.append(k)
					k += 1
				a.reverse()
			mtx.append(a)
		for i in range(n):
			for j in range(n):
				print(mtx[i][j], end=' ')
			print()


t = int(input())
for inh in range(0, t):
	n = int(input())
	if n % 2 == 1:
		(m, j) = (1, 1)
		while m <= n:
			k = m * n
			while j <= k:
				print(j, end=' ')
				j += 1
			print('')
			m += 1
	else:
		(m, j) = (1, 1)
		while m <= n:
			k = m * n
			a = []
			while j <= k:
				a.append(j)
				j += 1
			if m % 2 == 0:
				a.reverse()
			print(*a)
			m += 1


try:
	import numpy as np
	T = int(input())
	for _ in range(T):
		N = int(input())
		i = 1
		j = 1
		a = np.zeros((N + 1, N + 1))
		val = 1
		while i <= N:
			if i % 2 == 1:
				j = 1
				while j <= N:
					a[i][j] = val
					val += 1
					j += 1
			else:
				j = N
				while j >= 1:
					a[i][j] = val
					val += 1
					j -= 1
			i += 1
		for i in range(1, N + 1):
			for j in range(1, N + 1):
				print(int(a[i][j]), end=' ')
			print()
except:
	pass


t = int(input())
for _ in range(t):
	n = int(input())
	if n % 2 == 0:
		for i in range(1, n + 1):
			val = list(range((i - 1) * n + 1, i * n + 1))
			if i % 2 == 0:
				val.reverse()
			for v in val:
				print(v, end=' ')
			print()
	else:
		for i in range(1, n + 1):
			val = list(range((i - 1) * n + 1, i * n + 1))
			for v in val:
				print(v, end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	l = []
	k = 1
	for i in range(0, n):
		a = []
		if i % 2 == 0:
			for j in range(0, n):
				a.append(k)
				k += 1
		else:
			for x in range(0, n):
				a.append(k)
				k += 1
			a.reverse()
		l.append(a)
	for i in range(0, n):
		for j in range(0, n):
			print(l[i][j], end=' ')
		print()


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


try:
	for _ in range(int(input())):
		N = int(input())
		k = 1
		for i in range(N):
			L = [None] * N
			if i & 1 == 0:
				for x in range(N):
					L[x] = k
					k += 1
			else:
				for x in range(N):
					L[N - x - 1] = k
					k += 1
			print(*L)
except Exception as e:
	print(e.__class__)


for _ in range(int(input())):
	n = int(input())
	num = 1
	res = []
	for i in range(n):
		for j in range(n):
			if i % 2 == 0:
				print(num, end=' ')
				num += 1
			else:
				res.append(num)
				num += 1
		if sum(res) != 0:
			res.reverse()
			for k in range(n):
				print(res[k], end=' ')
		print()
		res = []


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


for _ in range(int(input())):
	n = int(input())
	dp = [[0 for x in range(n)] for y in range(n)]
	odd = 1
	even = 2
	for i in range(n):
		for j in range(n):
			if (i + j) % 2 == 0:
				dp[i][j] = odd
				odd = odd + 2
			else:
				dp[i][j] = even
				even = even + 2
	for po in dp:
		print(*po)


t = int(input())
while t:
	t -= 1
	n = int(input())
	dp = [[0 for x in range(n)] for y in range(n)]
	if n & 1 == 0:
		for i in range(len(dp)):
			for j in range(len(dp)):
				if i & 1 == 0:
					dp[i][j] = n * i + 1 + j
				else:
					dp[i][j] = n * (i + 1) - j
	else:
		for i in range(len(dp)):
			for j in range(len(dp)):
				dp[i][j] = n * i + 1 + j
	for k in dp:
		print(*k)


test_cases = int(input())
for i in range(test_cases):
	mat = int(input())
	num = []
	sum = 0
	elem = list((a for a in range(1, mat * mat + 1)))
	for j in range(1, mat + 1):
		if j % 2 == 1:
			num = elem[sum:sum + mat]
			sum += mat
			for t in num:
				print(t, end=' ')
		if j % 2 == 0:
			num = elem[sum:sum + mat]
			sum += mat
			num.reverse()
			for t in num:
				print(t, end=' ')
		print()


for _ in range(int(input())):
	size = int(input())
	n = 1
	for i in range(size):
		mat = []
		for j in range(size):
			mat.append(n)
			n += 1
		if i & 1:
			print(*mat[::-1])
		else:
			print(*mat)


t = int(input())
while t > 0:
	n = int(input())
	matrix = [[0] * n for i in range(n)]
	odd = 1
	even = 2
	for i in range(n):
		for j in range(n):
			if (i + j) % 2 == 0:
				matrix[i][j] = odd
				odd += 2
			else:
				matrix[i][j] = even
				even += 2
	for i in range(n):
		for j in range(n):
			print(matrix[i][j], end=' ')
		print()
	t -= 1


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


def evenm(n):
	a = []
	tn = 0
	start = 1
	for k in range(n):
		temp = []
		for t in range(n):
			temp.append(start)
			start += 1
		if tn == 0:
			a.append(temp)
			tn = 1
		else:
			a.append(temp[::-1])
			tn = 0
	return a
t = int(input())
for i in range(t):
	n = int(input())
	a = evenm(n)
	for j in a:
		print(*j)


t = int(input())
for kkkk in range(t):
	n = int(input())
	e = 2
	o = 1
	a = ['0' for j in range(n)]
	mat = []
	for i in range(0, n):
		for j in range(0, n):
			if (i + j) % 2 == 0:
				a[j] = o
				o += 2
			else:
				a[j] = e
				e += 2
		for j in range(n):
			print(a[j], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	if n & 1 == 1:
		for i in range(n):
			ans = range(n * i + 1, n * i + n + 1)
			print(' '.join(map(str, ans)))
	else:
		for i in range(n):
			if i & 1 == 0:
				ans = range(n * i + 1, n * i + n + 1)
			else:
				ans = [j + 1 if j & 1 == 1 else j - 1 for j in range(n * i + 1, n * i + n + 1)]
			print(' '.join(map(str, ans)))


for _ in range(int(input())):
	N = int(input())
	k = 1
	if N % 2 != 0:
		for i in range(1, N + 1):
			for j in range(1, N + 1):
				print(k, end=' ')
				k += 1
			print()
	else:
		for i in range(1, N + 1):
			if i % 2 != 0:
				for j in range(1, N + 1):
					print(k, end=' ')
					k += 1
				print()
			else:
				for j in range(1, int(N / 2) + 1):
					print(k + 1, k, end=' ')
					k += 2
				print()


for _ in range(int(input())):
	n = int(input())
	c = 1
	for i in range(1, n + 1):
		if i % 2 != 0:
			for j in range(c, c + n - 1):
				print(c, end=' ')
				c += 1
			print(c)
			c += 1
		else:
			s = ''
			for j in range(c, c + n):
				a = str(c) + ' ' + s
				s = a
				a = ''
				c += 1
			print(s)


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


T = int(input())
while T > 0:
	N = int(input())
	T = T - 1
	k = 1
	for i in range(N):
		L = []
		if i % 2 == 0:
			for x in range(N):
				L.append(k)
				k = k + 1
		else:
			for x in range(N):
				L.insert(0, k)
				k = k + 1
		print(*L)


t = int(input())
for i in range(t):
	n = int(input())
	i = j = 0
	ad = count = 1
	a = [[0] * n for i in range(n)]
	while i < n:
		while -1 < j < n:
			a[i][j] = count
			j = j + ad
			count += 1
		ad = ad * -1
		j = j + ad
		i += 1
	for i in a:
		for j in i:
			print(j, end=' ')
		print()


def eveFun(n):
	arr = []
	for i in range(n):
		if i % 2 == 0:
			arr.append(list(range(1 + n * i, (i + 1) * n + 1)))
		else:
			x = list(range(1 + n * i, (i + 1) * n + 1))
			arr.append(x[::-1])
	return res(arr, n)

def oddFun(n):
	arr = []
	for i in range(n):
		arr.append(list(range(1 + n * i, (i + 1) * n + 1)))
	return res(arr, n)

def res(arr, n):
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print('')
	print('\n')
try:
	t = int(input())
	for i in range(t):
		n = int(input())
		if n % 2 == 0:
			eveFun(n)
		else:
			oddFun(n)
except Exception:
	pass


t = int(input())
for _ in range(t):
	N = int(input())
	x = [0 for _ in range(N)]
	for i in range(N):
		for j in range(N):
			x[j] = N * i + j + 1
		if N % 2 == 0 and i % 2 == 1:
			for item in x[::-1]:
				print(item, end=' ')
		else:
			for item in x:
				print(item, end=' ')
		print()


t = int(input())
while t > 0:
	n = int(input())
	res = [[0] * n for i in range(n)]
	at = [[] for i in range(n + n)]
	for i in range(n):
		for j in range(n):
			at[i + j].append((i, j))
	ptr = 1
	for i in range(n + n):
		for pos in at[i]:
			if (pos[0] + pos[1]) % 2 == 0:
				res[pos[0]][pos[1]] = ptr
				ptr += 2
	ptr = 2
	for i in range(n + n):
		for pos in at[i]:
			if (pos[0] + pos[1]) % 2 != 0:
				res[pos[0]][pos[1]] = ptr
				ptr += 2
	for i in range(n):
		print(' '.join(map(str, res[i])))
	t -= 1


import sys
import math

def main(n):
	m = [[0] * n for i in range(n)]
	start = 1
	for i in range(n):
		m[0][i] = start
		start += 1
	for i in range(1, n):
		for j in range(n):
			m[i][j] = n + m[i - 1][j]
	if n % 2 == 0:
		for i in range(1, n):
			if i % 2:
				(p, q) = (0, n - 1)
				while p < q:
					(m[i][p], m[i][q]) = (m[i][q], m[i][p])
					p += 1
					q -= 1
	for e in m:
		print(*e)
	return
for _ in range(int(input())):
	n = int(input())
	main(n)


t = int(input())
while t:
	t -= 1
	n = int(input())
	matrix = []
	for i in range(n):
		matrix.append([i * n + j for j in range(1, n + 1)])
	if n & 1 == False:
		for i in range(1, n, 2):
			matrix[i] = matrix[i][::-1]
	for c in matrix:
		print(*c)