Garbled Email

Split the garbled string into dictionary words with changed letters spaced at least 5 apart while changing as few letters as possible.

Medium7Dynamic programmingTrieString matchingNo attempts yetTime limit60sMemory limit512 MB

Problem

Gagan just got an email from her friend Jorge. The email carries important information, but it was corrupted in transit. All of the spaces are gone, and after the spaces were removed some of the letters were changed into other letters. All Gagan has now is a string SS of lowercase characters.

The original email was built out of words from the dictionary described below. The letters were changed after the spaces were removed, and the difference between the indices of any two changed letters is at least 55. For example, "code jam" could have become "codejam", "dodejbm", "zodejan", or "cidejab", but not "kodezam", because the distance between the index of the "k" change and the index of the "z" change is only 44.

What is the minimum number of letters that could have been changed?

The dictionary contains WW words of at least 11 and at most 1010 lowercase characters and is given at the start of the input. It is not a dictionary from any natural language, though it does contain some English words. One input carries one dictionary, and every test case in that input uses it. The dictionary is given in lexicographically increasing order and contains no duplicate words.

Input

The first line of the input gives the number of words in the dictionary, WW. Each of the next WW lines contains a string of lowercase characters a to z representing one word of the dictionary. The next line gives the number of test cases, TT. TT test cases follow. Each test case is a single line containing a string SS of lowercase characters a to z.

Output

For each test case, print one line containing "Case #x: y", where x is the test case number starting from 11 and y is the minimum number of letters that could have been changed while SS was produced.

Constraints

  • 1W50001 \le W \le 5000
  • Each word of the dictionary has at least 11 and at most 1010 lowercase characters.
  • The dictionary is sorted in lexicographically increasing order.
  • The dictionary contains no duplicate words.
  • The total number of characters in the dictionary is at most 3000030000.
  • 1T41 \le T \le 4
  • 1S40001 \le |S| \le 4000
  • SS can be produced by the method described above.

Note

The dictionary is not a natural language dictionary, so an English word is not guaranteed to appear in it. It may hold "operation" and "oo" while it does not hold "cooperation". A stretch that looks like "cooperation" then still has to be split into words the dictionary does hold.