Saving the Universe (Large)

Given a query stream and a set of engines, choose which engine handles each query, never an engine matching the query, to minimize the number of switches.

Medium5GreedyImplementationArraySimulationInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

An urban legend says that if you open the Google homepage and search for "Google", the universe implodes. It does not.

In a universe far away the rule is different. There, searching a search engine for that same search engine's name really does make the universe implode.

To stay safe, the people there pool every query. The pooled queries go to a central system, and the central system decides which search engine each query is sent to. The central system sends a run of queries to one search engine and can switch to another search engine at any time. Queries must be processed in the order they arrive, and the central system must never send a query to the search engine whose name equals that query. Switching costs money, so the number of switches has to be as small as possible.

Find how many times the central system has to switch search engines when it is programmed optimally.

Input

The first line contains the number of test cases, NN. NN test cases follow.

Each test case starts with a line holding the number of search engines, SS. The next SS lines each hold the name of one search engine. A name is at most 100 characters long and contains only uppercase letters, lowercase letters, spaces, and digits. No two search engines in the same test case have the same name.

The next line holds the number of incoming queries, QQ. The next QQ lines each hold one query. Every query is the name of one of the search engines in that test case.

Limits:

  • 0<N200 < N \le 20
  • 2S1002 \le S \le 100
  • 0Q10000 \le Q \le 1000

Output

For each test case, print one line in this format.

Case #X: Y

XX is the test case number and YY is the number of search engine switches. Do not count the initial choice of a search engine as a switch.

Hint

In the first test case of example 1, you can start with Dont Ask and switch to NSM after the eighth query. In the second test case you can stay on B9 the whole time, so no switch happens.