For a square matrix of integers of size $n \times n$, let's define its beauty as follows: for each pair of side-adjacent elements $x$ and $y$, write out the number $|x-y|$, and then find the number of different numbers among them.

For example, for the matrix $\begin{pmatrix} 1 & 3\\ 4 & 2 \end{pmatrix}$ the numbers we consider are $|1-3|=2$, $|1-4|=3$, $|3-2|=1$ and $|4-2|=2$; there are $3$ different numbers among them ($2$, $3$ and $1$), which means that its beauty is equal to $3$.

You are given an integer $n$. You have to find a matrix of size $n \times n$, where each integer from $1$ to $n^2$ occurs exactly once, such that its beauty is the maximum possible among all such matrices.


-----Input-----

The first line contains a single integer $t$ ($1 \le t \le 49$) – the number of test cases.

The first (and only) line of each test case contains a single integer $n$ ($2 \le n \le 50$).


-----Output-----

For each test case, print $n$ rows of $n$ integers — a matrix of integers of size $n \times n$, where each number from $1$ to $n^2$ occurs exactly once, such that its beauty is the maximum possible among all such matrices. If there are multiple answers, print any of them.


-----Examples-----

Input
2
2
3
Output
1 3
4 2
1 3 4
9 2 7
5 8 6


-----Note-----

None

Difficulty: EASY
Tags: Mathematics

Constructive algorithms

Starter code:

Solutions:
from itertools import chain
import itertools
import sys

def main():
	input = sys.stdin.readline
	test_cases = int(input())
	while test_cases:
		test_cases -= 1
		n = int(input())
		n_squared = n * n
		array = list(range(1, n_squared + 1))
		array_rev = array[::-1]
		array = list(chain(*zip(array_rev, array)))
		array = array[:n_squared]
		matrix = [array[i * n:(i + 1) * n] if i % 2 == 0 else array[(i + 1) * n - 1:i * n - 1:-1] for i in range(n)]
		print('\n'.join((' '.join(map(str, i)) for i in matrix)))
main()


t = int(input())
result = []
for i in range(t):
	n = int(input())
	m = n ** 2 // 2
	k = n ** 2
	tab = []
	for j in range(1, m + 1):
		tab += [str(k), str(j)]
		k -= 1
	if n % 2 == 1:
		tab.append(str(n ** 2 // 2 + 1))
	h = 0
	for j in range(0, len(tab) - n + 1, n):
		c = tab[j:j + n]
		if h == 0:
			result.append(' '.join(c))
		else:
			c.reverse()
			result.append(' '.join(c))
		h = 1 - h
for i in result:
	print(i)


def matrix_print(n=2):
	a = 1
	b = n ** 2
	c = True
	d = True
	answer = [[0 for i in range(n)] for j in range(n)]
	ps = n ** 2
	i = 0
	j = 0
	while i < n:
		if not c:
			answer[i][j] = str(a)
			a += 1
		else:
			answer[i][j] = str(b)
			b -= 1
		c = not c
		if d:
			j = j + 1
			if j == n:
				j = j - 1
				i = i + 1
				d = not d
		else:
			j = j - 1
			if j == -1:
				j = j + 1
				i = i + 1
				d = not d
	for row in answer:
		print(' '.join(row))

def solve(inps):
	for inp in inps:
		matrix_print(inp)
lst = []
tcs = int(input())
for tc in range(tcs):
	lst.append(int(input()))
solve(lst)


def ii(num=False):
	i = input().split()
	if num:
		return int(i[0])
	try:
		return list(map(int, i))
	except Exception:
		return i

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

def get_a():
	b = n ** 2
	i = 1
	yield b
	while True:
		yield i
		yield (b - i)
		i += 1
for _ in range(ii(1)):
	n = ii(1)
	gen = get_a()
	for i in range(n):
		k = []
		for j in range(n):
			k.append(str(next(gen)))
		if i % 2 == 1:
			k.reverse()
		print(' '.join(k))
	pass


import time
import sys

def print_matr(n, print, end_line):
	arr = [n * n]
	stop = n * n // 2 + 1
	for i in range(1, stop):
		arr.append(i)
		arr.append(n * n - i)
	if n % 2 == 0:
		arr.pop()
	matr = [[] for _ in range(n)]
	for i in range(n):
		for j in range(n):
			matr[i].append(arr[i * n + j])
		if i % 2 == 1:
			matr[i].reverse()
	for i in range(n):
		print(' '.join(map(str, matr[i])) + end_line)

def main(input, print, end_line):
	t = int(input())
	for _ in range(t):
		n = int(input())
		print_matr(n, print, end_line)
main(sys.stdin.readline, sys.stdout.write, '\n')


from math import ceil
for _ in range(int(input())):
	n = int(input())
	m = n * n
	matrix = []
	for i in range(ceil(m / 2)):
		matrix.append(m - i)
		if len(matrix) < m:
			matrix.append(i + 1)
	for y in range(n):
		for x in range(n):
			if not y % 2:
				matrix.append(matrix.pop(0))
			else:
				matrix.append(matrix.pop(n - 1 - x))
	for i in range(0, m, n):
		print(' '.join(list(map(str, matrix[i:i + n]))))


for _ in range(int(input())):
	n = int(input())
	m = n ** 2
	i = 1
	for x in range(n):
		if x % 2 == 0:
			for j in range(n):
				if j % 2 == 0:
					print(m - i + 1, end=' ')
				else:
					print(i, end=' ')
					i += 1
			print()
		else:
			str = ''
			for j in range(n):
				if j % 2 == 0 and n % 2 == 0:
					str = '{}'.format(m - i + 1) + ' ' + str
				elif j % 2 == 1 and n % 2 == 0:
					str = '{}'.format(i) + ' ' + str
					i += 1
				elif j % 2 == 0 and n % 2 == 1:
					str = '{}'.format(i) + ' ' + str
					i += 1
				else:
					str = '{}'.format(m - i + 1) + ' ' + str
			print(str)


def solve(n):
	res = []
	k1 = n ** 2
	k2 = 1
	f = 1
	for i in range(n):
		t = []
		if i % 2 == 0:
			for j in range(n):
				if f:
					t.append(k1)
					k1 -= 1
					f = 0
				else:
					t.append(k2)
					k2 += 1
					f = 1
		else:
			for j in range(n):
				if f:
					t.insert(0, k1)
					k1 -= 1
					f = 0
				else:
					t.insert(0, k2)
					k2 += 1
					f = 1
		res.append(t)
	return res
for _ in range(int(input())):
	n = int(input())
	res = solve(n)
	for i in res:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	arr = [[0] * n for _ in range(n)]
	p = False
	a = 1
	b = n ** 2
	for i in range(n):
		x = 0
		y = i
		while x < n and y >= 0:
			if p:
				arr[x][y] = a
				a += 1
			else:
				arr[x][y] = b
				b -= 1
			x += 1
			y -= 1
		p = not p
	for i in range(1, n):
		x = i
		y = n - 1
		while x < n and y >= 0:
			if p:
				arr[x][y] = a
				a += 1
			else:
				arr[x][y] = b
				b -= 1
			x += 1
			y -= 1
		p = not p
	for i in range(n):
		print(*arr[i])


for t in range(int(input())):
	n = int(input())
	arr = []
	i = 1
	j = n * n
	while i <= j:
		if i == j:
			arr.append(i)
		else:
			arr.append(j)
			arr.append(i)
		i += 1
		j -= 1
	ans = []
	count = 0
	for i in range(1, n * n, n):
		if count % 2 == 0:
			print(*arr[i - 1:i + n - 1][:])
		else:
			print(*arr[i - 1:i + n - 1][::-1])
		count += 1


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

	def increasing(s):
		while True:
			yield s
			s += 1

	def decreasing(s):
		while True:
			yield s
			s -= 1
	inc = increasing(1)
	dec = decreasing(n ** 2)
	for i in range(n):
		row = [_ for j in range(n)]
		for j in range(n - 1, -1, -1) if i & 1 else range(n):
			if i + j & 1:
				row[j] = next(inc)
			else:
				row[j] = next(dec)
		print(*row)


t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	ans = []
	for i in range(n):
		row = []
		if i % 2 == 0:
			for j in range(n):
				if j % 2 == 0:
					row.append(n * n - n * (i // 2) - j // 2)
				else:
					row.append(1 + n * (i // 2) + j // 2)
		else:
			for j in range(n):
				if j % 2 == 0:
					row.append(n * (i // 2 + 1) - j // 2)
				else:
					row.append(n * n - n + 1 - n * (i // 2) + j // 2)
		ans.append(row)
	for r in ans:
		print(*r)


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