def solve(): n, m = map(int, input().split()) if n * m % 8 != 0 or n == 1 or m == 1: print(-1) return rev = False if n % 4 == 0: n, m = m, n rev = True A = [[0] * m for _ in range(n)] c = 0 if n % 2 == 1: S = [ [1, 2, 2, 2, 5, 5, 5, 6], [1, 2, 3, 4, 4, 4, 5, 6], [1, 1, 3, 3, 3, 4, 6, 6], ] for j in range(0, m, 8): for i in range(3): for k in range(8): A[i][j + k] = S[i][k] + c c += 6 s = 3 else: s = 0 S = [ [1, 1, 1, 2], [1, 2, 2, 2], ] for i in range(s, n, 2): for j in range(0, m, 4): for ii in range(2): for k in range(4): A[i + ii][j + k] = S[ii][k] + c c += 2 print(n * m // 4) if rev: B = [[0] * n for _ in range(m)] for i in range(n): for j in range(m): B[j][i] = A[i][j] for row in A: print(*row) for _ in range(int(input())): solve()