Construct an N \times M matrix with entries having positive integers such that:

If M > 1, i^{th} row is strictly increasing from left to right with a fixed common difference d_{i} for all 1≤ i ≤ N.
If N > 1, j^{th} column is strictly increasing from top to bottom with a fixed common difference c_{j} for all 1≤ j ≤ M.
All the common differences should be distinct.
In the case of multiple answers, find an arrangement that minimizes the maximum element of the matrix. 

------ Input Format ------ 

- First line will contain T, number of test cases. Then the test cases follow.
- Each test case contains a single line of input, two space-separated integers N and M - the dimensions of the matrix.

------ Output Format ------ 

For each test case, output N lines with each line having M space-separated integers representing the answer.

------ Constraints ------ 

$1 ≤ T ≤ 10$
$1 ≤ N, M ≤ 10^{5}$
- Sum $N \cdot M$ over all cases is atmost $10^{5}$.

----- Sample Input 1 ------ 
3
3 3
1 2
1 1

----- Sample Output 1 ------ 
1 2 3
3 6 9
5 10 15
1 2
1

----- explanation 1 ------ 
Test case $1$: The values $d_{1}, d_{2},$ and $d_{3}$ are $1, 3,$ and $5$ respectively. The values $c_{1}, c_{2},$ and $c_{3}$ are $2, 4,$ and $6$ respectively. Note that all these common differences $\{d_{1}, d_{2}, d_{3}, c_{1}, c_{2}, c_{3}\}$ are distinct. Moreover, it can be shown that over all valid matrices, $15$ is the minimal value of the maximum element of the matrix one can achieve.

Test case $2$: The values $d_{1}$ is $1$. It can be shown that over all valid matrices, $2$ is the minimal value of the maximum element of the matrix one can achieve.

Difficulty: MEDIUM
Tags: Mathematics

Starter code:

Solutions:
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ans = []
	if m == n:
		r = 1
		for i in range(n):
			mat = []
			c = r + 1
			ele = r
			for j in range(m):
				mat.append(ele)
				ele += r
			r += 2
			ans.append(mat)
	elif n < m:
		r = 0
		dif = n + 1
		ele = 1
		for i in range(1, n + 1):
			mat = []
			r = i
			for j in range(m):
				mat.append(ele)
				ele += r
			strt = mat[0]
			ele = strt + dif
			ans.append(mat)
	else:
		ele = 1
		for i in range(1, n + 1):
			mat = []
			dif = m + i
			for j in range(m):
				mat.append(ele)
				ele += dif
			strt = mat[0]
			ele = strt + 1
			ans.append(mat)
	for li in ans:
		for j in li:
			print(j, end=' ')
		print()


t = int(input())
while t:
	(n, m) = map(int, input().split())
	arr = [[0 for i in range(m)] for j in range(n)]
	val = 1
	r = 1
	c = 2
	if m > n:
		c = n + 1
	elif n > m:
		c = 1
		r = m + 1
	for i in range(n):
		temp = val
		for j in range(m):
			arr[i][j] = val
			val += r
		val = temp + c
		if n == m:
			r += 2
		else:
			r += 1
	for i in range(n):
		print(*arr[i])
	t -= 1


import math
import collections
import itertools
import functools

def fun(t):
	(n, m) = map(int, input().split(' '))
	ans = []
	dif = 0
	if m == n:
		for i in range(n):
			temp = []
			dif += 1
			temp.append(dif)
			for j in range(1, m):
				temp.append(dif * (j + 1))
			ans.append(temp)
			dif += 1
		for l in ans:
			print(*l)
	elif n > m:
		d = 2 * min(m, n) + abs(m - n)
		op = 1
		for i in range(n):
			temp = []
			for j in range(m):
				temp.append(op + d * j)
			print(*temp)
			op += min(m, n)
			d -= 1
	else:
		d = min(m, n)
		op = 1
		for i in range(n):
			temp = []
			for j in range(m):
				temp.append(op + d * j)
			print(*temp)
			op += 2 * min(m, n) + abs(m - n)
			d -= 1
for tc in range(int(input())):
	fun(tc)


def soln(n, m):
	arr = [[0] * m for _ in range(n)]
	if n == m:
		d = 1
		init = 1
		for i in range(n):
			arr[i][0] = init
			init += 2
			for j in range(1, m):
				arr[i][j] = arr[i][j - 1] + d
			d = init
	elif n > m:
		init = 1
		d = m + 1
		for i in range(n):
			arr[i][0] = init
			init += 1
			for j in range(1, m):
				arr[i][j] = arr[i][j - 1] + d
			d += 1
	else:
		init = 1
		d = n + 1
		for j in range(m):
			arr[0][j] = init
			init += 1
			for i in range(1, n):
				arr[i][j] = arr[i - 1][j] + d
			d += 1
	return arr
for _ in range(int(input())):
	(n, m) = map(int, input().split(' '))
	res = soln(n, m)
	for i in range(n):
		for j in range(m):
			print(res[i][j], end=' ')
		print()


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	arr = []
	for i in range(n):
		column = []
		for j in range(m):
			column.append(0)
		arr.append(column)
	if n == m:
		hdcc = 1
		vdcc = 2
		a = 1
		temp = a
		for i in range(n):
			for j in range(m):
				arr[i][j] = a
				print(arr[i][j], end=' ')
				a = a + hdcc
			a = temp
			a = a + vdcc
			temp = a
			hdcc += 2
			print('')
	elif n > m:
		a = 1
		vdcc = 1
		hdcc = m + 1
		temp = a
		for i in range(n):
			for j in range(m):
				arr[i][j] = a
				print(arr[i][j], end=' ')
				a = a + hdcc
			a = temp
			a = a + vdcc
			temp = a
			hdcc += 1
			print('')
	elif m > n:
		a = 1
		vdcc = n + 1
		hdcc = 1
		temp = a
		for i in range(n):
			for j in range(m):
				arr[i][j] = a
				print(arr[i][j], end=' ')
				a = a + hdcc
			a = temp
			a = a + vdcc
			temp = a
			hdcc += 1
			print('')


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	l = [[0 for i in range(n)] for j in range(m)]
	if m > n:
		c = 1
		r = n + 1
		for i in range(m):
			l[i][0] = c
			for j in range(1, n):
				l[i][j] = l[i][j - 1] + r
			r += 1
			c += 1
	elif m == n:
		c = 1
		for i in range(m):
			l[i][0] = c
			for j in range(1, n):
				l[i][j] = l[i][j - 1] + c
			c += 2
	else:
		r = 1
		x = 1
		for i in range(m):
			l[i][0] = x
			for j in range(1, n):
				l[i][j] = l[i][j - 1] + r
			r += 1
			x += m + 1
	for i in l:
		print(*i)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n > m:
		for i in range(n):
			ans = ''
			start = m * i + 1
			diff = m + n - i
			for j in range(m):
				d = start + diff * j
				ans = ans + ' ' + str(d)
			print(ans.strip())
	elif m > n:
		for i in range(n):
			ans = ''
			start = (m + n) * i + 1
			diff = n - i
			for j in range(m):
				d = start + diff * j
				ans = ans + ' ' + str(d)
			print(ans.strip())
	else:
		for i in range(0, n):
			ans = ''
			start = 1 + 2 * i
			diff = 1 + 2 * i
			for j in range(0, m):
				d = start + diff * j
				ans = ans + ' ' + str(d)
			print(ans.strip())


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	arr = []
	for i in range(n):
		col = []
		for j in range(m):
			col.append(0)
		arr.append(col)
	if n == m:
		hd = 1
		vd = 2
		a = 1
		temp = a
		for i in range(n):
			for j in range(m):
				arr[i][j] = a
				print(arr[i][j], end=' ')
				a = a + hd
			a = temp
			a = a + vd
			temp = a
			hd += 2
			print('')
	elif n > m:
		a = 1
		vd = 1
		hd = m + 1
		temp = a
		for i in range(n):
			for j in range(m):
				arr[i][j] = a
				print(arr[i][j], end=' ')
				a = a + hd
			a = temp
			a = a + vd
			temp = a
			hd += 1
			print('')
	elif m > n:
		a = 1
		vd = n + 1
		hd = 1
		temp = a
		for i in range(n):
			for j in range(m):
				arr[i][j] = a
				print(arr[i][j], end=' ')
				a = a + hd
			a = temp
			a = a + vd
			temp = a
			hd += 1
			print('')


t = int(input())
for i in range(t):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	(n, m) = (m, n)
	if m == n:
		s = 1
		d = 1
		stk = []
		for i in range(m):
			stk.append(s)
			s += 2
		for i in range(n):
			for j in range(m):
				print(stk[i], end=' ')
				stk[i] += d
			print('')
			d += 2
	elif m < n:
		stk = []
		stk.append(1)
		d = m + 2
		for i in range(1, m):
			stk.append(d)
			d += m + 1
		d = 1
		for i in range(m):
			for j in range(n):
				print(stk[i], end=' ')
				stk[i] += d
			print('')
			d += 1
	elif m > n:
		stk = []
		d = n + 1
		for i in range(1, m + 1):
			stk.append(i)
		for i in range(m):
			for j in range(n):
				print(stk[i], end=' ')
				stk[i] += d
			print('')
			d = d + 1


import math
from collections import Counter
from sys import maxsize
ints = lambda : list(map(int, input().split()))
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
m = 10 ** 9 + 7
inp = lambda : int(input())

def complementary():
	d = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
	n = inp()
	s = input()
	for x in s:
		if x in d:
			print(d[x], end='')
	print()

def get_max_count(arr):
	d = {}
	for x in arr:
		if x in d:
			d[x] += 1
		else:
			d[x] = 1
	return max(d.values())

def make_all_equal():
	n = inp()
	arr = ints()
	max_count = get_max_count(arr)
	print(n - max_count)

def count_acs():
	p = inp()
	n_hundred = p // 100
	n_single = p % 100
	if n_hundred + n_single <= 10:
		print(n_hundred + n_single)
	else:
		print(-1)

def is_prime(n):
	for x in range(2, int(n ** 0.5)):
		if n % x == 0:
			return False
	return True

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

def strong_elements():

	def get_prime_count(arr):
		count = 0
		for x in arr:
			if is_prime(x):
				count += 1
			if count == 2:
				return count
		return count
	n = inp()
	arr = ints()
	co_primes = 0
	gcd_of_n = arr[0]
	prev_gcd = gcd_of_n
	for x in arr:
		gcd_of_n = gcd(x, gcd_of_n)
		if gcd_of_n == 1:
			co_primes += 1
			gcd_of_n = prev_gcd
		if co_primes == 2:
			print(0)
			return
		prev_gcd = gcd_of_n
	if co_primes == 1:
		print(2)
	else:
		print(n)

def bubble_sort(arr, n, _max):
	for i in range(n - 1):
		flag = 0
		for j in range(n - i - 1):
			if arr[j] > arr[j + 1]:
				if arr[j] + arr[j + 1] <= _max:
					flag = 1
					(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
				else:
					return 'no'
		if not flag:
			return 'yes'
	return 'yes'

def reverse_sort():
	(n, x) = ints()
	A = ints()
	for i in range(len(A)):
		min_idx = i
		for j in range(i + 1, len(A)):
			if A[min_idx] > A[j]:
				min_idx = j
		if A[i] + A[min_idx] <= x or A[i] == A[min_idx]:
			(A[i], A[min_idx]) = (A[min_idx], A[i])
		else:
			return 'no'
	return 'yes'

def AP():
	(n, m) = ints()
	for row in range(n):
		if m == n:
			a = 2 * row + 1
			arr = [a * col for col in range(1, m + 1)]
		elif m < n:
			initial = row + 1
			diff = m + initial
			arr = [initial + col * diff for col in range(m)]
		else:
			initial = (n + 1) * row + 1
			diff = row + 1
			arr = [initial + col * diff for col in range(m)]
		print(*arr)
for _ in range(inp()):
	AP()


t = int(input())
for x in range(t):
	(N, M) = map(int, input().strip().split())
	matrix = [[0] * M for i in range(N)]
	if M > N:
		var = 1
		for i in range(N):
			for j in range(M):
				if j == 0:
					matrix[i][j] = var
				else:
					matrix[i][j] = matrix[i][j - 1] + (i + 1)
			var += N + 1
	if N > M:
		var = M + 1
		for i in range(N):
			for j in range(M):
				if j == 0:
					matrix[i][j] = i + 1
				else:
					matrix[i][j] = matrix[i][j - 1] + var
			var += 1
	elif N == M:
		var = 1
		for i in range(N):
			for j in range(M):
				matrix[i][j] = var * (j + 1)
			var += 2
	for i in matrix:
		for j in i:
			print(j, '', end='')
		print('\n', end='')