Under construction

Still being built. Things may change

Colliding Encoding

Interview

Time limit20sMemory limit1024 MB

Summary
Given a digit for each letter A to Z and a list of words, determine whether two different words encode to the same digit string.
Level

Easy3 of 10

Topics
Hash map, String
Solved
No attempts yet

Problem

Alan just had his first cryptography class in school today. He decided to apply what he learned and came up with his own cipher. He maps each English letter from A to Z to a decimal digit from 00 to 99. He then encodes each word as a string of decimal digits by replacing each letter with its mapped digit.

Alan did not notice that there are 2626 letters and only 1010 decimal digits. As a result, collisions are possible, meaning two different words can have the same encoding.

Given a list of NN words and the mapping Alan uses, determine whether any collisions exist among the words on the list.

Input

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

The first line of each test case contains 26 decimal digits (integers from 00 to 99, inclusive) DA,DB,…,DZD_A, D_B, \dots, D_Z. Letter α\alpha is mapped to digit DαD_\alpha.

The second line of each test case contains NN, the number of words.

Each of the next NN lines contains one word SiS_i.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number starting from 1. Output YES if at least one pair of different words on the list has the same encoding. Otherwise, output NO.

Constraints

  • 1≤T≤1001 \le T \le 100.
  • 0≤Dα≤90 \le D_\alpha \le 9 for all letters α\alpha.
  • 1≤1 \le the length of Si≤10S_i \le 10 for all ii.
  • Each character of SiS_i is an uppercase English letter from A to Z.
  • Si≠SjS_i \ne S_j for all i≠ji \ne j.

Hint

In Sample Case #1, the mapping for A is 00, for B is 11, for C is 22, for D is 33, and for E is 33. With this mapping, ABC is encoded as 012012, BC is encoded as 1212, BCD as 123123, and CDE as 233233. Since all of these encodings are distinct, there are no collisions.

In Sample Case #2, the mapping for C is 22, for D is 33, for E is 33, for F is 33, and for G is 33. With this mapping, CDE is encoded as 233233, DEF as 333333, and EFG as 333333. Since the encodings for DEF and EFG are the same, there is a collision.

Examples1

  1. Example 1

    Input
    2
    0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
    4
    ABC
    BC
    BCD
    CDE
    0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
    3
    CDE
    DEF
    EFG
    
    Expected output
    Case #1: NO
    Case #2: YES