This is the hard version of the problem. The difference between the versions is that in this version, the limits on $$$t$$$, $$$n$$$ and $$$m$$$ are higher. You can hack only if you solved all versions of this problem.
In Gensokyo, Alice Margatroid and Marisa Kirisame are playing a strange game with enchanted pieces. Alice has $$$n$$$ pieces, Marisa has $$$m$$$ pieces, and every piece has an ability value. Both players choose their moves optimally.
Alice moves first, and then the players alternate turns. On a turn, a player may do one of two things:
The game ends immediately after a move if at least one player has no pieces left. If Alice has no pieces and Marisa still has pieces, Marisa wins. If Marisa has no pieces and Alice still has pieces, Alice wins. If both players have no pieces after the same move, the game is a draw. Also, if more than $$$10^{100}$$$ turns have passed without the game ending, the game is a draw.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$).
The second line of each case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the ability values of Alice's pieces.
The third line of each case contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le 10^9$$$) — the ability values of Marisa's pieces.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output one line containing A if Alice wins, B if Marisa wins, or C if the game is a draw.
52 23 12 12 23 14 12 24 14 13 24 3 34 43 24 4 34 4
ABCBC
In the first test case, Alice can use her piece of value $$$3$$$ to defeat all of Marisa's pieces.
In the third test case, the two players have the same pieces, so Marisa can always answer with the symmetric move, and the game is a draw.
In the fifth test case:
So $$$10^{100}$$$ turns pass without the game ending, and the game is a draw.