P4752 Divided Prime

Description

Given a number $A$, where $A$ is obtained by multiplying $a_1, a_2, \cdots, a_N$. Given a number $B$, where $B$ is obtained by multiplying $b_1, b_2, \cdots, b_M$. If $\frac{A}{B}$ is a prime number, output `YES`; otherwise, output `NO`.

Input Format

Each test point contains multiple test cases. The first line contains an integer $T$, which denotes the number of test cases. For each test case: The first line contains two integers $N, M$, indicating that $A$ is obtained by multiplying $N$ numbers, and $B$ is obtained by multiplying $M$ numbers. The second line contains $N$ integers, which are the $N$ numbers that make up $A$. The third line contains $M$ integers, which are the $M$ numbers that make up $B$. It is guaranteed that, for any number, the number of times it appears in ${b_i}$ is no more than the number of times it appears in ${a_i}$.

Output Format

For each test case: If $\frac{A}{B}$ is a prime number, output `YES`; otherwise, output `NO`. After outputting `YES` or `NO`, output a newline character.

Explanation/Hint

For all testdata, $1 \le N \le 10^5$, $0 \le M \le N$, $1 \le a_i, b_i \le 10^{12}$, $1 \le T \le 10$, and $\sum N \le 10^5$. Translated by ChatGPT 5