We need to solve problem: given two arrays a (length n) and b (length m), find a non-empty array c that is a subsequence of both a and b, with the smallest possible length. If multiple minimal length, any. If none, output NO. Constraints: t up to 1000, sum of n and sum of m across tests ≤ 1000. So total size small.
Observations: The smallest possible length is 1, if there is any common element between a and b. Because a single element is a subsequence of both. So if there exists x such that x appears in a and in b, then we can output [x] and length 1. That would be minimal (since length must be at least 1). So answer is YES with length 1 if the intersection of sets of elements of a and b is non-empty. If there is no common element, then we need to see if we can find a common subsequence of length at least 2? But if there is no common element, there is no common subsequence of any length, because any subsequence's elements are taken from the arrays; if no element appears in both arrays, then there cannot be any common subsequence (since the elements themselves need to be present in both). Wait, careful: A common subsequence means the sequence of values (not indices) appears as a subsequence in both. The values must be elements from both arrays. If there is no value that appears in both arrays, then any common subsequence would have to consist of values that appear in both? Actually, if a value v is in the subsequence c, then v must be present in a (since c is subsequence of a) and in b (since c is subsequence of b). So each element of c must belong to the intersection of the sets of values from a and b. So if the intersection is empty, there is no common subsequence at all (except maybe if arrays are empty but they are non-empty? Problem requires non-empty c). So answer would be NO. So essentially, the problem reduces to: check if there is any common value in a and b. If yes, output YES and any such common value (single element). That yields minimal length 1.
But wait, is there any scenario where the smallest length is >1 even though there is a common element? No, because 1 is minimal possible length; if a common element exists, then we can produce a common subsequence of length 1. So that is always optimal. So answer is simply: if there exists any number that appears in both arrays, output YES and that number as c of length 1. Otherwise NO.
But is that always correct? Let's double-check the problem statement: "Your task is to find a non-empty array c_1,…,c_k that is a subsequence of a_1,…,a_n, and also a subsequence of b_1,…,b_m. If there are multiple answers, find one of the smallest possible length." So indeed, if any common element exists, length 1 is possible and thus smallest. So trivial solution.
But wait, constraints: The problem originally may be more complex? Actually, there might be a nuance: The arrays have elements from 1 to 1000. The sums of lengths up to 1000 total. So it's very easy.
But why did the sample output for test case 5 output "YES\n1 2"? Both arrays are [1,2,3,4,5], so any common element works; they chose 2. That's consistent.
Test case 4: a = [1000,2,2,2,3], b = [3,1,5]. Intersection: 3 appears in both? a has 3, b has 3, yes. So output 3. That's sample output: YES 1 3.
Test case 1: a=[10,8,6,4], b=[1,2,3,4,5] -> common 4 -> output 4. Works.
Test case 2: both [3] and [3] -> common 3 -> output 3.
Test case 3: [3] and [2] -> no common -> NO.
Thus problem is trivial: find any common element.
However, is there any trick like if they require the subsequence to be in order? But we're only taking a single element, order doesn't matter.
Therefore solution: For each test case, read n, m, arrays a and b. Create a set (or boolean array of size 1001) for a's elements. Then iterate over b to find any element present in that set. If found, output YES, 1, that element. Else NO.
But wait: The sum of n and m across test cases ≤ 1000, but we also have t up to 1000. So we need to handle many small test cases.
Edge cases: Could there be case where a and b share no element but there is a common subsequence of length >1? No, because each element of subsequence must be an element present in both arrays. If there is no common element, then it's impossible. So answer NO.
Thus solution is straightforward.
But maybe the problem originally had additional twist? The problem statement says: "If there are multiple answers, find one of the smallest possible length." That is satisfied by length 1 if exists.
Thus answer is trivial.
But why would they provide constraints that sum of lengths ≤ 1000? It seems too small; maybe they intended a more complex solution (like shortest common supersequence? No, it's common subsequence). But it's indeed that easy.
We should confirm with an example where arrays have overlapping values but in different order: e.g., a=[1,2], b=[2,1]. Intersection includes 1 and 2, so length 1 works. So answer is YES 1 1 or 2.
Thus solution is correct.
We'll implement accordingly.
Complexity: O(n+m) per test case, total O(1000) overall.
Implementation details:
Read integer t. For each test case: read n, m. Read list a of n ints, list b of m ints. Use a boolean array seen[1001] = false. For each x in a, seen[x] = true. Then for each y in b, if seen[y] then output answer and break. If no break, output NO.
Note: The output format: For YES case: first line "YES". Second line: "k" followed by k integers. So we need to output "1 y" on the second line.
Make sure to flush output.
Edge: Possibly there might be multiple test cases with large t but tiny arrays. That's fine.
Thus final answer.
I'll write a Python solution.
But wait: The problem statement says: "If the answer is 'YES', on the next line output an integer k (1≤ k≤ 1000) — the length of the array, followed by k integers c_1,…,c_k (1≤ c_i≤ 1000) — the elements of the array." So we can output k=1.
Alright.
Thus final solution. ```python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
m = int(next(it))
a = [int(next(it)) for __ in range(n)]
b = [int(next(it)) for __ in range(m)]
# mark numbers present in a
seen = [False] * 1001 # values are between 1 and 1000
for x in a:
seen[x] = True
# search for a common element in b
found = None
for y in b:
if seen[y]:
found = y
break
if found is None:
out_lines.append("NO")
else:
out_lines.append("YES")
out_lines.append(f"1 {found}")
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()