We will consider the numbers $a$ and $b$ as adjacent if they differ by exactly one, that is, $|a-b|=1$.

We will consider cells of a square matrix $n \times n$ as adjacent if they have a common side, that is, for cell $(r, c)$ cells $(r, c-1)$, $(r, c+1)$, $(r-1, c)$ and $(r+1, c)$ are adjacent to it.

For a given number $n$, construct a square matrix $n \times n$ such that:

Each integer from $1$ to $n^2$ occurs in this matrix exactly once;

If $(r_1, c_1)$ and $(r_2, c_2)$ are adjacent cells, then the numbers written in them must not be adjacent.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 100$). Then $t$ test cases follow.

Each test case is characterized by one integer $n$ ($1 \le n \le 100$).


-----Output-----

For each test case, output:

-1, if the required matrix does not exist;

the required matrix, otherwise (any such matrix if many of them exist).

The matrix should be outputted as $n$ lines, where each line contains $n$ integers.


-----Examples-----

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


-----Note-----

None

Difficulty: EASY
Tags: Constructive algorithms

Starter code:

Solutions:
T = int(input())
for i in range(T):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	elif n == 3:
		print('2 9 7')
		print('4 6 3')
		print('1 8 5')
	else:
		l = list()
		for i in range(n ** 2 // 2 * 2):
			l.append(i + 1)
			if n % 2 == 1:
				l.append(n ** 2 // 2 + 2 + i)
			else:
				l.append(int(n ** 2 / 2 + 1 + i))
		if n % 2 == 1:
			l.append(n ** 2)
		(g, a) = (list(), list())
		for i in range(n):
			for j in range(n):
				a.append(l[n * i + j])
			g.append(a)
			a = []
		for i in g:
			for j in range(len(i)):
				print(i[j], end=' ')
			print()


rn = lambda : int(input())
rl = lambda : [x for x in input().split()]
rln = lambda : [int(x) for x in input().split()]
rlfn = lambda : [float(x) for x in input().split()]

def solve():
	return []
tests = rn()
for tc in range(1, tests + 1):
	n = rn()
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		l = [i for i in range(1, n ** 2 + 1) if i % 2 == 0]
		l += [i for i in range(1, n ** 2 + 1) if i % 2 == 1]
		for i in range(n):
			s = ' '.join([str(l[i * n + j]) for j in range(n)])
			print(s)


for test in range(int(input())):
	n = int(input())
	ans = []
	ans2 = []
	if n == 2:
		print(-1)
	else:
		for i in range(1, n * n + 1):
			if i % 2 == 0:
				ans.append(i)
			else:
				ans2.append(i)
		ans2.extend(ans)
		for i in range(n):
			print(*ans2[i * n:(i + 1) * n])


def give(ind, n):
	if n % 2 == 1 or ind <= n * n:
		return ind % (n * n) + int(ind == n * n) * n * n
	else:
		return ind + 1 - n * n
t = int(input())
for _ in range(t):
	n = int(input())
	if n == 2:
		print(-1)
	elif n == 1:
		print(1)
	else:
		grid = [[0] * n for i in range(n)]
		val = -1
		for i in range(n):
			for j in range(n):
				val += 2
				grid[i][j] = give(val, n)
			print(*grid[i])


import sys
input = sys.stdin.readline
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		a = [[-1 for i in range(n)] for j in range(n)]
		x = 1
		for i in range(n):
			a[i][i] = x
			x += 1
		j = 1
		for i in range(n - 1):
			o = 0
			k = j
			while o < n and k < n:
				a[o][k] = x
				x += 1
				o += 1
				k += 1
			o = j
			k = 0
			while o < n and k < n:
				a[o][k] = x
				x += 1
				o += 1
				k += 1
			j += 1
		for i in range(n):
			print(*a[i])


def solve(n):
	if n == 1:
		print(1)
		return
	if n == 2:
		print(-1)
		return
	l = []
	for i in range(n):
		l.append([-1] * n)
	i = 1
	first = False
	for j in range(n):
		rr = j
		rc = 0
		lr = 0
		lc = j
		while rr < n and rc < n:
			l[rr][rc] = i
			rr += 1
			rc += 1
			i += 1
		while first and lr < n and (lc < n):
			l[lr][lc] = i
			lr += 1
			lc += 1
			i += 1
		first = True
	for i in l:
		print(*i)
for _ in range(int(input())):
	solve(int(input()))


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


for _ in range(int(input())):
	n = int(input())
	ans = []
	if n == 2:
		print(-1)
	else:
		odd = [i for i in range(1, n * n + 1, 2)]
		even = [i for i in range(2, n * n + 1, 2)]
		mid = n * n // 2
		c = 0
		for i in range(n):
			for j in range(n):
				if c < mid:
					print(even[0], end=' ')
					even.pop(0)
				else:
					print(odd[0], end=' ')
					odd.pop(0)
				c += 1
			print()


mas = list(map(int, input().split()))
t = mas[0]
for j in range(t):
	mas = list(map(int, input().split()))
	n = mas[0]
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		lst = [[0 for k in range(n)] for i in range(n)]
		ii = 0
		jj = 0
		ind = 1
		for k in range(n):
			ii = 0
			jj = k
			while jj > -1 and ii < n:
				lst[ii][jj] = str(ind)
				ind += 1
				jj -= 1
				ii += 1
		for k in range(1, n):
			ii = k
			jj = n - 1
			while jj > -1 and ii < n:
				lst[ii][jj] = str(ind)
				ind += 1
				jj -= 1
				ii += 1
		lst[0][0] = str(n * n)
		lst[n - 1][n - 1] = '1'
		for i in range(n):
			print(' '.join(lst[i]))


from sys import stdin as inp
from functools import reduce
for t in range(int(inp.readline())):
	n = int(inp.readline())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		(even, odd) = ([], [])
		for i in range(1, n ** 2 + 1):
			if i % 2 == 0:
				even.append(i)
			else:
				odd.append(i)
		for i in range(n):
			for j in range(n):
				if odd:
					c = odd.pop()
				else:
					c = even.pop()
				print(c, end=' ')
			print()


def printt(s):
	for i in s:
		print(' '.join([str(x) for x in i]))

def task():
	n = int(input())
	if n == 1:
		print(1)
		return
	elif n == 2:
		print(-1)
		return
	elif n == 3:
		printt([[1, 8, 6], [7, 5, 3], [4, 2, 9]])
		return
	s = [[j * n + i + 1 for i in range(n)] for j in range(n)]
	for i in range(n // 2):
		for j in range(n):
			if i + j & 1:
				s[i][j] ^= s[n - i - 1][j]
				s[n - i - 1][j] ^= s[i][j]
				s[i][j] ^= s[n - i - 1][j]
	if n & 1:
		for i in range(n // 2 - 2):
			if i & 1:
				s[n // 2][i] ^= s[n // 2][n - i - 1]
				s[n // 2][n - i - 1] ^= s[n // 2][i]
				s[n // 2][i] ^= s[n // 2][n - i - 1]
		m = s[n // 2][n // 2]
		(s[n // 2][n // 2 - 2], s[n // 2][n // 2 - 1], s[n // 2][n // 2 + 1], s[n // 2][n // 2 + 2]) = (m - 1, m + 2, m - 2, m + 1)
	printt(s)
for i in range(int(input())):
	task()


def main():
	n = int(input())
	if n == 1:
		print(1)
		return
	if n == 2:
		print(-1)
		return
	nn = n * n
	a = list(range(nn))
	i = 1
	step = n + 1
	way = 0
	while i <= nn:
		a[way] = i
		i += 1
		way += step
		way = way % nn
	tmp = []
	for y in range(n):
		tmp.append(' '.join((str(_) for _ in a[y * n:y * n + n])))
	print('\n'.join(tmp))
	return
	for (i, x) in enumerate(a, 1):
		print('{:5d}'.format(x), end=' ')
		if i % n == 0:
			print('')
t = int(input())
for i in range(t):
	main()


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	elif n == 3:
		print('2 9 7', '4 6 3', '1 8 5', sep='\n')
	elif n == 4:
		print('1 3 5 7', '9 16 14 12', '11 8 2 4', '6 13 10 15', sep='\n')
	else:
		x = 1
		l = []
		for i in range(n):
			l.append([])
			for j in range(0, n, 2):
				l[i].append(0)
				l[i].append(0)
				l[i][j] = x
				x += 1
			for j in range(1, n, 2):
				l[i][j] = x
				x += 1
		for i in range(n):
			for j in range(n):
				print(l[i][j], end=' ')
			print()


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		li = [(i + (n ** 2 + 1) * i - (1 if i > n ** 2 // 2 and n % 2 == 0 else 0)) % n ** 2 if (i + (n ** 2 + 1) * i - (1 if i > n ** 2 // 2 and n % 2 == 0 else 0)) % n ** 2 != 0 else n ** 2 for i in range(1, n ** 2 + 1)]
		for i in range(n):
			print(*li[n * i:n * i + n])


import math
from collections import Counter
from itertools import permutations, combinations
import fractions

def solve():
	n = int(input())
	grid = []
	cur = []
	for i in range(1, n * n + 1):
		if i % n == 0:
			cur.append(i)
			grid.append(cur)
			cur = []
		else:
			cur.append(i)
	if n == 1:
		print(1)
		return
	if n == 2:
		print(-1)
		return
	rot = 1
	for i in range(1, n - 1):
		col = []
		for j in range(n):
			col.append(grid[j][i])
		col = col[rot:] + col[:rot]
		rot += 1
		for k in range(n):
			grid[k][i] = col[k]
	for i in range(n):
		for j in range(n):
			print(grid[i][j], end=' ')
		print()
t = int(input())
for _ in range(t):
	solve()


t = int(input())
for inh in range(0, t):
	n = int(input())
	if n == 2:
		print(-1)
	elif n == 1:
		print(1)
	else:
		k = n ** 2
		(i, j) = (1, 0)
		while i <= k:
			if j == n:
				print('')
				j = 0
			print(i, end=' ')
			i += 2
			j += 1
		i = 2
		while i <= k:
			if j == n:
				print('')
				j = 0
			print(i, end=' ')
			i += 2
			j += 1
		print('')


for _ in range(int(input())):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	a = [[0 for i in range(n)] for j in range(n)]
	c = -1
	for i in range(n):
		for j in range(n):
			c += 2
			if c > n * n:
				c = 2
			a[i][j] = c
		print(*a[i])


t = int(input())
for _ in range(t):
	n = int(input())
	mat = [[0 for i in range(n)] for j in range(n)]
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	elif n == 3:
		lis = [[1, 8, 6], [7, 5, 3], [4, 2, 9]]
		for i in range(n):
			for j in range(n):
				print(lis[i][j], end=' ')
			print()
	else:
		mat[0][0] = 1
		mat[0][n - 1] = 3
		mat[n - 1][0] = 2
		mat[n - 1][n - 1] = 4
		cnt = 5
		for i in range(n):
			for j in range(n):
				k = 0
				while j - k >= 0 and i + k < n and (mat[i + k][j - k] == 0):
					mat[i + k][j - k] = cnt
					cnt += 1
					k += 1
		for i in range(n):
			for j in range(n):
				print(mat[i][j], end=' ')
			print()


from os import path
import sys, time, collections as c, math, pprint as p, itertools as it, operator as op, bisect as bs
(maxx, localsys, mod) = (float('inf'), 0, int(1000000000.0 + 7))
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = [[0 for _ in range(n)] for i in range(n)]
	x = 1
	l = 1
	for i in range(n):
		for j in range(n):
			arr[i][j] = x
			x += 2
			if x > n * n:
				l += 1
				x = l
		if x > n * n:
			l += 1
			x = l
	ok = True
	for i in range(n):
		for j in range(n - 1):
			if abs(arr[i][j] - arr[i][j + 1]) <= 1:
				ok = False
			if i + 1 < n and abs(arr[i][j] - arr[i + 1][j]) <= 1:
				ok = False
			if i - 1 >= 0 and abs(arr[i][j] - arr[i - 1][j]) <= 1:
				ok = False
			if j - 1 >= 0 and abs(arr[i][j] - arr[i][j - 1]) <= 1:
				ok = False
			if not ok:
				break
		if not ok:
			break
	if ok:
		for i in arr:
			print(*i)
	else:
		print(-1)


def solve(n):
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		mat = [0] * n
		for i in range(n):
			mat[i] = [0] * n
		num = 1
		for i in range(0, n):
			if i % 2 == 0:
				for j in range(0, n, 2):
					mat[i][j] = num
					num += 1
			else:
				for j in range(1, n, 2):
					mat[i][j] = num
					num += 1
		for i in range(0, n):
			if i % 2 == 1:
				for j in range(0, n, 2):
					mat[i][j] = num
					num += 1
			else:
				for j in range(1, n, 2):
					mat[i][j] = num
					num += 1
		for i in range(n):
			for j in range(n):
				print(mat[i][j], end=' ')
			print()
for _ in range(int(input())):
	n = int(input())
	solve(n)


from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	mat = [[-1 for i in range(n)] for j in range(n)]
	k = 1
	for i in range(n):
		for j in range(n):
			mat[i][j] = k
			k += 2
			if k > n * n:
				k = 2
	if n == 2:
		print(-1)
		continue
	for i in range(n):
		print(*mat[i])


def notYourMatrix(n):
	if n == 2:
		return [[-1]]
	l = [[0 for i in range(n)] for j in range(n)]
	r = 0
	s = 0
	p = 0
	dir = 1
	for i in range(1, n * n + 1):
		l[r][s] = i
		if r == n - 1 or s == n - 1:
			if dir == 0:
				r = p
				s = 0
				dir = 1
			else:
				p += 1
				r = 0
				s = p
				dir = 0
		else:
			r += 1
			s += 1
	return l
i = int(input())
l = []
for j in range(i):
	x = int(input())
	ans = notYourMatrix(x)
	l = l + ans
for a in l:
	print(*a)


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


from sys import stdin, stdout
input = stdin.readline

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

def ii():
	return int(input())

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

def ins():
	return input()[:-1]
for _ in range(ii()):
	n = ii()
	if n == 1:
		print(1)
		continue
	if n == 2:
		print(-1)
		continue
	lis = []
	for i in range(n):
		lis.append([0] * n)
	c = flag = 0
	r = count = 1
	while True:
		if r == n and c == n:
			break
		if flag == 0:
			j = c
			i = 0
			flag = 1
			c += 1
		else:
			i = r
			j = 0
			flag = 0
			r += 1
		while i < n and j < n:
			lis[i][j] = count
			count += 1
			i += 1
			j += 1
	for i in lis:
		print(*i)


for _ in range(int(input())):
	a = int(input())
	if a == 1:
		print(1)
	elif a == 2:
		print(-1)
	else:
		l = []
		for i in range(1, a ** 2 + 1, 1):
			l.append(i)
		c = []
		for i in range(0, len(l), 2):
			c.append(l[i])
		for j in range(1, len(l), 2):
			c.append(l[j])
		q = 0
		for i in range(a):
			for j in range(a):
				print(c[q], end=' ')
				q = q + 1
			print()


for test in range(int(input())):
	n = int(input())
	if n == 2:
		print(-1)
	else:
		m = [[0 for i in range(n)] for j in range(n)]
		n2 = n * n // 2
		num1 = iter(range(1, n2 + 1))
		num2 = iter(range(n2 + 1, n * n + 1))
		flag = 0
		for i in range(n):
			for j in range(n):
				if flag == 1:
					m[i][j] = next(num1)
					flag = 0
				else:
					m[i][j] = next(num2)
					flag = 1
		for i in m:
			for j in i:
				print(j, end=' ')
			print()


import math
import collections
import itertools
import bisect

def is_good(matrix):
	n = len(matrix)
	for y in range(n):
		for x in range(n):
			if y + 1 < n and abs(matrix[y][x] - matrix[y + 1][x]) == 1:
				return False
			if x + 1 < n and abs(matrix[y][x] - matrix[y][x + 1]) == 1:
				return False
	return True
for tt in range(1, int(input()) + 1):
	n = int(input())
	matrix = [[0] * n for _ in range(n)]
	i = 1
	for y in range(n):
		for x in range(n):
			matrix[y][x] = i
			i += 2
			if i > n * n:
				i = 2
	possible = is_good(matrix)
	if possible:
		for row in matrix:
			print(' '.join(map(str, row)))
	else:
		print('-1')


import math, sys
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
from itertools import permutations
MOD = int(1000000000.0) + 7

def solve():
	n = int(input())
	if n == 2:
		print(-1)
		return
	ans = [[0 for i in range(n)] for j in range(n)]
	cnt = 1
	start = 0
	for i in range(n):
		for j in range(start, n, 2):
			ans[i][j] = cnt
			cnt += 1
		start ^= 1
	start = 1
	for i in range(n):
		for j in range(start, n, 2):
			ans[i][j] = cnt
			cnt += 1
		start ^= 1
	for row in ans:
		print(*row)
t = 1
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	matrix = [[0] * n for i in range(n)]
	jump = 0
	count = 1
	if n == 2:
		print(-1)
		continue
	for i in range(2 * n):
		if i < n:
			if i % 2 == 0:
				jump = 0
			else:
				jump = 1
		else:
			if n % 2 == 0:
				jump = 1
			else:
				jump = 0
			if i % 2 == 1:
				jump = (jump + 1) % 2
			else:
				jump = (jump + 0) % 2
		while jump < n:
			matrix[i % n][jump] = count
			jump += 2
			count += 1
	for i in matrix:
		for j in i:
			print(j, end=' ')
		print()


from sys import stdin, stdout
stdin.readline

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

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil, gcd, sqrt, factorial, log2, floor
for _ in range(it()):
	n = it()
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		v = [[0 for _ in range(n)] for _ in range(n)]
		x = []
		for i in range(n ** 2):
			if not i + 1 & 1:
				x.append(i + 1)
		x.reverse()
		y = []
		for i in range(n ** 2):
			if i + 1 & 1:
				y.append(i + 1)
		y.reverse()
		x = x + y
		for i in range(n):
			for j in range(n):
				v[i][j] = x.pop()
		for i in v:
			print(*i)


from math import log
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	ans = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		ans[i][i] = i + 1
	temp = 1
	temp1 = n + 1
	for i in range(1, n):
		for j in range(n - i):
			ans[j + i][j] = temp1
			temp1 += 1
		for j in range(n - i):
			ans[j][j + i] = temp1
			temp1 += 1
	for i in range(n):
		print(*ans[i])


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(1)
		continue
	if n == 2:
		print(-1)
		continue
	odd = even = n * n // 2
	cnt = 0
	if n * n % 2:
		odd += 1
	for i in range(odd):
		cnt += 1
		ending = ' '
		if not cnt % n:
			ending = '\n'
		print(1 + i * 2, end=ending)
	for i in range(even):
		cnt += 1
		ending = ' '
		if not cnt % n:
			ending = '\n'
		print((i + 1) * 2, end=ending)


from __future__ import division, print_function
import os
import 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 main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		if n == 2:
			print(-1)
			continue
		mat = [[0] * n for _ in range(n)]
		curr = 1
		for i in range(n):
			for j in range(i % 2, n, 2):
				mat[i][j] = curr
				curr += 1
		for i in range(n):
			for j in range(1 - i % 2, n, 2):
				mat[i][j] = curr
				curr += 1
		print(*[' '.join(map(str, row)) for row in mat], sep='\n')
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')
main()


import sys, heapq, math
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	ans = [[0] * n for j in range(n)]
	(p, d) = ([], [])
	for i in range(1, n * n + 1):
		if i % 2 == 0:
			p.append(i)
		else:
			d.append(i)
	x = p + d
	if n == 2:
		print(-1)
	elif n == 1:
		print(1)
	else:
		for k in range(len(x)):
			print(x[k], end=' ')
			if (k + 1) % n == 0:
				print()


def func():
	n = int(input())
	if n == 1:
		print(1)
		return
	elif n == 2:
		print(-1)
		return
	numbers_odd = [i for i in range(1, n ** 2 + 1, 2)]
	numbers_even = [i for i in range(2, n ** 2 + 1, 2)]
	for i in range(n):
		for j in range(n):
			if len(numbers_odd) > 0:
				print(numbers_odd.pop(0), end=' ')
			else:
				print(numbers_even.pop(0), end=' ')
		print()
t = int(input())
for _ in range(t):
	func()


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


from io import BytesIO, IOBase
from math import sqrt
import sys, os, math
from os import path
int_inpl = lambda : list(map(int, input().split()))
int_inpm = lambda : map(int, input().split())
int_inp = lambda : int(input())
inp = lambda : input()
from collections import Counter
import heapq
testcases = int_inp()
while testcases:
	testcases -= 1
	int_nump = int_inp()
	if int_nump == 2:
		print(-1)
	elif int_nump == 1:
		print(1)
	else:
		int_list = [[0 for i in range(int_nump)] for j in range(int_nump)]
		int_sqrt_o = int_nump ** 2
		int_matri1 = 0
		int_matri2 = 0
		for i in range(1, int_sqrt_o + 1, 2):
			int_list[int_matri1][int_matri2 % int_nump] = i
			if int_matri2 % int_nump == int_nump - 1:
				int_matri1 += 1
			int_matri2 += 1
		for i in range(2, int_sqrt_o + 1, 2):
			int_list[int_matri1][int_matri2 % int_nump] = i
			if int_matri2 % int_nump == int_nump - 1:
				int_matri1 += 1
			int_matri2 += 1
		for i in int_list:
			print(*i)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = []
	if n == 2:
		print(-1)
		continue
	for i in range(1, n * n + 1, 2):
		a.append(i)
	for i in range(2, n * n + 1, 2):
		a.append(i)
	for i in range(len(a)):
		print(a[i], end=' ')
		if i % n == n - 1:
			print()


T = int(input())
while T > 0:
	n = int(input())
	if n == 2:
		print(-1)
	else:
		list1 = [i for i in range(1, n * n + 1)]
		mat = []
		i = 0
		j = n * n - 1
		c = 0
		temp = []
		f = 1
		l = 0
		while i <= j:
			if c == n:
				mat.append(temp)
				temp = []
				c = 0
			if f == 1:
				temp.append(list1[i])
				f = 0
				l = 1
				c += 1
				i += 1
			elif l == 1:
				temp.append(list1[j])
				f = 1
				l = 0
				c += 1
				j -= 1
		mat.append(temp)
		t = mat[0][0]
		mat[0][0] = mat[n - 1][n - 1]
		mat[n - 1][n - 1] = t
		for i in range(0, n):
			for j in range(0, n):
				print(mat[i][j], end=' ')
			print()
	T -= 1


import sys
import math
import heapq

def input():
	return sys.stdin.readline().strip('\n')

def I():
	return input()

def II():
	return int(input())

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

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

def isPerfectSquare(x):
	return int(math.sqrt(x)) ** 2 == x
for _ in range(II()):
	n = II()
	if n == 2:
		print(-1)
	else:
		a = []
		for i in range(1, n * n + 1, 2):
			a.append(i)
		for i in range(2, n * n + 1, 2):
			a.append(i)
		for j in range(0, len(a), n):
			print(*a[j:j + n])


def solve():
	n = int(input())
	if n == 1:
		print('1')
		return
	if n == 2:
		print('-1')
		return
	grid = []
	for i in range(1, n + 1):
		ve = []
		for j in range(1, n + 1):
			ve.append(0)
		grid.append(ve)
	odd = 1
	even = 2
	for i in range(0, n):
		for j in range(0, n):
			if odd <= n * n:
				grid[i][j] = odd
				odd += 2
			else:
				grid[i][j] = even
				even += 2
	for i in range(0, n):
		for j in range(0, n):
			print(grid[i][j], end=' ')
		print('\n')
testCase = int(input())
for i in range(0, testCase):
	solve()


for _ in range(int(input())):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	k = 1
	for i in range(n):
		for j in range(n):
			print(k, end=' ')
			k += 2
			if k > n * n:
				k = 2
		print()


from random import randint, shuffle
t = int(input())
m = []
n = 0

def dirs(r, c):
	return [(rr, cc) for (rr, cc) in [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)] if 0 <= rr < n and 0 <= cc < n]

def adj_cells(r, c):
	return [m[rr][cc] for (rr, cc) in dirs(r, c)]

def adj(a, b):
	if a is None or b is None:
		return False
	return abs(a - b) == 1

def solve(n):
	numbers = [i for i in range(1, n * n + 1, 2)]
	numbers += [i for i in range(2, n * n + 1, 2)]
	placing = 0
	sol = []
	for r in range(n):
		sol.append([])
		for c in range(n):
			sol[r].append(numbers[placing])
			placing += 1
	return sol
attempts = 10
for case in range(1, t + 1):
	n = int(input())
	if n == 1:
		print(1)
		continue
	elif n == 2:
		print(-1)
		continue
	sol = solve(n)
	for r in range(len(sol)):
		print(' '.join([str(i) for i in sol[r]]))


aa = [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666, 777, 888, 999, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 111111, 222222, 333333, 444444, 555555, 666666, 777777, 888888, 999999, 1111111, 2222222, 3333333, 4444444, 5555555, 6666666, 7777777, 8888888, 9999999, 11111111, 22222222, 33333333, 44444444, 55555555, 66666666, 77777777, 88888888, 99999999, 111111111, 222222222, 333333333, 444444444, 555555555, 666666666, 777777777, 888888888, 999999999]
for _ in range(int(input())):
	n = int(input())
	a = []
	s = 1
	for i in range(n):
		b = []
		for j in range(n):
			b.append(s)
			s += 2
			if n % 2 == 0 and s == n * n + 1:
				s = 2
			if n % 2 != 0 and s == n * n + 2:
				s = 2
		a.append(b)
	result = True
	if n == 1:
		print(1)
	else:
		for i in range(n):
			for j in range(n):
				if i == 0:
					if j == 0:
						if abs(a[0][0] - a[0][1]) == 1 or abs(a[0][0] - a[1][0]) == 1:
							result = False
							break
					elif j == n - 1:
						if abs(a[0][n - 1] - a[0][n - 2]) == 1 or abs(a[0][n - 1] - a[1][n - 1]) == 1:
							result = False
							break
					elif abs(a[0][j] - a[0][j - 1]) == 1 or abs(a[0][j] - a[0][j + 1]) == 1 or abs(a[0][j] - a[1][j]) == 1:
						result = False
						break
				elif i == n - 1:
					if j == 0:
						if abs(a[n - 1][0] - a[n - 1][1]) == 1 or abs(a[n - 1][0] - a[n - 2][0]) == 1:
							result = False
							break
					elif j == n - 1:
						if abs(a[n - 1][n - 1] - a[n - 1][n - 2]) == 1 or abs(a[n - 1][n - 1] - a[n - 2][n - 1]) == 1:
							result = False
							break
					elif abs(a[n - 1][j] - a[n - 1][j - 1]) == 1 or abs(a[n - 1][j] - a[n - 1][j + 1]) == 1 or abs(a[n - 1][j] - a[n - 2][j]) == 1:
						result = False
						break
				elif j == 0:
					if abs(a[i][0] - a[i][1]) == 1 or abs(a[i][0] - a[i + 1][0]) == 1 or abs(a[i][0] - a[i - 1][0]) == 1:
						result = False
						break
				elif j == n - 1:
					if abs(a[i][n - 1] - a[i][n - 2]) == 1 or abs(a[i][n - 1] - a[i - 1][n - 1]) == 1 or abs(a[i][n - 1] - a[i + 1][n - 1]) == 1:
						result = False
						break
				elif abs(a[i][j] - a[i][j - 1]) == 1 or abs(a[i][j] - a[i][j + 1]) == 1 or abs(a[i][j] - a[i - 1][j]) == 1 or (abs(a[i][j] - a[i + 1][j]) == 1):
					result = False
					break
	if result == False and n != 1:
		print(-1)
	elif result == True and n != 1:
		for i in a:
			print(*i, sep=' ')


import random

def slow(N):
	allow = {}
	P = {}
	used = set()
	M = [[0 for _ in range(N)] for _ in range(N)]
	for i in range(N):
		for j in range(N):
			allow[i, j] = set(range(1, N * N + 1))
			P[i, j] = 0

	def n_allows(p):
		return len(allow[p] - used)
	while P:
		(i, j) = min(P, key=n_allows)
		if not allow[i, j] - used:
			return None
		k = random.choice(tuple(allow[i, j] - used))
		M[i][j] = k
		del P[i, j]
		used.add(k)
		for (di, dj) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
			if (i + di, j + dj) in P:
				for n in (k + 1, k - 1):
					if n in allow[i + di, j + dj] - used:
						allow[i + di, j + dj].remove(n)
	return M

def build(N):
	if N == 2:
		return False
	M = [[0 for _ in range(N)] for _ in range(N)]
	k = 1
	for i in range(N):
		for j in range(N):
			M[i][j] = k
			k += 2
			if k > N * N:
				k = 2
	return M
T = int(input())
for _ in range(T):
	N = int(input())
	r = build(N)
	if not r:
		print(-1)
		continue
	for row in r:
		print(' '.join(map(str, row)))


test_cases = int(input())
for test_case in range(1, test_cases + 1):
	dimension = int(input())
	result = []
	number = 1
	row = []
	while number <= dimension ** 2:
		if len(row) == dimension:
			result.append(row)
			row = []
		row.append(number)
		number += 2
	number = 2
	while number <= dimension ** 2:
		if len(row) == dimension:
			result.append(row)
			row = []
		row.append(number)
		number += 2
	if row:
		result.append(row)
	if dimension == 2:
		print(-1)
	else:
		for row in result:
			for column in row:
				print(column, end=' ')
			print()


import sys
import math
from heapq import heappop, heappush, heapify
intl = lambda : list(map(int, inp().split(' ')))
iinp = lambda : int(sys.stdin.readline())
inp = lambda : sys.stdin.readline().strip()
strl = lambda : list(inp().strip().split(' '))
mint = lambda : map(int, inp().split())
finp = lambda : list(map(float, inp().split(' ')))
flush = lambda : sys.stdout.flush()
ran = lambda x: range(len(x))
enum = lambda x, y=0: enumerate(x, y)
mati = lambda n, fun=0: [[fun if type(fun) is int else fun(i, j) for j in range(n)] for i in range(n)]
vec = lambda n, fun=0: [fun if type(fun) is int else fun(i) for i in range(n)]
vecst = lambda n, fun=0: [fun if type(fun) is int else fun() for i in range(n)]
vecadv = lambda n, fun=0, s=0, st=1: [fun if type(fun) is int else fun(i) for i in range(s, n, st)]
matinp = lambda n: [intl() for i in range(n)]
for _ in range(iinp()):
	n = iinp()
	if n == 2:
		print(-1)
		continue
	xx = list(range(1, n * n + 1))
	print(*xx[::2] + xx[1::2])


t = int(input())
for co in range(t):
	n = int(input())
	if n == 1:
		print('1')
	elif n == 2:
		print('-1')
	else:
		l = [[0 for i in range(n)] for j in range(n)]
		k = 1
		for i in range(n):
			l[i][i] = k
			k += 1
		for i in range(1, n):
			c = i
			for r in range(n - i):
				l[r][c] = k
				k += 1
				c += 1
			r = i
			for c in range(n - i):
				l[r][c] = k
				r += 1
				k += 1
		for i in range(n):
			for j in range(n):
				print(l[i][j], end=' ')
			print()


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


for t in range(int(input())):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	lis = []
	for i in range(1, n * n + 1, 2):
		lis.append(i)
	for i in range(2, n * n + 1, 2):
		lis.append(i)
	for i in range(1, n * n + 1):
		print(lis[i - 1], end=' ')
		if i % n == 0:
			print()


t = int(input())
for x in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		val = 1
		v = []
		for j in range(n):
			ar = []
			for k in range(n):
				ar.append(0)
			v.append(ar)
		for k in range(n):
			i = k
			j = 0
			while i < n:
				v[i][j] = val
				val += 1
				i += 1
				j += 1
			if k == 0:
				continue
			i = 0
			j = k
			while j < n:
				v[i][j] = val
				val += 1
				i += 1
				j += 1
		for j in range(n):
			for k in range(n):
				print(v[j][k], end=' ')
			print()


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


t = int(input())
import math
for _ in range(t):
	n = int(input())
	mat = []
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		k = 1
		p = math.ceil(n * n / 2) + 1
		for i in range(n):
			a = []
			for j in range(n):
				if (i + j) % 2 == 0:
					a.append(k)
					k += 1
				else:
					a.append(p)
					p += 1
			mat.append(a)
		for i in range(n):
			print(*mat[i])


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


vvods = int(input())
for _ in range(vvods):
	side = int(input())
	if side == 2:
		print(-1)
	else:
		mat = [[0 for mhm in range(side)] for aha in range(side)]
		(i, j) = (side - 1, 0)
		for nu in range(1, side ** 2 + 1):
			mat[i][j] = nu
			(i, j) = (i - 1, j + 1)
			if i < 0:
				i = side - 1
			if j >= side:
				j = 0
			if mat[i][j] != 0:
				i += 1
			if i > side - 1:
				i = 0
		for row in range(side):
			print(*mat[row])


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


tcs = int(input())
for tc in range(tcs):
	n = int(input())
	if n == 1:
		print(1)
		continue
	if n == 2:
		print(-1)
		continue
	M = [[0] * n for _ in range(n)]
	odd = 1
	even = 2
	for i in range(n):
		for j in range(n):
			if odd <= n * n:
				M[i][j] = str(odd)
				odd += 2
			else:
				M[i][j] = str(even)
				even += 2
	for lst in M:
		print(' '.join(lst))


from collections import defaultdict
for i in range(int(input())):
	n = int(input())
	ans = []
	if n == 2:
		print(-1)
	else:
		c = 1
		sq = n ** 2
		while c <= sq:
			for j in range(n):
				if c > sq:
					break
				ans.append(c)
				c += 2
		c = 2
		while c <= sq:
			for j in range(n):
				if c > sq:
					break
				ans.append(c)
				c += 2
		pos = 0
		for j in range(n):
			for k in range(n):
				print(ans[pos], end=' ')
				pos += 1
			print()


def solve(size):
	if size == 2:
		print(-1)
		return
	res = [[0 for _ in range(size)] for _ in range(size)]
	(val, start) = (1, 0)
	for i in range(size):
		for j in range(start, size, 2):
			res[i][j] = val
			val += 1
		start = 1 - start
	start = 1
	for i in range(size):
		for j in range(start, size, 2):
			res[i][j] = val
			val += 1
		start = 1 - start
	for r in res:
		print(*r)
n = int(input())
for _ in range(n):
	size = int(input())
	solve(size)


import sys, os, io
from sys import stdin, stdout
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import bisect
import math
input = stdin.readline
alphabets = 'abcdefghijklmnopqrstuvwxyz'

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

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

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

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 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 countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

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

def ii():
	return int(input())

def li():
	return list(map(int, input().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')

def power_set(L):
	cardinality = len(L)
	n = 2 ** cardinality
	powerset = []
	for i in range(n):
		a = bin(i)[2:]
		subset = []
		for j in range(len(a)):
			if a[-j - 1] == '1':
				subset.append(L[j])
		powerset.append(subset)
	powerset_orderred = []
	for k in range(cardinality + 1):
		for w in powerset:
			if len(w) == k:
				powerset_orderred.append(w)
	return powerset_orderred

def fastPlrintNextLines(a):
	print('\n'.join(map(str, a)))

def sortByFirstAndSecond(A):
	A = sorted(A, key=lambda x: x[0])
	A = sorted(A, key=lambda x: x[1])
	return list(A)

def dfs(node, vis, adj, lis):
	lis.append(node)
	vis[node] = 1
	for i in adj[node]:
		if vis[i] == 0:
			dfs(i, vis, adj, lis)

def dfsOfGraph(n, adj):
	lis = []
	vis = [0] * (n + 1)
	for i in range(1, n + 1):
		if vis[i] == 0:
			dfs(i, vis, adj, lis)
	return lis

def bfsOfGraph(n, adj):
	vis = [0] * (n + 1)
	lis = []
	for i in range(1, n + 1):
		if vis[i] == 0:
			queue = [i]
			vis[i] = 1
			while len(queue) > 0:
				node = queue.pop(0)
				lis.append(node)
				for j in adj[node]:
					if vis[j] == 0:
						queue.append(j)
						vis[j] = 1
	return lis
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	n = ii()
	even = 2
	odd = 1
	if n == 2:
		print(-1)
		return
	res = []
	for i in range(n):
		res.append([0] * n)
	for i in range(n):
		for j in range(n):
			if odd <= n ** 2:
				res[i][j] = odd
				odd += 2
			else:
				res[i][j] = even
				even += 2
	for i in range(n):
		print(*res[i])
t = ii()
for _ in range(t):
	solve()


def evenmat(n):
	m = [[0 for i in range(n)] for j in range(n)]
	k = 1
	c = 0
	for i in range(n):
		for j in range(n):
			c += 1
			if c % 2 == 1:
				m[i][j] = k
				k += 1
	c = 0
	for i in range(n):
		for j in range(n):
			c += 1
			if c % 2 == 0:
				m[i][j] = k
				k += 1
	return m

def pmat(m):
	for i in range(len(m)):
		for j in range(len(m)):
			print(m[i][j], end=' ')
		print('')
t = int(input())
for tt in range(t):
	n = int(input())
	if n == 2:
		m = -1
		print(m)
	else:
		m = evenmat(n)
		pmat(m)


t = int(input())
for _ in range(t):
	a = int(input())
	if a == 1:
		print(1)
	elif a == 2:
		print(-1)
	elif a == 3:
		a1 = [2, 9, 7]
		a2 = [4, 6, 3]
		a3 = [1, 8, 5]
		print(*a1)
		print(*a2)
		print(*a3)
	else:
		mat = [[0 for i in range(a)] for j in range(a)]
		count = 0
		for i in range(0, len(mat)):
			for j in range(0, len(mat), 2):
				count += 1
				mat[i][j] = count
		for i in range(0, len(mat)):
			for j in range(1, len(mat), 2):
				count += 1
				mat[i][j] = count
		for i in range(0, len(mat)):
			print(*mat[i])


for _ in range(int(input())):
	a = int(input())
	if a == 2:
		print(-1)
	else:
		t = []
		for i in range(a):
			z = []
			for j in list(range(i, a)) + list(range(i)):
				z.append(i + 1 + j * a)
			t.append(z)
		for i in range(a):
			for j in range(a):
				print(t[i][j], end=' ')
			print()


for i in range(int(input())):
	n = int(input())
	if n == 2:
		print(-1)
	else:
		odd = []
		even = []
		case = False
		for i in range(1, n ** 2 + 1):
			if case:
				even.append(i)
			else:
				odd.append(i)
			case = not case
		arr = odd + even
		prev = 0
		for i in range(n):
			print(*arr[prev:prev + n])
			prev += n


from collections import defaultdict
for cases in range(int(input())):
	ans = 'YES'
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		mat = []
		cnt = 0
		for i in range(0, n):
			v = [cnt + i for i in range(1, n + 1)]
			cnt += n
			mat.append(v)
		for j in range(0, n, 2):
			for i in range(1, n):
				(mat[i][j], mat[i - 1][j]) = (mat[i - 1][j], mat[i][j])
		for i in mat:
			print(*i)


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 NotAdjacentMatrix(n):
	if n == 1:
		print(1)
		return
	if n == 2:
		print(-1)
		return
	tmp = []
	for i in range(1, n * n + 1, 2):
		tmp.append(str(i))
		if len(tmp) == n:
			print(' '.join(tmp))
			tmp = []
	for i in range(2, n * n + 1, 2):
		tmp.append(str(i))
		if len(tmp) == n:
			print(' '.join(tmp))
			tmp = []
	return
t = inp()
for i in range(t):
	n = inp()
	NotAdjacentMatrix(n)


for _ in range(int(input())):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	if n == 1:
		print(1)
		continue
	a = 1
	count = 0
	while a <= n ** 2:
		if count == n:
			print()
			count = 0
		print(a, end=' ')
		if n % 2 == 0 and a == n ** 2 - 1:
			a = 2
		elif n % 2 == 1 and a == n ** 2:
			a = 2
		else:
			a += 2
		count += 1


t = int(input())
for _ in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		a = []
		arr = []
		for i in range(1, n ** 2 + 1, 2):
			a.append(i)
			if len(a) == n:
				print(*a)
				a = []
		for i in range(2, n ** 2 + 1, 2):
			if len(a) == n:
				print(*a)
				a = []
			a.append(i)
		print(*a)


def draw(n):
	l = []
	for i in range(1, n * n + 1, 2):
		l.append(i)
	for i in range(2, n * n + 1, 2):
		l.append(i)
	idx = 0
	for i in range(n):
		for j in range(n):
			print(l[idx], end=' ')
			idx += 1
		print()
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		draw(n)


from collections import deque
t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		ans = []
		for j in range(n):
			ans.append(deque([n * k + (j + 1) for k in range(n)]))
		for j in range(n):
			ans[j].rotate(j)
			print(*ans[j])


def main():
	n = int(input())
	nn = n * n
	if n == 1:
		print(1)
		return
	if n == 2:
		print(-1)
		return
	d = {}
	for a in range(1, n + 1):
		d[a, a] = a
	for i in range(n - 1, 0, -1):
		for j in range(1, i + 1):
			a += 1
			d[n - i + j, j] = a
		for j in range(1, i + 1):
			a += 1
			d[j, n - i + j] = a
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			print(d[i, j], end=' ')
		print('')
t = int(input())
for i in range(t):
	main()


t = int(input())
while t:
	t -= 1
	a = [[9, 2, 4], [3, 5, 7], [6, 8, 1]]
	n = int(input())
	if n == 1:
		print(1)
		continue
	if n == 2:
		print(-1)
		continue
	for i in range(3, n):
		for j in range(i):
			a[j].append(i * i + 1 + j * 2)
		sub = []
		for j in range(i):
			sub.append(i * i + 2 + j * 2)
		sub.append(a[0][0])
		a[0][0] = (i + 1) ** 2
		a.append(sub)
	for i in range(n):
		print(' '.join([str(I) for I in a[i]]))


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


import math

def gen(brr):
	arr = []
	crr = []
	drr = []
	for i in range(0, len(brr), 2):
		crr.append(brr[i])
	for i in range(1, len(brr), 2):
		drr.append(brr[i])
	return crr + drr

def generateMatrix(arr, A):
	grid = [[0] * A for _ in range(A)]
	L = 0
	R = A - 1
	T = 0
	B = A - 1
	direction = 0
	num = 0
	while L <= R and T <= B:
		if direction == 0:
			for i in range(L, R + 1):
				grid[T][i] = arr[num]
				num += 1
			T += 1
		elif direction == 1:
			for i in range(T, B + 1):
				grid[i][R] = arr[num]
				num += 1
			R -= 1
		elif direction == 2:
			for i in range(R, L - 1, -1):
				grid[B][i] = arr[num]
				num += 1
			B -= 1
		elif direction == 3:
			for i in range(B, T - 1, -1):
				grid[i][L] = arr[num]
				num += 1
			L += 1
		direction = (direction + 1) % 4
	return grid
for _ in range(int(input())):
	n = int(input())
	k = n
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	elif n == 3:
		print('2 9 7')
		print('4 6 3')
		print('1 8 5')
	elif n == 4:
		print('2 9 7 10')
		print('4 6 3 12')
		print('1 8 5 14')
		print('15 13 11 16')
	else:
		arr = list(range(1, n ** 2 + 1))
		for i in range(0, len(arr), k):
			ar = arr[i:i + k]
			ar = gen(ar)
			print(' '.join(map(str, ar)))


import bisect
import collections
import functools
import itertools
import math
import heapq
import random
import string

def repeat(_func=None, *, times=1):

	def decorator(func):

		@functools.wraps(func)
		def wrapper(*args, **kwargs):
			for _ in range(times):
				func(*args, **kwargs)
		return wrapper
	if _func is None:
		return decorator
	else:
		return decorator(_func)

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def getint():
	return int(input())

def getmatrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def display_matrix(mat):
	for i in range(len(mat)):
		print(*mat[i])
MOD = 1000000007

@repeat(times=int(input()))
def main():
	n = getint()
	mat = [[0] * n for _ in range(n)]
	z = n * n
	if n == 2:
		print(-1)
		return

	def get():
		arr = list(range(1, z + 1))
		k = 0
		for i in range(z + 1):
			yield arr[k]
			k += 2
			if k >= z:
				k = 1
	it = get()
	for i in range(n):
		for j in range(n):
			mat[i][j] = next(it)
	display_matrix(mat)
main()


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


t = int(input())
for i in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		nmat = [[0] * n for j in range(n)]
		lastd = -1
		val = 1
		j = 0
		while j < n:
			if j % 2 == 0:
				for k in range(0, n, 2):
					nmat[j][k] = val
					lastd = val
					val += 1
			else:
				for k in range(1, n, 2):
					nmat[j][k] = val
					lastd = val
					val += 1
			j += 1
		val = lastd + 1
		for j in range(n):
			if j % 2 == 1:
				last = 1
				for k in range(0, n, 2):
					nmat[j][k] = val
					val += 1
			else:
				for k in range(1, n, 2):
					nmat[j][k] = val
					val += 1
		for j in range(n):
			print(*nmat[j])


mod = pow(10, 9) + 7
MOD = 998244353
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from bisect import bisect, bisect_left, bisect_right
from math import *
from itertools import *
from collections import *
sml = {chr(ord('a') + i): i for i in range(26)}
cap = {chr(ord('A') + i): i for i in range(26)}

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

def get_int():
	return int(input())

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

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

def godpower(b, p):
	res = 1
	while p > 0:
		if p % 2 == 0:
			b = b * b
			p //= 2
		else:
			res *= b
			p -= 1
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def get(n):
	d = []
	while n > 0:
		n1 = n % 10
		d.append(n1)
		n //= 10
	return len(d)
for tt in range(get_int()):
	n = get_int()
	e = []
	o = []
	for i in range(1, n * n + 1):
		if i % 2:
			o.append(i)
		else:
			e.append(i)
	ans = e + o
	mat = [[0 for i in range(n)] for i in range(n)]
	for i in range(n):
		for j in range(n):
			mat[i][j] = ans.pop(0)
	if n == 2:
		print(-1)
	else:
		for i in mat:
			print(*i)


for s in [*open(0)][1:]:
	n = int(s)
	a = n * n * [0]
	j = 1
	for f in (0, 1):
		for i in range(n * n):
			if i // n + i % n & 1 ^ f:
				a[i] = j
				j += 1
	print(*(a, [-1])[n == 2])


R = lambda : list(map(int, input().split()))
for _ in ' ' * int(input()):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		a = [[-1] * n for _ in ' ' * n]
		curr = 1
		for i in range(n):
			for j in range(n):
				if not i + j & 1:
					a[i][j] = curr
					curr += 1
		for i in range(n):
			for j in range(n):
				if i + j & 1:
					a[i][j] = curr
					curr += 1
		for i in range(n):
			print(*a[i])


from collections import defaultdict as dic
for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		mat = list(range(1, n * n + 1))
		k = n * n
		p = k - 1
		for i in range(n):
			for j in range(n):
				if p >= 0:
					print(mat[p], end=' ')
				else:
					p = k - 2
					print(mat[p], end=' ')
				p -= 2
			print()


import sys, os
import math
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		if n == 2:
			print(-1)
			continue
		mat = [[-1 for i in range(n)] for j in range(n)]
		for i in range(n):
			mat[i][i] = i + 1
		(i, c) = (1, n + 1)
		while i < n:
			(x, y) = (i, 0)
			while x < n:
				mat[x][y] = c
				x += 1
				y += 1
				c += 1
			(x, y) = (0, i)
			while y < n:
				mat[x][y] = c
				x += 1
				y += 1
				c += 1
			i += 1
		for i in mat:
			print(*i)
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 MI():
	return map(int, input().split())

def outP(var):
	sys.stdout.write(str(var) + '\n')
MOD = 10 ** 9 + 7
main()


for test in range(int(input())):
	n = int(input())
	if n == 1:
		print(1)
		continue
	elif n == 2:
		print(-1)
		continue
	else:
		ans = [[0] * n for i in range(n)]
		ans[-1][0] = n ** 2
		ans[0][-1] = n ** 2 - 1
		q = 1
		for i in range(n - 1):
			k = 0
			for j in range(n - i):
				ans[j][i + k] = q
				k += 1
				q += 1
		for i in range(1, n - 1):
			k = 0
			for j in range(n - i):
				ans[i + k][k] = q
				q += 1
				k += 1
		for i in ans:
			print(*i)


def adj(n):
	if n == 3:
		arr = [[2, 9, 7], [4, 6, 3], [1, 8, 5]]
		return arr
	arr = [[0 for i in range(n)] for i in range(n)]
	num = 1
	for i in range(n):
		for j in range(0, n, 2):
			arr[i][j] = num
			num += 1
	for i in range(n):
		for j in range(1, n, 2):
			arr[i][j] = num
			num += 1
	return arr
t = int(input())
for i in range(t):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	res = adj(n)
	for i in range(len(res)):
		print(*res[i], sep=' ')


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

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

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

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

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

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

def Solution(n):
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		res = [[0 for _ in range(n)] for _ in range(n)]
		c = 1
		for i in range(n):
			res[i][i] = c
			c += 1
		for i in range(1, n):
			for j in range(i):
				res[i][j] = c
				res[j][i] = c + 1
				c += 2
		for i in range(n):
			print(*res[i])

def main():
	for _ in range(get_int()):
		Solution(get_int())
main()


def solve(n):
	if n == 1:
		return [[1]]
	elif n == 2:
		return [[-1]]
	mat = [[0] * n for _ in range(n)]
	(i, num) = (0, 1)
	while i < n * n:
		mat[i // n][i % n] = num
		num += 1
		i += 2
	i = 1
	while i < n * n:
		mat[i // n][i % n] = num
		num += 1
		i += 2
	return mat
t = int(input())
for _ in range(t):
	n = int(input())
	for row in solve(n):
		print(' '.join(map(str, row)))


import sys, os
from io import BytesIO, IOBase
mod = 10 ** 9 + 7
Mod = 998244353
INF = float('inf')
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')
inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split()))
tc = 1
tc = int(input())
for test in range(1, tc + 1):
	n = int(input())
	if n == 2:
		print(-1)
		continue
	a = [[0] * n for i in range(n)]
	cnt = 1
	for i in range(n):
		a[i][i] = cnt
		cnt += 1
	cnt1 = cnt
	cnt2 = cnt + 1
	for i in range(n):
		for j in range(n):
			if i < j:
				a[i][j] = cnt1
				cnt1 += 2
			if j < i:
				a[i][j] = cnt2
				cnt2 += 2
	for i in a:
		print(*i)


import os, sys, threading
from io import BytesIO, IOBase
threading.stack_size(2 ** 25)
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 ceil
from bisect import bisect_right
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = [[0] * n for _ in range(n)]
	t = 1
	z = 2
	if n == 2:
		print(-1)
		continue
	for i in range(n):
		for j in range(n):
			a[i][j] = t
			t += z
			if t > n * n:
				t = 2
	for i in a:
		print(*i)


x = int(input())
for i in range(x):
	n = int(input())
	if n == 2:
		print(-1)
	else:
		b = []
		for j in range(1, n * n + 1):
			b.append(j)
		ar = [[0] * n] * n
		m = 0
		y = (len(b) + 1) // 2
		for j in range(n):
			for k in range(n):
				if j % 2 == 0:
					if k % 2 == 0:
						ar[j][k] = b[m]
						m = m + 1
					else:
						ar[j][k] = b[y]
						y = y + 1
				if j % 2 == 1:
					if k % 2 == 1:
						ar[j][k] = b[m]
						m = m + 1
					else:
						ar[j][k] = b[y]
						y = y + 1
				print(ar[j][k], end=' ')
			print()


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		mat = []
		od = 1
		ev = 2
		for i in range(n):
			c = []
			for j in range(n):
				if od <= n * n:
					c.append(od)
					od += 2
				else:
					c.append(ev)
					ev += 2
			mat.append(c)
		for i in mat:
			for j in i:
				print(j, end=' ')
			print('\n')


t = int(input())

def solve():
	n = int(input())
	if n == 2:
		print('-1')
		return
	arr = []
	l = 1
	r = n ** 2
	while l <= r:
		arr.append(l)
		if r != l:
			arr.append(r)
		l += 1
		r -= 1
	(arr[0], arr[-1]) = (arr[-1], arr[0])
	matrix = []
	for i in range(n):
		matrix.append([0] * n)
	for i in range(len(arr)):
		matrix[i // n][i % n] = arr[i]
	for i in matrix:
		print(*i)
for i in range(t):
	solve()


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())
for _ in range(ii()):
	n = ii()
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	elif n == 3:
		l = [[2, 9, 7], [4, 6, 3], [1, 8, 5]]
		for a in l:
			for i in a:
				print(i, end=' ')
			print()
	else:
		l = []
		for i in range(n):
			l.append([0] * n)
		p = 1
		for i in range(n):
			for j in range(0, n, 2):
				l[i][j] = p
				p += 1
		for i in range(n):
			for j in range(1, n, 2):
				l[i][j] = p
				p += 1
		for a in l:
			for i in a:
				print(i, end=' ')
			print()


def fun(n):
	if n == 1:
		print(1)
		return
	if n == 2:
		print(-1)
		return
	ls = [[0 for j in range(n)] for i in range(n)]
	value = 1
	for i in range(n):
		for j in range(0, n, 2):
			if i % 2 != 0:
				j = j + 1
			if j < n:
				ls[i][j] = value
				value += 1
	for i in range(n):
		for j in range(0, n, 2):
			if i % 2 == 0:
				j = j + 1
			if j < n:
				ls[i][j] = value
				value += 1
	for i in ls:
		print(*i)
T = int(input())
for _ in range(T):
	n = int(input())
	fun(n)


from collections import defaultdict
from sys import stdin, stdout, setrecursionlimit
from math import *
from statistics import mode

def SieveOfEratosthenes(n):
	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
	return prime

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

def prime_factors(n):
	(i, factors) = (2, [])
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return set(factors)

def prefix_arr(arr):
	for i in range(1, len(arr)):
		arr[i] = arr[i] + arr[i - 1]
	return arr

def kadane(a):
	max_so_far = curr_max = a[0]
	for i in range(1, len(a)):
		curr_max = max(a[i], curr_max + a[i])
		max_so_far = max(max_so_far, curr_max)
	return max_so_far

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, ceil(sqrt(n)) + 1, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

def isPowerOf2(x):
	return x and (not x & x - 1)

def isPerfectSquare(n):
	return pow(floor(sqrt(n)), 2) == n

def highestPowerof2(n):
	p = int(log(n, 2))
	return int(pow(2, p))

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

def S():
	return input()

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

def ars():
	return list(map(str, input().split()))

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

def mps():
	return map(str, input().split())
setrecursionlimit(10 ** 8)

def func(N):
	if N == 1:
		return [[1]]
	if N == 2:
		return [[-1]]
	mat = [[-1 for i in range(N)] for j in range(N)]
	od = True
	ev = False
	ods = 1
	evs = 2
	for i in range(N):
		for j in range(N):
			if ods <= N ** 2:
				mat[i][j] = ods
				ods += 2
			else:
				mat[i][j] = evs
				evs += 2
	return mat
for t in range(I()):
	N = I()
	mt = func(N)
	for i in mt:
		print(*i)


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = int(input())
for test in range(t):
	n = int(input())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		a = [0 for i in range(n ** 2)]
		value = 1
		idx = 0
		for i in range(2, n):
			if gcd(i, n ** 2) == 1:
				prime = i
				break
		while value <= n ** 2:
			a[idx] = value
			value += 1
			idx += prime
			idx %= n ** 2
		for i in range(n ** 2):
			print(a[i], end=' ')
			if (i + 1) % n == 0:
				print()


import math
t = int(input())

def get_answer(n):
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		N = n ** 2
		for i in range(n):
			a = [0] * n
			for j in range(n):
				a[j] = 1 + (i * (n - 1) + j * n) % N
			print(*a)
for _ in range(t):
	n = int(input())
	get_answer(n)


import io
import os
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from heapq import heappush, heappop, heapify
from math import gcd, inf

def solve(N):
	if N == 2:
		return -1
	nums = deque(range(1, N * N + 1))
	grid = [[0 for j in range(N)] for i in range(N)]
	for i in range(N):
		for j in range(N):
			if (i + j) % 2 == 0:
				grid[i][j] = nums.popleft()
	for i in range(N):
		for j in range(N):
			if (i + j) % 2 == 1:
				grid[i][j] = nums.popleft()
	return '\n'.join((' '.join(map(str, row)) for row in grid))
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())
	if n == 1:
		print(1)
	elif n == 2:
		print(-1)
	else:
		dp = [[0 for i in range(n)] for j in range(n)]
		dp[0][n - 1] = 2
		dp[n - 1][0] = 1
		ct = 3
		for i in range(n - 2, -1, -1):
			j = 0
			k = i
			while k < n and j < n:
				dp[k][j] = ct
				ct += 1
				j += 1
				k += 1
		for i in range(1, n - 1):
			j = i
			k = 0
			while k < n and j < n:
				dp[k][j] = ct
				ct += 1
				k += 1
				j += 1
		for i in dp:
			print(*i)


dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def solve():
	N = int(input())
	arr = [[0] * N for _ in range(N)]
	arr[0][0] = 1
	midCols = N - 2
	(L, R) = (1, N ** 2)
	usingL = True
	for r in range(N):
		for c in range(N):
			arr[r][c] = L if usingL else R
			if usingL:
				L += 1
			else:
				R -= 1
			usingL = not usingL
	(arr[0][0], arr[-1][-1]) = (arr[-1][-1], arr[0][0])
	for r in range(N):
		for c in range(N):
			for d in dirs:
				(x, y) = (r + d[0], c + d[1])
				if 0 <= x < N and 0 <= y < N:
					if abs(arr[x][y] - arr[r][c]) == 1:
						return -1
	return arr
for _ in range(int(input())):
	s = solve()
	if s == -1:
		print(s)
	else:
		for row in s:
			print(' '.join(map(str, row)))


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