def solve(): n, m = map(int, input().split()) FLIP = False def v2(x): res = 0 while x % 2 == 0: res += 1 x //= 2 return res if v2(n) + v2(m) <= 2: print("-1") return if v2(n) > v2(m): n, m = m, n FLIP = True cnt = 0 a = [[0] * m for _ in range(n)] B = [[1, 1, 1, 2], [1, 2, 2, 2]] def set_B(si, sj): nonlocal cnt for di in range(2): for dj in range(4): a[si + di][sj + dj] = cnt + B[di][dj] cnt += 2 C = [ [1, 1, 3, 3, 3, 4, 6, 6], [1, 2, 3, 4, 4, 4, 5, 6], [1, 2, 2, 2, 5, 5, 5, 6] ] def set_C(si, sj): nonlocal cnt for di in range(3): for dj in range(8): a[si + di][sj + dj] = cnt + C[di][dj] cnt += 6 # case (II) if v2(n) >= 1 and v2(m) >= 2: k = n * m // 4 for i in range(n // 2): for j in range(m // 4): set_B(i * 2, j * 4) print(k) if FLIP: a_old = [[0] * n for _ in range(m)] for i in range(n): for j in range(m): a_old[j][i] = a[i][j] a, n, m = a_old, m, n for row in a: print(" ".join(map(str, row))) return # case (III) if v2(n) == 0 and v2(m) >= 3: if n == 1: print("-1") return k = n * m // 4 for i in range(n // 2 - 1): for j in range(m // 4): set_B(i * 2, j * 4) for j in range(m // 8): set_C(n - 3, j * 8) print(k) if FLIP: a_old = [[0] * n for _ in range(m)] for i in range(n): for j in range(m): a_old[j][i] = a[i][j] a, n, m = a_old, m, n for row in a: print(" ".join(map(str, row))) return assert False def main(): t = int(input()) for _ in range(t): solve() if __name__ == "__main__": main()