Box Factory (Small)

Match boxes and toys from two run-length encoded lines in order to maximize the number of equal-type pairs.

Medium6Dynamic programmingNo attempts yetTime limit5sMemory limit512 MB

Problem

You run a factory with two assembly lines. The first line makes boxes, and the second line makes the toys that go into those boxes. Each box type takes exactly one toy type, and each toy type fits exactly one box type.

At the start you pick up a box from the first line and a toy from the second line. While you hold one box and one toy, you can do one of the following.

  • Throw away the box you hold and pick up the next box.
  • Throw away the toy you hold and pick up the next toy.
  • If the box and the toy have the same type, put the toy in the box and send it out to a customer.

You pick up boxes in the order they were made, and you pick up toys in the order they were made. Given the production order of both lines, find the largest number of boxed toys you can send out.

The two lines make a great many boxes and toys. Each line keeps making one type for a long stretch before it switches to another type, so the production order is given as runs of one type.

Input

The first line contains the number of test cases TT. Each test case takes three lines.

The first line contains two integers NN and MM. The second line contains 2N2N integers a1a_1, A1A_1, a2a_2, A2A_2, ..., aNa_N, ANA_N. The third line contains 2M2M integers b1b_1, B1B_1, b2b_2, B2B_2, ..., bMb_M, BMB_M.

The first line makes a1a_1 boxes of type A1A_1, then a2a_2 boxes of type A2A_2, and finally aNa_N boxes of type ANA_N. In the same way, the second line makes b1b_1 toys of type B1B_1 through bMb_M toys of type BMB_M. A toy goes into a box if and only if their type numbers are equal.

Two neighboring runs can have the same type number.

Limits

  • 1T1001 \le T \le 100
  • 1N31 \le N \le 3
  • 1M1001 \le M \le 100
  • 1ai,bi10161 \le a_i, b_i \le 10^{16}
  • 1Ai,Bi1001 \le A_i, B_i \le 100

Output

For each test case, print one line in the form "Case #x: y", where xx is the test case number starting from 1 and yy is the largest number of boxed toys you can send out.