P2436 Designated

Description

Xiao S got the roster for the semifinals. The contestants on the list are arranged by repeating cycles of $A$ pros and $B$ noobs. For example, when $A = 2$, $B = 3$, contestants $1, 2, 6, 7, 11, 12\dots$ are pros, and contestants $3, 4, 5, 8, 9, 10, 13, 14, 15\dots$ are noobs. Now Xiao S has designated some contestants as pros and some as noobs, but she cannot construct such $A, B$. Can you help her?

Input Format

The first line contains an integer $T$, the number of test cases. For each test case: - The first line contains two integers $N, M$. - The second line contains $N$ distinct integers $a_1, a_2, \dots, a_N$, the contestant indices designated as pros. - The third line contains $M$ distinct integers $b_1, b_2, \dots, b_M$, the contestant indices designated as noobs.

Output Format

For each test case, output two integers $A, B$ (separated by a space) that satisfy the requirements. Among all valid pairs, choose the one with the smallest $A$, and if there are multiple, the one with the smallest $B$. If no such $A, B$ exist, output `NO`.

Explanation/Hint

Let there be $T$ test cases in a test file. - For $10\%$ of the testdata, $1 \le N, M \le 10$. - For $30\%$ of the testdata, $1 \le N, M \le 100$. - For $100\%$ of the testdata, $1 \le N, M \le 1000$, $1 \le T \le 10$, $1 \le a_i, b_i \le 10 \times (N + M)$. Translated by ChatGPT 5