People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.

— Pedro Domingos

You work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!

The department you work in sells $n \cdot k$ items. The first item costs $1$ dollar, the second item costs $2$ dollars, and so on: $i$-th item costs $i$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $n$ shelves in total, and each shelf contains exactly $k$ items. We will denote by $a_{i,j}$ the price of $j$-th item (counting from the left) on the $i$-th shelf, $1 \le i \le n, 1 \le j \le k$.

Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}$ for some shelf $i$ and indices $l \le r$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.

You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $a$ that:

Every number from $1$ to $n \cdot k$ (inclusively) occurs exactly once.

For each $i, l, r$, the mean price of items from $l$ to $r$ on $i$-th shelf is an integer.

Find out if such an arrangement is possible, and if it is, give any example of such arrangement.


-----Input-----

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

The first and only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 500$) — the number of shelves and length of each shelf, respectively.

It is guaranteed that the sum $n$ over all test cases does not exceed $500$ and the sum $k$ over all test cases does not exceed $500$.


-----Output-----

Print the answer for each test case.

If such an arrangement exists, print "YES" on a single line. After that, print any example on $n$ lines of $k$ numbers each, one line per shelf. Each number from $1$ to $n \cdot k$ must occur exactly once in the output.

If no good arrangement exists, print a single word "NO" on its own line.


-----Examples-----

Input
4
1 1
2 2
3 3
3 1
Output
YES
1 
YES
1 3 
2 4 
NO
YES
1 
2 
3


-----Note-----

None

Difficulty: EASY
Tags: Constructive algorithms

Starter code:

Solutions:
for t in range(int(input())):
	(n, k) = [*map(int, input().split())]
	if k != 1 and n % 2:
		print('NO')
		continue
	print('YES')
	for i in range(n):
		s = i // 2 * k * 2 + (2 if i % 2 else 1)
		print(' '.join([str(x) for x in range(s, s + 2 * (k - 1) + 1, 2)]))


import sys
fin = sys.stdin

def solve(n, k):
	if k == 1:
		answer = '\n'.join(map(str, range(1, n + 1)))
		return f'YES\n{answer}'
	elif n == 1:
		return 'NO'
	elif n % 2 == 0:
		answer = ['YES']
		for i in range(0, n, 2):
			start = i * k + 1
			end = i * k + 2 * k + 1
			answer.append(' '.join(map(str, range(start, end, 2))))
			answer.append(' '.join(map(str, range(start + 1, end, 2))))
		return '\n'.join(answer)
	return 'NO'
t = int(fin.readline())
for test in range(t):
	(n, k) = map(int, fin.readline().split())
	print(solve(n, k))


for t in range(int(input())):
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
		continue
	if n % 2 == 1:
		print('NO')
		continue
	print('YES')
	for j in range(0, n, 2):
		joined_string = ' '.join([str(i) for i in range(j * k + 1, (j + 2) * k + 1, 2)])
		print(joined_string)
		joined_string = ' '.join([str(i) for i in range(j * k + 2, (j + 2) * k + 1, 2)])
		print(joined_string)


def calc(n, k):
	tot = n * k
	even_tot = tot >> 1
	odd_tot = even_tot + (tot & 1)
	if even_tot % k or odd_tot % k:
		print('NO')
		return
	print('YES')
	even_idx = 2
	odd_idx = 1
	for i in range(n):
		row = []
		for j in range(k):
			if odd_idx <= tot:
				row.append(str(odd_idx))
				odd_idx += 2
			else:
				row.append(str(even_idx))
				even_idx += 2
		print(' '.join(row))
cnt = int(input())
while cnt:
	(n, k) = [int(i) for i in input().split()]
	calc(n, k)
	cnt -= 1


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

def build_grid(n, k):
	res = []
	num = 1
	for _ in range(n // 2):
		res.append([])
		res.append([])
		for j in range(k):
			res[-2].append(num)
			num += 1
			res[-1].append(num)
			num += 1
	return res

def solve():
	(n, k) = read_nums()
	if k == 1:
		print('YES')
		for i in range(n):
			print(i + 1)
		return
	if n % 2 == 0:
		print('YES')
		grid = build_grid(n, k)
		for row in grid:
			print(' '.join(map(str, row)))
		return
	print('NO')

def main():
	(t,) = read_nums()
	for _ in range(t):
		solve()
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	ans = []
	if n % 2 == 0 or k == 1:
		print('YES')
		if k == 1:
			for i in range(n):
				print(i + 1)
		else:
			for i in range(n // 2):
				start = i * k + 1
				print(' '.join((str(i * (k * 2) + x) for x in range(1, k * 2 + 1, 2))))
				print(' '.join((str(i * (k * 2) + x) for x in range(2, k * 2 + 1, 2))))
	else:
		print('NO')


def produce(n, k):
	if k == 1:
		for shelf in range(n):
			print(shelf + 1)
		return
	odds = [c for c in range(1, n * k + 1, 2)]
	evens = [c + 1 for c in range(1, n * k, 2)]
	for shelf in range(n // 2):
		print(' '.join((str(e) for e in odds[shelf * k:(shelf + 1) * k])))
	for shelf in range(n // 2):
		print(' '.join((str(e) for e in evens[shelf * k:(shelf + 1) * k])))

def solve(n, k):
	if k == 1:
		return 'YES'
	if n * k % 2 != 0:
		return 'NO'
	if n % 2 != 0:
		return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	(n, k) = (int(component) for component in input().split(' '))
	result = solve(n, k)
	print(result)
	if result == 'YES':
		produce(n, k)


t = int(input())
for line in range(t):
	(n, k) = map(int, input().split())
	if n % 2 == 1 and k > 1:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			print(' '.join([str(i + 1 + n * j) for j in range(k)]))


import sys

def solve():
	inp = sys.stdin.readline
	(n, k) = map(int, inp().split())
	a = [[i + 1 + j * n for j in range(k)] for i in range(n)]
	s = 0
	for i in range(k):
		s += a[0][i]
		if s % (i + 1) != 0:
			print('NO')
			return
	print('YES')
	print('\n'.join([' '.join(map(str, i)) for i in a]))

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


import sys

def getline() -> str:
	return sys.stdin.readline().strip()

def solve():
	(n, k) = list(map(int, getline().split()))
	evens = list(range(2, n * k + 1, 2))
	odds = list(range(1, n * k + 1, 2))
	even_rows = len(evens) / k
	odd_rows = len(odds) / k
	if even_rows.is_integer() and odd_rows.is_integer():
		print('YES')
		(l, r) = (0, k)
		evens = iter(evens)
		odds = iter(odds)
		mtx = [[] for _ in range(n)]
		for i in range(1, n + 1):
			if i % 2 == 1:
				for _ in range(k):
					mtx[i - 1].append(next(odds))
			else:
				for _ in range(k):
					mtx[i - 1].append(next(evens))
		for row in mtx:
			print(' '.join(map(str, row)))
	else:
		print('NO')
t = int(getline())
for _ in range(t):
	solve()


import sys

def printMatriz(n, k):
	for i in range(n):
		aux = i + 1
		ret = ''
		for j in range(k):
			ret += str(aux) + ' '
			aux += n
		print(ret.strip())
l = sys.stdin.readlines()
t = int(l[0].replace('\n', ''))
for i in range(1, t + 1):
	(n, k) = [int(x) for x in l[i].replace('\n', '').split()]
	ehPossivel = False
	if k == 1 or n % 2 == 0:
		ehPossivel = True
	if ehPossivel:
		print('YES')
		printMatriz(n, k)
		continue
	print('NO')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	ans = [[0] * k for _ in range(n)]
	c = 1
	for i in range(k):
		for j in range(n):
			ans[j][i] = c
			c += 1
	ok = True
	s = 0
	for i in range(k):
		s += ans[0][i]
		if s % (i + 1) != 0:
			ok = False
	if ok:
		print('YES')
		for a in ans:
			print(' '.join(map(str, a)))
	else:
		print('NO')


from collections import Counter
from queue import Queue
import math
T = int(input())
for t in range(T):
	(n, k) = [int(k) for k in input().split()]
	if n % 2 == 1:
		if k > 1:
			print('NO')
		else:
			print('YES')
			for idx in range(n):
				print(1 + idx)
	else:
		print('YES')
		for jdx in range(n // 2):
			print(' '.join([str(2 * idx + 1 + 2 * k * jdx) for idx in range(k)]))
			print(' '.join([str(2 * idx + 2 + 2 * k * jdx) for idx in range(k)]))


def pallindrome(s: str):
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True

def find_matrix_and_print(n: int, k: int):
	evens = iter(range(2, n * k + 1, 2))
	odds = iter(range(1, n * k + 1, 2))
	evenFill = True
	i = 0
	while i < n:
		arr = []
		if evenFill:
			for j in range(k):
				even = next(evens, None)
				if even:
					arr.append(even)
			evenFill = False
		else:
			for j in range(k):
				odd = next(odds, None)
				if odd:
					arr.append(odd)
			evenFill = True
		if len(arr) > 0:
			i += 1
			print(' '.join([str(x) for x in arr]))

def main():
	T = input()
	for _ in range(int(T)):
		l1 = input()
		n = int(l1.split()[0])
		k = int(l1.split()[1])
		if n == 1:
			if k == 1:
				print('YES')
				print(1)
			else:
				print('NO')
			continue
		if k == 1 or n % 2 == 0:
			print('YES')
			find_matrix_and_print(n, k)
			continue
		else:
			print('NO')
main()


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(n):
			print(i + 1)
	elif n % 2 == 1:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			if i < n / 2:
				print(' '.join([str(2 * (k * i + j) + 1) for j in range(k)]))
			else:
				print(' '.join([str(2 * (int(k * (i - n / 2)) + j) + 2) for j in range(k)]))


def main():
	(n, k) = map(int, input().split())
	if k == 1:
		return 'YES\n' + '\n'.join((str(i + 1) for i in range(n)))
	elif n % 2 == 1 and k >= 2:
		return 'NO'
	return 'YES\n' + '\n'.join((' '.join((str(2 * s * k + 2 * i + j + 1) for i in range(k))) for s in range(n // 2) for j in [0, 1]))
cases = int(input())
for _ in range(cases):
	print(main())


def fill_mass(n, k):
	odd = 1
	while odd <= n * k:
		odd_str = ''
		for j in range(k):
			odd_str += str(odd) + ' '
			odd += 2
		print(odd_str[:-1])
	even = 2
	while even <= n * k:
		even_str = ''
		for j in range(k):
			even_str += str(even) + ' '
			even += 2
		print(even_str[:-1])

def okeya():
	number_of_ids = int(input())
	input_list = []
	for i in range(number_of_ids):
		input_list.append(input().split())
	for i in input_list:
		n_k = i
		n = int(n_k[0])
		k = int(n_k[1])
		if k == 1:
			print('YES')
			fill_mass(n, k)
			continue
		if n * k % 2 == 0 and n * k // 2 % k == 0:
			print('YES')
			fill_mass(n, k)
			continue
		print('NO')
okeya()


t = int(input())
for x in range(t):
	(n, k) = map(int, input().split(' '))
	if k == 1:
		print('YES')
		for y in range(n):
			print(y + 1)
	elif n * k % 2 == 0 and n % 2 == 0:
		print('YES')
		arr = [[0 for i in range(k)] for j in range(n)]
		cou = 1
		for q in range(k):
			for z in range(n):
				arr[z][q] = str(cou)
				cou += 1
		for b in range(n):
			print(' '.join(arr[b]))
	else:
		print('NO')


t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	if k == 1:
		print('YES')
		for i in range(n):
			print(i + 1)
	elif n % 2 != 0:
		print('NO')
	else:
		print('YES')
		val = 0
		for j in range(n // 2):
			print(' '.join([str(val + 1 + 2 * jj) for jj in range(k)]))
			print(' '.join([str(val + 2 + 2 * jj) for jj in range(k)]))
			val = val + k * 2


def ga():
	return list(map(int, input().split()))
for _ in range(int(input())):
	(n, k) = ga()
	if k == 1:
		print('YES')
		for i in range(n):
			print(i + 1)
		continue
	odds = [i for i in range(1, n * k + 1) if i % 2]
	evens = [i for i in range(1, n * k + 1) if i % 2 == 0]
	if len(odds) != len(evens) or len(odds) % k != 0 or len(odds) // k != n // 2:
		print('NO')
		continue
	print('YES')
	ans = []
	i = 0
	while i < len(odds):
		ans += [odds[i:i + k]]
		ans += [evens[i:i + k]]
		i += k
	for i in ans:
		i = list(map(str, i))
		print(' '.join(i))


t = int(input())
for _ in range(0, t):
	inputs = [int(num) for num in input().split()]
	n = inputs[0]
	k = inputs[1]
	if n == 1 and k == 1:
		print('YES')
		print('1')
	elif n == 1 and k > 1:
		print('NO')
	elif n % 2 == 1 and k > 1:
		print('NO')
	elif k == 1:
		print('YES')
		i = 1
		for i in range(1, n + 1):
			print(i)
	else:
		print('YES')
		rows = 0
		max_num = n * k
		i = 0
		kk = 1
		for i in range(0, int(n / 2)):
			s = ''
			j = 0
			for j in range(0, k):
				s += str(kk) + ' '
				kk += 2
			print(s.strip())
		kk = 2
		i = 0
		for i in range(0, int(n / 2)):
			s = ''
			j = 0
			for j in range(0, k):
				s += str(kk) + ' '
				kk += 2
			print(s.strip())


for i in range(int(input())):
	(n, k) = list(map(int, input().split()))
	m = n * k
	a = 1
	b = 2
	if m % 2 == 0:
		f = int(m / 2)
	else:
		f = int((m + 1) / 2)
	if f % k != 0:
		print('NO')
	else:
		print('YES')
		for i in range(1, n + 1):
			d = ''
			if i % 2 == 0:
				for i in range(k):
					d += str(b) + ' '
					b += 2
				print(d)
			else:
				for i in range(k):
					d += str(a) + ' '
					a += 2
				print(d)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(n):
			print(i + 1)
	elif n % 2 == 1:
		print('NO')
	else:
		print('YES')
		for i in range(n):
			line = []
			for j in range(k):
				if i % 2 == 0:
					line.append(1 + i * k + 2 * j)
				else:
					line.append(2 + (i - 1) * k + 2 * j)
			print(' '.join((str(num) for num in line)))


def solve(n, k):
	a = n * k
	odd = int(a // 2)
	even = int(a // 2)
	if a % 2:
		odd += 1
	if odd % k == 0 and even % k == 0:
		res = []
		j = 0
		for i in range(1, n + 1):
			if i % 2:
				res.append([2 * k * j + 2 * p + 1 for p in range(k)])
			else:
				res.append([2 * k * j + 2 * p + 2 for p in range(k)])
				j += 1
		return (True, res)
	return (False, [])
out = []
t = int(input())
for case in range(t):
	(n, k) = list(map(int, input().split(' ')))
	(res, answ) = solve(n, k)
	if res:
		out.append('YES')
		out.append('\n'.join([' '.join(map(str, el)) for el in answ]))
	else:
		out.append('NO')
print('\n'.join(out))


t = int(input())
for i in range(t):
	arr1 = list(map(int, input().rstrip().split()))
	n = arr1[0]
	k = arr1[1]
	a = n * k
	if 1 == 1:
		m = 1
		m1 = 2
		arr = []
		h = 0
		f = 0
		for i in range(a):
			if m <= a:
				arr.append(str(m))
				m += 2
			else:
				if i % k != 0 and f == 0:
					print('NO')
					h = 1
					break
				f = 1
				arr.append(str(m1))
				m1 += 2
		if h == 1:
			g = 1
		else:
			print('YES')
			for i in range(n):
				print(' '.join(arr[0 + i * k:k + i * k]))


import math

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

def stringlsit():
	return input().split(' ')

def solve():
	(n, k) = intlist()
	if n % 2 and k > 1:
		return 'NO'
	ans = 'YES'
	for i in range(n):
		l = []
		for j in range(k):
			l.append(str(j * n + i + 1))
		ans += '\n' + ' '.join(l)
	return ans
t = input()
for tt in range(int(t)):
	print(solve())


def solve():
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
		return
	if n % 2 == 1:
		print('NO')
		return
	print('YES')
	for i in range(1, n + 1):
		print(*[i for i in range(i, n * k + 1, n)])
for _ in range(int(input())):
	solve()


for s in [*open(0)][1:]:
	(n, k) = map(int, s.split())
	f = k > 1 == n % 2
	print('YNEOS'[f::2])
	for i in range(~-f & n):
		print(*range(i + 1, n * k + 1, n))


for _ in [0] * int(input()):
	(n, k) = list(map(int, input().split()))
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
		continue
	elif n % 2 == 0:
		print('YES')
		for i in range(n):
			print(*range(i + 1, k * n + 1, n))
	else:
		print('NO')


import io
import os
import sys

def main():
	t = int(input())
	q = 500 ** 2
	arr = [x for x in range(1, q + 1, 2)] + [x for x in range(2, q + 1, 2)]
	for _ in range(t):
		(n, k) = map(int, input().split())
		if k == 1:
			ans = 'YES\n'
			for x in range(1, n * k + 1):
				ans += f'{x}\n'
			sys.stdout.write(ans)
			continue
		p = n * k
		if p % 2 or n == 1 or n % 2:
			sys.stdout.write('NO\n')
		else:
			i = 0
			j = q // 2
			ans = 'YES\n'
			for a in range(n):
				if a % 2 == 0:
					for _ in range(k):
						ans += f'{arr[i]} '
						i += 1
				else:
					for _ in range(k):
						ans += f'{arr[j]} '
						j += 1
				ans += '\n'
			sys.stdout.write(ans)
main()


I = lambda : map(int, input().split())
for _ in range(int(input())):
	(n, k) = I()
	if k == 1:
		print('YES')
		i = 1
		while i <= n * k:
			print(*list(range(i, i + k, 1)))
			i = i + k
	elif n == 1:
		print('NO')
	elif n % 2 == 0:
		print('YES')
		x = list(range(1, n * k + 1, 2))
		y = list(range(2, n * k + 1, 2))
		for i in range(0, len(x), k):
			print(*x[i:i + k])
		for i in range(0, len(y), k):
			print(*y[i:i + k])
	else:
		print('NO')


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
	elif n % 2 == 1:
		print('NO')
	else:
		print('YES')
		for i in range(1, n + 1):
			print(*[i for i in range(i, n * k + 1, n)])
	t -= 1


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
	elif n % 2 == 1:
		print('NO')
	else:
		print('YES')
		numbers = list(range(1, n * k + 1, 2)) + list(range(2, n * k + 1, 2))
		start = 0
		for i in range(n):
			print(*numbers[start:start + k])
			start += k


t = int(input())
for x in range(t):
	(n, k) = [int(i) for i in input().split(' ')]
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
	elif n % 2 == 1:
		print('NO')
	else:
		print('YES')
		for i in range(1, n + 1):
			print(*[j for j in range(i, n * k + 1, n)])


cas = int(input())
for i in range(cas):
	(n, k) = [int(x) for x in input().split()]
	if k != 1:
		if n % 2 != 0:
			print('NO')
		else:
			print('YES')
			for j in range(1, n + 1):
				print(*range(j, k * n + j, n))
	else:
		print('YES')
		l = list(range(1, n + 1))
		for r in l:
			print(r)


import sys

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

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

def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		if k == 1:
			print('YES')
			for i in range(1, n + 1):
				print(i)
		elif n % 2 == 1:
			print('NO')
		else:
			print('YES')
			for i in range(1, n + 1):
				print(*[j for j in range(i, n * k + 1, n)])
main()


t = int(int(input()))
for _ in range(t):
	(n, m) = map(int, input().split())
	odd = True
	prev = 0
	lst = []
	for i in range(n):
		lst.append(list(range(prev + 1, prev + 1 + 2 * m, 2)))
		if odd:
			prev = lst[-1][0]
			odd = False
		else:
			prev = lst[-1][-1]
			odd = True
	if lst[-1][-1] > n * m:
		print('NO')
	else:
		print('YES')
		for i in lst:
			print(*i)


for _ in range(0, int(input())):
	(n, k) = map(int, input().split())
	if n % 2 == 0 or k == 1:
		print('YES')
		if k == 1:
			for i in range(1, n + 1):
				print(i)
		else:
			for i in range(0, n * k, 2 * k):
				print(*range(i + 1, i + k * 2 + 1, 2))
				print(*range(i + 2, i + k * 2 + 2, 2))
	else:
		print('NO')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
		continue
	if n % 2:
		print('NO')
		continue
	print('YES')
	for i in range(1, n + 1):
		print(*[i for i in range(i, n * k + 1, n)])


for t in range(int(input())):
	(a, b) = map(int, input().split())
	if b == 1:
		print('YES')
		for k in range(1, a + 1):
			print(k)
	elif a == 1:
		print('NO')
	elif a * b % 2 == 0 and a % 2 == 0:
		print('YES')
		for i in range(a // 2):
			print(*list(range(1 + i * b * 2, (i + 1) * b * 2 + 1, 2)))
			print(*list(range(2 + i * b * 2, (i + 1) * b * 2 + 2, 2)))
	else:
		print('NO')


def solve():
	(n, k) = [int(x) for x in input().split()]
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
		return
	if n % 2 != 0:
		print('NO')
		return
	r = []
	print('YES')
	for i in range(2, n * k + 1, 2):
		r.append(i)
	for i in range(1, n * k + 1, 2):
		r.append(i)
	for s in range(0, n * k, k):
		print(*r[s:s + k])
t = int(input())
for _ in range(t):
	solve()


def printShelves(odd, even, k):
	for i in range(0, len(odd), k):
		st = f'{odd[i]}'
		for j in range(1, k):
			st += f' {odd[i + j]}'
		print(st)
	for i in range(0, len(even), k):
		st = f'{even[i]}'
		for j in range(1, k):
			st += f' {even[i + j]}'
		print(st)
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	its = n * k
	exists = True
	aux = []
	for i in range(1, its + 1):
		aux.append(i)
	even = []
	odd = []
	for i in range(its):
		if aux[i] % 2:
			odd.append(aux[i])
		else:
			even.append(aux[i])
	if k > 1:
		if its % 2:
			exists = False
		elif len(even) % k:
			exists = False
	if exists:
		print('YES')
		if k == 1:
			for i in range(1, n + 1):
				print(i)
		else:
			printShelves(even, odd, k)
	else:
		print('NO')


def okea():
	(n, k) = [int(x) for x in input().split()]
	if k == 1:
		print('YES')
		for i in range(1, n + 1):
			print(i)
		return
	if n % 2 == 1:
		print('NO')
		return
	print('YES')
	for i in range(1, n + 1):
		print(*[x for x in range(i, n * k + 1, n)])
cases = int(input())
for case in range(cases):
	okea()