Under construction

Still being built. Things may change

Box Factory (Large)

Time limit5sMemory limit512 MB

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

Medium7 of 10

Topics
Dynamic programming, String matching
Solved
No attempts yet

Problem

You own a factory with two assembly lines. The first line makes boxes and the second line makes toys to put in those boxes. Every box and every toy carries a type number, and a toy fits a box only when the two type numbers are equal.

You start by picking up a box from the first line and a toy from the second line. After that you repeat the following actions as often as you like.

  • Throw out the box you are holding and pick up the next box.
  • Throw out the toy you are holding and pick up the next toy.
  • If the box and the toy have the same type, put the toy in the box, send it out to customers, then pick up the next box and the next toy.

You always pick boxes up in the order in which they are made, and the same holds for toys. You know the order in which both lines produce. Plan a strategy that sends out as many boxed toys as possible, and report that largest number.

The two lines make a lot of boxes and toys. They tend to make one kind of thing for a long time before switching.

Input

The first line of input contains the number of test cases TT.

Each test case begins with a line containing two integers NN and MM. The next line contains 2N2N integers a1,A1,a2,A2,…,aN,ANa_1, A_1, a_2, A_2, \dots, a_N, A_N, and the line after that contains 2M2M integers b1,B1,b2,B2,…,bM,BMb_1, B_1, b_2, B_2, \dots, b_M, B_M.

The first assembly line makes a1a_1 boxes of type A1A_1, then a2a_2 boxes of type A2A_2, and so on until it finishes with aNa_N boxes of type ANA_N. The second assembly line makes b1b_1 toys of type B1B_1 through bMb_M toys of type BMB_M in the same way. Two neighbouring runs may share a type number.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤ai,bi≤10161 \le a_i, b_i \le 10^{16}
  • 1≤Ai,Bi≤1001 \le A_i, B_i \le 100
  • 1≤N,M≤1001 \le N, M \le 100

Output

For each test case, output 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 to customers.

Examples2

  1. Example 1

    Input
    4
    3 3
    10 1 20 2 25 3
    10 2 30 3 20 1
    3 5
    10 1 6 2 10 1
    5 1 3 2 10 1 3 2 5 1
    3 5
    10 1 6 2 10 1
    5 1 6 2 10 1 6 2 5 1
    1 1
    5000000 10
    5000000 100
    
    Expected output
    Case #1: 35
    Case #2: 20
    Case #3: 21
    Case #4: 0
    
  2. Example 2

    Input
    2
    1 3
    6 1
    2 1 3 2 4 1
    2 2
    3 1 4 2
    4 2 3 1
    
    Expected output
    Case #1: 6
    Case #2: 4