Under construction

Still being built. Things may change

Touchbar Typing

Interview

Memory limit1024 MB

Summary
Given a word S and a one-row keyboard K, find the minimum finger movement time to type every character of S in order.
Level

Medium5 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

The Glide Typing task in the Crowdsource app uses a new Google keyboard to type a word by sliding a finger across keys without lifting it. See the animation below.

To make Glide Typing harder, the usual keyboard is replaced by a linear keyboard K\mathbf{K} whose keys are all in one row.

You want to type a word S\mathbf{S} that is N\mathbf{N} characters long. The keyboard K\mathbf{K} has M\mathbf{M} keys. The keys cover every character in S\mathbf{S}, but some keys may share a character. All characters and keys are integers.

You may start with your finger on any key. Moving your finger from a key to an adjacent key takes 1 second. Glide Typing has no pressing. If your finger is on key ii, which has character K_i\mathbf{K\_i}, and you want to type the character K_j\mathbf{K\_j} at index jj, you glide the finger from key ii to key jj, which takes ∣j−i∣\lvert j-i \rvert seconds. While your finger is on key xx, you can type K_x\mathbf{K\_x} any number of times instantly. You must type S\mathbf{S} one character at a time, so S_i\mathbf{S\_i} is typed before S_i+1\mathbf{S\_{i+1}} for each 1≤i≤N−11 \le i \le \mathbf{N}-1.

For example, let S\mathbf{S} be 1, 2, 2, 3, 4. You can start with your finger on the key with character 1 at index ii. Then glide to a key with character 2 at index jj, which takes ∣j−i∣\lvert j-i \rvert seconds. Typing character 2 twice takes no extra time, since ∣j−j∣=0\lvert j-j \rvert = 0. Then continue gliding through the remaining characters in order.

Find the minimal time needed to type the word.

Input

The first line contains the number of test cases T\mathbf{T}. T\mathbf{T} test cases follow.

The first line of each test case contains one integer N\mathbf{N}, the length of the word S\mathbf{S}.

The second line contains N\mathbf{N} integers, where each S_i\mathbf{S\_i} is the character at index ii.

The third line contains one integer M\mathbf{M}, the length of the keyboard K\mathbf{K}.

The fourth line contains M\mathbf{M} integers, where each K_i\mathbf{K\_i} is the character of key ii.

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 minimal time needed to type S\mathbf{S} on the keyboard K\mathbf{K}.

Constraints

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • Every character in S\mathbf{S} appears at least once in K\mathbf{K}.
  • 1≤K_i≤25001 \le \mathbf{K\_i} \le 2500.
  • 1≤S_i≤25001 \le \mathbf{S\_i} \le 2500.

Examples2

  1. Example 1

    Input
    2
    5
    1 2 3 2 1
    3
    1 2 3
    3
    1 1 1
    2
    2 1
    
    Expected output
    Case #1: 4
    Case #2: 0
    
  2. Example 2

    Input
    2
    4
    2 1 4 1
    8
    4 1 5 2 1 3 5 4
    3
    1 2 3
    8
    2 3 5 1 4 6 7 2
    
    Expected output
    Case #1: 4
    Case #2: 4