Touchbar Typing

아직 제출이 없습니다메모리 제한1024 MB

문제

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

To make the Glide Typing task more challenging, instead of a normal keyboard, we have a special linear keyboard K\mathbf{K} that has all the keys in one row.

Imagine that you want to type a word S\mathbf{S} that is N\mathbf{N} characters long. The linear keyboard K\mathbf{K} has M\mathbf{M} keys. It is guaranteed that the keys cover all characters in S\mathbf{S}. However, some of the keys may be duplicates. In other words, for each character in S\mathbf{S}, there is one or more keys in K\mathbf{K} mapped to the character. Note that, all characters and keys are represented as integers.

You may start with your finger on any key. It takes 11 second to move your finger from a key to an adjacent key. Due to Glide Typing, there is no pressing a key. If the finger is currently at the key ii which has character K_i\mathbf{K\_i}, and we want to type the character K_j\mathbf{K\_j} at index jj, we will glide the finger from the key ii to the key jj, which takes ji\lvert j-i \rvert seconds. If your finger is at key xx, you can type character K_x\mathbf{K\_x} any number of times instantly. You need to type string S\mathbf{S} character by character. Formally, you need to type S_i\mathbf{S\_i} before S_i+1\mathbf{S\_{i+1}} for each 1iN11 \le i \le \mathbf{N}-1.

For example, suppose the word S\mathbf{S} has characters: 1, 2, 2, 3, 4. You can start by keeping your finger on key with character 1 on the keyboard which is at index ii. Then you glide your finger to key which has character 2 which is at index jj. It would take ji\lvert j-i \rvert seconds. In order to type character 2 two times in string S\mathbf{S}, you can do that in no additional time as jj=0\lvert j-j \rvert = 0 seconds. Then you can continue to glide your finger to type the other characters in the word S\mathbf{S} sequentially.

Can you calculate the minimal time needed to type the word?

입력

The first line of the input gives 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 of each test case contains N\mathbf{N} integers: each S_i\mathbf{S\_i} is the character at the ii-th index.

The third line of each test case contains one integer M\mathbf{M}: the length of the keyboard K\mathbf{K}.

The fourth line of each test case contains M\mathbf{M} integers: each K_i\mathbf{K\_i} is the character at the ii-th key.

출력

For each test case, output one line containing the minimal time needed to type the word. 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}.

제한

  • 1T1001 \le \mathbf{T} \le 100.
  • All characters in S\mathbf{S} appears at least once in K\mathbf{K}.
  • 1K_i25001 \le \mathbf{K\_i} \le 2500.
  • 1S_i25001 \le \mathbf{S\_i} \le 2500.