OpenKattis
Set #10

Start

2018-03-28 05:00 AKDT

Set #10

End

2018-04-04 04:59 AKDT
The end is near!
Contest is over.
Not yet started.
Contest is starting in -1181 days 1:13:32

167:59:59

0:00:00

Problem GPrince and Princess

On an $n \times n$ chessboard, the Prince and the Princess play a game. The squares on the chessboard are numbered $1, 2, 3, \ldots , n^2$, as shown in Figure 1:

The Prince stands in square $1$, makes $p$ jumps and finally reaches square $n^2$. He enters a square at most once. So if we use $x_ i$ to denote the $i$:th square he enters, then $x_1, x_2, \ldots , x_{p+1}$ are all different. Note that $x_{1}=1$ and $x_{p+1}=n^2$.

The Princess does essentially the same thing – stands in square $1$, makes $q$ jumps and fainally reaches square $n^2$. We use $y_{1}, y_{2}, \ldots y_{q+1}$ to denote the sequence, and all $q+1$ numbers are different.

Figure 2 below shows a $3 \times 3$ square, a possible route for the Prince and a different route for the Princess.

The Prince moves along the sequence: $1 \rightarrow 7 \rightarrow 5 \rightarrow 4 \rightarrow 8 \rightarrow 3 \rightarrow 9$ (Black arrows), while the Princess moves along this sequence: $1 \rightarrow 4 \rightarrow 3 \rightarrow 5 \rightarrow 6 \rightarrow 2 \rightarrow 8 \rightarrow 9$ (White arrows).

The King, their father, has just come. “Why move separately? You are brother and sister!”, said the King, “Ignore some jumps and make sure that you’re always together.”

For example, if the Prince ignores his 2:nd, 3:rd, and 6:th jump, he’ll follow the route: $1 \rightarrow 4 \rightarrow 8 \rightarrow 9$. If the Princess ignores her 3:rd, 4:th, 5:th, 6:th jump, she’ll follow the same route: $1 \rightarrow 4 \rightarrow 8 \rightarrow 9$, (The common route is shown in Figure 3) thus satisfying the King. The King wants to know the longest route they can move together. Could you tell him?

Input

The first line of the input contains a single integer $t$, the number of test cases to follow. For each case, the first line contains three integers $n, p, q$ ($2 \le n \le 250, 1 \le p, q < n^2$). The second line contains $p+1$ different integers in the range $[1 \ldots n^2]$, the sequence of the Prince. The third line contains $q+1$ different integers in the range $[1 \ldots n^2]$, the sequence of the Princess.

Output

For each test case, print the case number and the length of longest route they can move together. Look at the output for sample input for details.

Sample Input 1 Sample Output 1
1
3 6 7
1 7 5 4 8 3 9
1 4 3 5 6 2 8 9

Case 1: 4