for _ in range(int(input())): N, M = map(int, input().split()) if N >= 3 and N % 2 == 1 and M % 8 == 0: print(N * M // 4) ans = [[0] * M for _ in range(N)] c = 1 for i in range(0, N - 3, 2): for j in range(0, M, 4): ans[i][j] = c ans[i][j + 1] = c ans[i][j + 2] = c ans[i][j + 3] = c + 1 ans[i + 1][j] = c ans[i + 1][j + 1] = c + 1 ans[i + 1][j + 2] = c + 1 ans[i + 1][j + 3] = c + 1 c += 2 for j in range(0, M, 8): ans[N - 3][j] = c ans[N - 3][j + 1] = c + 1 ans[N - 3][j + 2] = c + 1 ans[N - 3][j + 3] = c + 1 ans[N - 3][j + 4] = c + 2 ans[N - 3][j + 5] = c + 2 ans[N - 3][j + 6] = c + 2 ans[N - 3][j + 7] = c + 3 ans[N - 2][j] = c ans[N - 2][j + 1] = c + 1 ans[N - 2][j + 2] = c + 4 ans[N - 2][j + 3] = c + 5 ans[N - 2][j + 4] = c + 5 ans[N - 2][j + 5] = c + 5 ans[N - 2][j + 6] = c + 2 ans[N - 2][j + 7] = c + 3 ans[N - 1][j] = c ans[N - 1][j + 1] = c ans[N - 1][j + 2] = c + 4 ans[N - 1][j + 3] = c + 4 ans[N - 1][j + 4] = c + 4 ans[N - 1][j + 5] = c + 5 ans[N - 1][j + 6] = c + 3 ans[N - 1][j + 7] = c + 3 c += 6 for i in range(N): print(*ans[i]) continue if M >= 3 and M % 2 == 1 and N % 8 == 0: print(N * M // 4) ans = [[0] * M for _ in range(N)] c = 1 for i in range(0, N, 4): for j in range(0, M - 3, 2): ans[i][j] = c ans[i][j + 1] = c ans[i + 1][j] = c ans[i + 1][j + 1] = c + 1 ans[i + 2][j] = c ans[i + 2][j + 1] = c + 1 ans[i + 3][j] = c + 1 ans[i + 3][j + 1] = c + 1 c += 2 for i in range(0, N, 8): ans[i][M - 3] = c ans[i][M - 2] = c ans[i][M - 1] = c ans[i + 1][M - 3] = c ans[i + 1][M - 2] = c + 1 ans[i + 1][M - 1] = c + 1 ans[i + 2][M - 3] = c + 4 ans[i + 2][M - 2] = c + 4 ans[i + 2][M - 1] = c + 1 ans[i + 3][M - 3] = c + 4 ans[i + 3][M - 2] = c + 5 ans[i + 3][M - 1] = c + 1 ans[i + 4][M - 3] = c + 4 ans[i + 4][M - 2] = c + 5 ans[i + 4][M - 1] = c + 2 ans[i + 5][M - 3] = c + 5 ans[i + 5][M - 2] = c + 5 ans[i + 5][M - 1] = c + 2 ans[i + 6][M - 3] = c + 3 ans[i + 6][M - 2] = c + 2 ans[i + 6][M - 1] = c + 2 ans[i + 7][M - 3] = c + 3 ans[i + 7][M - 2] = c + 3 ans[i + 7][M - 1] = c + 3 c += 6 for i in range(N): print(*ans[i]) continue if N % 4 == 0 and M % 2 == 0: print(N * M // 4) 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]) continue elif N % 2 == 0 and M % 4 == 0: print(N * M // 4) 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]) continue print(-1)