for _ in range(int(input())): N, M = map(int, input().split()) if N * M % 4 != 0: print(-1) elif N % 4 == 0: if M % 2 == 0: K = N * M // 4 print(K) ans = [[0] * M for _ in range(N)] c = 1 for i in range(N // 4): for j in range(M // 2): ans[4 * i][2 * j] = c ans[4 * i][2 * j + 1] = c ans[4 * i + 1][2 * j] = c ans[4 * i + 1][2 * j + 1] = c + 1 ans[4 * i + 2][2 * j] = c ans[4 * i + 2][2 * j + 1] = c + 1 ans[4 * i + 3][2 * j] = c + 1 ans[4 * i + 3][2 * j + 1] = c + 1 c += 2 for i in range(N): print(*ans[i]) else: print(-1) elif N % 2 == 0: if M % 4 == 0: K = N * M // 4 print(K) ans = [[0] * M for _ in range(N)] c = 1 for i in range(N // 2): for j in range(M // 4): ans[2 * i][4 * j] = c ans[2 * i + 1][4 * j] = c ans[2 * i][4 * j + 1] = c ans[2 * i + 1][4 * j + 1] = c + 1 ans[2 * i][4 * j + 2] = c ans[2 * i + 1][4 * j + 2] = c + 1 ans[2 * i][4 * j + 3] = c + 1 ans[2 * i + 1][4 * j + 3] = c + 1 c += 2 for i in range(N): print(*ans[i]) else: print(-1) else: print(-1)