Consider the following $4 \times 4$ pattern:
1  2  4  7
3  5  8 11
6  9 12 14
10 13 15 16

You are given an integer $N$. Print the $N \times N$ pattern of the same kind (containing integers $1$ through $N^2$).

-----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; each of them should contain $N$ space-separated integers.

-----Constraints-----
- $1 \le T \le 10$
- $1 \le N \le 100$

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

-----Example Input-----
1
4

-----Example Output-----
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16

-----Explanation-----

Source: codechef | Difficulty: EASY
Tags: String algorithms

Implementation

Complete search


Solutions:
for i in range(int(input())):
	n = int(input())
	arr = [[0] * n for j in range(n)]
	c = 1
	for j in range(n):
		for k in range(j + 1):
			arr[k][j - k] = c
			c += 1
	for j in range(1, n):
		for k in range(n - j):
			arr[j + k][n - k - 1] = c
			c += 1
	for j in arr:
		print(*j)


try:
	for i in range(int(input())):
		n = int(input())
		a = [[0 for i in range(n)] for j in range(n)]
		p = 1
		for k in range(n):
			j = k
			i = 0
			while j >= 0:
				a[i][j] = p
				p = p + 1
				i = i + 1
				j = j - 1
		for k in range(1, n, 1):
			i = k
			j = n - 1
			f = k
			while j >= f:
				a[i][j] = p
				p = p + 1
				i = i + 1
				j = j - 1
		for i in range(n):
			for j in range(n):
				print(a[i][j], end=' ')
			print('\n')
except:
	pass


import numpy as np
for _ in range(int(input())):
	n = int(input())
	oxx = np.zeros((n, n))
	(i, pp, c) = (0, 0, 0)
	while i <= n - 1 and pp <= n - 1:
		tempi = i
		temppp = pp
		while tempi <= pp:
			c += 1
			oxx[tempi][temppp] = int(c)
			temppp -= 1
			tempi += 1
		if pp != n - 1:
			i = 0
			pp += 1
		else:
			i += 1
			pp = n - 1
	for i in range(n):
		print(*list(map(int, oxx[i])))


import math as m

def ncr(N, R):
	return m.factorial(N) // (m.factorial(R) * m.factorial(N - R))

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

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

def solve():
	n = int(input())
	arr = []
	for i in range(n):
		for j in range(n):
			arr += [[i + j, i, j]]
	arr = sorted(arr, key=lambda x: x[0])
	ans = [[0 for i in range(n)] for j in range(n)]
	count = 1
	for v in arr:
		ans[v[1]][v[2]] = count
		count += 1
	for j in ans:
		print(*j)
for _ in range(getInt()):
	solve()


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


try:
	for u in range(int(input())):
		n = int(input())
		li = []
		lid = []
		a = 1
		t = 0
		l = 0
		for i in range(n * n):
			t += 1
			lid.append(i + 1)
			if a == t:
				if l != -1:
					a += 1
					t = 0
				else:
					a -= 1
					t = 0
				li.append([int(k) for k in lid])
				lid.clear()
			if a == n:
				l = -1
		li2 = li.copy()
		for i in li:
			if len(li2) != 0:
				a = 0
				li3 = li2.copy()
				for j in li2:
					if a == n:
						break
					a += 1
					if len(j) != 0:
						print(j[0], end=' ')
						j.remove(j[0])
				li2.clear()
				li2 = li3.copy()
				if len(j) != 0:
					print()
				else:
					break
				li2.remove(li2[0])
		print()
except:
	pass


T = int(input())
for i in range(T):
	n = int(input())
	a = []
	b = [[0 for j in range(n)] for i in range(n)]
	for i in range(n):
		for j in range(n):
			a.append([i + j, i, j])
	a.sort()
	j = 1
	for x in a:
		(r, c) = (x[1], x[2])
		b[r][c] = j
		j += 1
	for i in range(n):
		for j in range(n):
			print(b[i][j], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	tm = 1
	mat = [[0 for j in range(n)] for i in range(n)]
	for i in range(n):
		k = 0
		l = i
		while l > -1:
			mat[k][l] = tm
			tm += 1
			k += 1
			l -= 1
	for i in range(1, n):
		k = i
		l = n - 1
		while k < n:
			mat[k][l] = tm
			tm += 1
			k += 1
			l -= 1
	for i in mat:
		print(*i)


for test_ in range(int(input())):
	n = int(input())
	op = [[0 for i in range(n)] for i in range(n)]
	i = 0
	j = 0
	num = 1
	while i < n and j < n:
		temp1 = i
		temp2 = j
		while temp1 <= j:
			op[temp1][temp2] = num
			num += 1
			temp1 += 1
			temp2 -= 1
		if j != n - 1:
			i = 0
			j += 1
		else:
			i += 1
			j = n - 1
	for i in op:
		print(*i)


for _ in range(int(input())):
	x = int(input())
	t = 0
	for i in range(1, x + 1):
		t = t + i
		m = [t]
		n = t
		for l in range(i, i + x - 1):
			if l < x:
				a = n + l
			else:
				a = n + (2 * x - l - 1)
			m.append(a)
			n = a
		print(*m)


for _ in range(int(input())):
	n = int(input())
	a = []
	b = [[0 for i in range(n)] for j in range(n)]
	for i in range(0, n):
		for j in range(0, n):
			a.append([i + j, i, j])
	a.sort()
	j = 1
	for i in a:
		(r, c) = (i[1], i[2])
		b[r][c] = j
		j += 1
	for i in range(n):
		for k in range(n):
			print(b[i][k], end=' ')
		print()


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


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	for i in range(n):
		k = list(range(n))
		l.append(k)
	c = 1
	for i in range(n):
		k = i
		for j in range(i + 1):
			l[j][k] = c
			c = c + 1
			k = k - 1
	for i in range(1, n):
		k = i
		for j in range(n - 1, i - 1, -1):
			l[k][j] = c
			c = c + 1
			k = k + 1
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	a = [[0 for j in range(n)] for i in range(n)]
	ans = 1
	temp = 0
	for i in range(n):
		for j in range(n):
			if i == 0:
				a[i][j] = ans + temp
				ans = a[i][j]
				temp += 1
			if j != n - 1 and i != 0:
				a[i][j] = a[i - 1][j + 1] + 1
			if j == n - 1 and i != 0:
				a[i][j] = a[i - 1][j] + temp
				temp -= 1
	for k in a:
		print(*k)


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


t = int(input())

def PrintL(l):
	for i in range(len(l)):
		for j in range(len(l)):
			print(l[i][j], end=' ')
		print()
for k in range(t):
	n = int(input())
	L = [[0] * n] * n
	L1 = []
	for i in range(len(L)):
		L2 = []
		for j in range(len(L)):
			L2.append(i + j)
		L1.append(L2)
	p = 1
	c = 0
	while p <= n ** 2:
		for i in range(len(L1)):
			for j in range(len(L1)):
				if i + j == c:
					L1[i][j] = p
					p += 1
		c += 1
	PrintL(L1)


tests = int(input())
for test in range(tests):
	n = int(input())
	a = [[0 for i in range(n)] for i in range(n)]
	count = 1
	for k in range(n):
		i = 0
		for j in range(k, -1, -1):
			a[i][j] = count
			count += 1
			i += 1
	for k in range(1, n):
		i = k
		j = n - 1
		for i in range(k, n):
			a[i][j] = count
			count += 1
			j -= 1
	my_str = ''
	for i in range(n):
		for j in range(n):
			my_str += str(a[i][j])
			my_str += ' '
		my_str += '\n'
	print(my_str)


for _ in range(int(input())):
	n = int(input())
	(x, y) = (1, n - 1)
	for i in range(n):
		a = x
		b = y
		for j in range(n):
			if i + j > b:
				a = b + a
				b -= 1
			else:
				a = i + j + a
			print(a, end=' ')
		print()
		x = i + 1 + x


for _ in range(int(input())):
	N = int(input())
	x = 0
	length = len(str(N * N))
	for i in range(1, N + 1):
		x += i
		X = ' ' * (length - len(str(x))) + str(x)
		row = [X]
		last = x
		for j in range(i, i + N - 1):
			a = last + j if j < N else last + (2 * N - j - 1)
			A = ' ' * (length - len(str(a))) + str(a)
			row.append(A)
			last = a
		print(*row)


t = int(input())
for _ in range(t):
	n = int(input())
	l = []
	ans = [[0 for i in range(n)] for j in range(n)]
	for i in range(n):
		for j in range(n):
			l.append((i + j, i, j))
	l.sort()
	x = 1
	for i in l:
		(r, c) = (i[1], i[2])
		ans[r][c] = x
		x += 1
	for i in range(n):
		for j in range(n):
			print(ans[i][j], end=' ')
		print()


t = int(input())
for x in range(t):
	n = int(input())
	c = 0
	for i in range(1, n + 1):
		c += i
		l = [c]
		m = c
		for o in range(i, n + i - 1):
			if o < n:
				b = m + o
			else:
				b = m + (2 * n - o - 1)
			l.append(b)
			m = b
		print(*l)


T = int(input())
for hp in range(T):
	n = int(input())
	compare = []
	for i in range(1, n):
		compare.append(i)
	for i in range(n - 1, 0, -1):
		compare.append(i)
	compare.append(0)
	digit = 1
	count = 2
	final = []
	for i in range(0, n):
		temp = digit
		for j in range(i, i + n):
			final.append(digit)
			digit = digit + compare[j]
		digit = temp + count
		count = count + 1
	index = 0
	for i in range(0, n):
		for j in range(0, n):
			print(final[index], end=' ')
			index = index + 1
		print()


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


def fill_val(i, j, n, mat):
	nonlocal val
	while j >= 0 and i < n:
		mat[i][j] = val
		val += 1
		i += 1
		j -= 1
for _ in range(int(input())):
	n = int(input())
	arr = [[0 for c in range(n)] for r in range(n)]
	val = 1
	for i in range(n):
		for j in range(n):
			if arr[i][j] == 0:
				fill_val(i, j, n, arr)
	for i in range(n):
		for j in range(n):
			print(arr[i][j], end=' ')
		print()


t = int(input())
while t > 0:
	n = int(input())
	l = []
	for i in range(n):
		l1 = []
		for j in range(n):
			l1.append(0)
		l.append(l1)
	c = 1
	for j in range(n):
		k = 0
		for i in range(j, -1, -1):
			l[k][i] = c
			c += 1
			k += 1
	for i in range(1, n):
		m = i
		for j in range(n - i):
			l[m][n - j - 1] = c
			c += 1
			m += 1
	for p in l:
		print(*p)
	t -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	arr = []
	new_arr = [[0 for x in range(n)] for y in range(n)]
	for i in range(n):
		for j in range(n):
			arr.append([i + j, i, j])
	b = sorted(arr, key=lambda arr: arr[0])
	counter = 1
	for items in b:
		r = items[1]
		c = items[2]
		new_arr[r][c] = counter
		counter = counter + 1
	for q in range(n):
		for p in range(n):
			print(new_arr[q][p], end=' ')
		print('\n')


t = int(input())
for z in range(0, t):
	N = int(input())
	li = [[i for i in range(0, N)] for j in range(0, N)]
	c = 1
	for j1 in range(0, N):
		i = 0
		j = j1
		while j != -1:
			li[i][j] = c
			i += 1
			j -= 1
			c += 1
	for i1 in range(1, N):
		j = N - 1
		i = i1
		while i != N:
			li[i][j] = c
			i += 1
			j -= 1
			c += 1
	for i in range(0, N):
		for j in range(0, N):
			print(li[i][j], end=' ')
		print()


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


for i in range(int(input())):
	n = int(input())
	k = [[0 for j in range(n)] for i in range(n)]
	k[0][0] = 1
	r = 0
	c = 1
	p = 1
	for i in range(2, n * n + 1):
		if r == n - 1:
			k[r][c] = i
			r = c + 1
			c = n - 1
		elif r == p:
			k[r][c] = i
			p += 1
			r = c
			c = p
		else:
			k[r][c] = i
			r += 1
			c -= 1
	for i in k:
		print(' '.join(map(str, i)))


for _ in range(int(input())):
	n = int(input())
	x = 1
	y = n
	a = [[0 for i in range(n)] for j in range(n)]
	for (indx1, i) in enumerate(a):
		if indx1 == 0:
			for (indx2, j) in enumerate(i):
				if indx2 == 0:
					a[indx1][indx2] = 1
				else:
					a[indx1][indx2] = a[indx1][indx2 - 1] + x
					x += 1
		else:
			for (indx2, j) in enumerate(i):
				if indx2 == n - 1:
					a[indx1][indx2] = a[indx1 - 1][indx2] + y
					y -= 1
				else:
					a[indx1][indx2] = a[indx1 - 1][indx2 + 1] + 1
	for i in a:
		for j in i:
			print(j, end=' ')
		print()


t = int(input())
for i in range(t):
	n = int(input())
	a = [[0 for i in range(n)] for j in range(n)]
	b = 1
	for k in range(n):
		j = k
		i = 0
		while j >= 0:
			a[i][j] = b
			b = b + 1
			i = i + 1
			j = j - 1
	for k in range(1, n, 1):
		i = k
		j = n - 1
		c = k
		while j >= c:
			a[i][j] = b
			b = b + 1
			i = i + 1
			j = j - 1
	for i in range(0, n, 1):
		for j in range(0, n, 1):
			print(a[i][j], end=' ')
		print('\n', end='')


test_cases = input()
for _ in range(int(test_cases)):
	arr_leng = int(input())
	op_arr = {}
	count = 2
	value = 1
	reverse_length = arr_leng - 1
	for i in range(1, arr_leng + 1):
		for j in range(1, arr_leng + 1):
			op_arr[i + j] = op_arr.get(i + j) or []
			for input_val in range(1, i + j):
				val = i + j
				op_val = op_arr.get(i + j)
				if len(op_val) < i + j - 1 < arr_leng + 1:
					op_val = op_arr.get(i + j)
					op_val.append(value)
					op_arr[i + j] = op_val
					value += 1
				elif i + j > arr_leng + 1 and len(op_val) <= arr_leng + arr_leng - i - j:
					op_val = op_arr.get(i + j)
					op_val.append(value)
					op_arr[i + j] = op_val
					value += 1
	for i in range(1, arr_leng + 1):
		for j in range(1, arr_leng + 1):
			print(op_arr[i + j].pop(0), end=' ')
		print('\n')


for _ in range(int(input())):
	n = int(input())
	matrix = [[0 for _ in range(n)] for _ in range(n)]
	cnt = 1
	matrix[0][0] = 1
	matrix[n - 1][n - 1] = n * n
	for i in range(1, n):
		r = 0
		c = i
		while r < n and c >= 0:
			cnt += 1
			matrix[r][c] = cnt
			r += 1
			c -= 1
	for i in range(1, n):
		r = i
		c = n - 1
		while r < n and c >= 0:
			cnt += 1
			matrix[r][c] = cnt
			r += 1
			c -= 1
	for i in range(n):
		for j in range(n - 1):
			print(matrix[i][j], end=' ')
		print(matrix[i][n - 1])


t = int(input())
for i in range(t):
	n = int(input())
	a = 1
	b = n - 1
	for i in range(n):
		first = a
		second = b
		for j in range(n):
			if i + j > second:
				first += second
				second -= 1
			else:
				first += i + j
			print(first, end=' ')
		print('')
		a += i + 1


try:
	for _ in range(int(input())):
		N = int(input())
		X = list(range(1, N + 1)) + list(range(N - 1, 0, -1))
		A = [[] for i in range(N)]
		count = 1
		start = 0
		for x in range(len(X)):
			if x > N - 1:
				start += 1
			for i in range(start, start + X[x]):
				A[i].append(count)
				count += 1
		for a in A:
			print(*a)
except:
	pass


for _ in range(int(input())):
	n = int(input())
	l = [[0 for i in range(n)] for j in range(n)]
	c = 1
	cur = 0
	while 1:
		i = 0
		j = cur
		while j >= 0:
			l[i][j] = c
			c += 1
			j -= 1
			i += 1
		cur += 1
		if cur == n:
			break
	cur = n - 1
	curi = 1
	while 1:
		i = curi
		j = cur
		if curi == n:
			break
		while i != n:
			l[i][j] = c
			c += 1
			i += 1
			j -= 1
		curi += 1
	for i in range(n):
		for j in range(n):
			print(l[i][j], end=' ')
		print()


def printPattern(n):
	k = 0
	for i in range(1, n + 1):
		k += i
		j = 1
		l = k
		inc = i
		while (i + j - 1) % n:
			print(l, end=' ')
			l = l + inc
			inc += 1
			j += 1
		while j <= n:
			print(l, end=' ')
			inc -= 1
			l = l + inc
			j += 1
		print()
for tc in range(int(input())):
	n = int(input())
	printPattern(n)


for _ in range(int(input())):
	n = int(input())
	k = 0
	for i in range(1, n + 1):
		k += i
		j = 1
		l = k
		inc = i
		while (i + j - 1) % n:
			print(l, end=' ')
			l = l + inc
			inc += 1
			j += 1
		while j <= n:
			print(l, end=' ')
			inc -= 1
			l = l + inc
			j += 1
		print()


for i in range(int(input())):
	n = int(input())
	list1 = [[] for x in range(n)]
	ini = 0
	ind = 0
	k = 1
	while k <= n * n:
		if ind == -1 or ini == n:
			ini = 0
		if len(list1[ini]) == n:
			ini = ini + 1
			ind = n
		else:
			list1[ini].append(k)
			ind = len(list1[ini]) - 1
			ind -= 1
			ini = ini + 1
			k += 1
	for s in list1:
		for a in s:
			print(a, end=' ')
		print('')


for _ in range(int(input())):
	n = int(input())
	ans = [[0] * n] * n
	c = 1
	for i in range(n):
		for j in range(i + 1):
			d = ans[j].copy()
			d[i - j] = c
			ans[j] = d.copy()
			c += 1
	for i in range(1, n):
		for j in range(n - i):
			d = ans[i + j].copy()
			d[n - j - 1] = c
			ans[i + j] = d.copy()
			c += 1
	for i in ans:
		print(*i)


for i in range(int(input())):
	t = int(input())
	n = 0
	for i in range(1, t + 1):
		n = n + i
		x = [n]
		y = n
		for j in range(i, t + i - 1):
			if j < t:
				z = y + j
			else:
				z = y + (2 * t - j - 1)
			x.append(z)
			y = z
		print(*x)


n_sum = [0]
for n in range(1, 100):
	n_sum.append(n * (n + 1) // 2)
tests = int(input())
for i in range(tests):
	n = int(input())
	ans_arr = []
	for j in range(n):
		temp_arr = []
		for k in range(n):
			if j + k < n:
				add = n_sum[j + k]
				temp_arr.append(add + j + 1)
			else:
				diff = j + k - n + 1
				temp_arr.append(temp_arr[k - 1] + n - diff)
		ans_arr.append(temp_arr)
	for x in ans_arr:
		print(*x)


try:
	T = int(input())
	P = ''
	for i in range(T):
		N = int(input())
		A = [[0 for i in range(N)] for j in range(N)]
		a = 1
		K = 0
		for x in range(2 * N):
			if K < N:
				i = 0
				j = K
				for y in range(K + 1):
					if i + j == K and i >= 0 and (j >= 0):
						A[i][j] = a
						i = i + 1
						j = j - 1
						a = a + 1
				K = K + 1
			else:
				J = N - 1
				I = K - J
				for y in range(2 * N - K - 1):
					A[I][J] = a
					I = I + 1
					J = J - 1
					a = a + 1
				K = K + 1
		for a in range(0, N):
			for b in range(0, N):
				if b != N - 1:
					P = P + str(A[a][b]) + ' '
				else:
					P = P + str(A[a][N - 1]) + '\n'
	print(P)
except:
	pass


for tc in range(int(input())):
	n = int(input())
	diag = list(range(1, n)) + list(range(n - 1, 0, -1))
	slide = list(range(n))
	triangle = [int(j * (j + 1) / 2) for j in range(1, n + 1)]
	if n == 1:
		print(1)
	else:
		for k in range(n):
			num = triangle[k]
			for i in range(slide[k], n + slide[k]):
				print(num, end=' ')
				if i < n + slide[k - 1]:
					num = num + diag[i]
			print()


for T in range(0, int(input())):
	if T <= 10:
		N = int(input())
		if N <= 100:
			L1 = [[0 for i in range(0, N)] for j in range(0, N)]
			c = 1
			for i in range(0, N):
				a = 0
				for j in range(i, -1, -1):
					L1[a][j] = c
					c += 1
					a += 1
			for x in range(1, N):
				k = x
				for y in range(N - x):
					L1[k][N - y - 1] = c
					c += 1
					k += 1
			for mat in L1:
				print(*mat)


t = int(input())
for i in range(t):
	n = int(input())
	k = 1
	q = [None] * n
	for i in range(n):
		k += i
		q[i] = k
	print(*q)
	for i in range(n - 1):
		for j in range(n - 1):
			q[j] = q[j + 1] + 1
		q[n - 1] = q[n - 2] + (n - i - 1)
		print(*q)


test_cases = int(input())
for cases in range(test_cases):
	a = int(input())
	b = a
	starting_point = 0
	for i in range(a):
		sum_counter = 0
		initial_increment = i + 1
		starting_point += initial_increment
		value = starting_point
		print(value, end=' ')
		for j in range(b - 1):
			actual_sum = i + j + 1
			if actual_sum < b:
				sum = actual_sum
			if actual_sum >= b and sum_counter < 2:
				sum = b - 1
				sum_counter += 1
			if sum_counter >= 2:
				sum -= 1
			value = value + sum
			print(value, end=' ')
		print()


for _ in range(int(input())):
	n = int(input())
	li = []
	for i in range(n):
		li.append([0] * n)
	li[0][0] = 1
	c = 2
	for i in range(1, n):
		for j in range(i + 1):
			li[j][i - j] = c
			c += 1
	for i in range(1, n):
		k = n - 1
		for j in range(i, n):
			li[j][k] = c
			k -= 1
			c += 1
	for i in li:
		print(*i)


for _ in range(int(input())):
	n = int(input())
	vals = []
	for i in range(n):
		vals.append([])
		for j in range(n):
			vals[i].append([])
	value = 1
	x = 0
	y = 0
	slope = 1
	while value <= n ** 2:
		vals[y][x] = value
		value += 1
		x -= 1
		y += 1
		if x < 0 or y == n:
			slope += 1
			x = min(slope - 1, n - 1)
			if slope < n:
				y = 0
			else:
				y = slope % n
	for y in range(n):
		for x in range(n):
			print(vals[y][x], end=' ')
		print()


for _ in range(int(input())):
	N = int(input())
	(p, q) = (1, N - 1)
	for i in range(N):
		t = p
		l = q
		for j in range(N):
			if i + j > l:
				t += l
				l -= 1
			else:
				t += i + j
			print(t, end=' ')
		print()
		p += i + 1