Longest Common Subsequence

아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

Chiaki has two sequences a_1,a_2,,a_na\_1,a\_2,\ldots,a\_n and b_1,b_2,,b_mb\_1,b\_2,\ldots,b\_m. She would like to find their longest common subsequence c_1,c_2,,c_kc\_1,c\_2,\ldots,c\_k such that c_1c_2c_kc\_1 \le c\_2 \le \ldots \le c\_k.

입력

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m1061 \le n, m \le 10^6): the lengths of two sequences.

The second line contains nn integers a_1,a_2,,a_na\_1,a\_2,\ldots,a\_n (1a_i31 \le a\_i \le 3).

The third line contains mm integers b_1,b_2,,b_mb\_1,b\_2,\ldots,b\_m (1b_i31 \le b\_i \le 3).

It is guaranteed that the sum of maxn,m\max\\{n,m\\} in all test cases does not exceed 10610^6.

출력

For each test case, output a single integer kk: the length of the longest common subsequence c_1,c_2,,c_kc\_1,c\_2,\ldots,c\_k such that c_1c_2c_kc\_1 \le c\_2 \le \ldots \le c\_k.