#K79032. Detecting Incorrect Letter Substitution

    ID: 35218 Type: Default 1000ms 256MiB

Detecting Incorrect Letter Substitution

Detecting Incorrect Letter Substitution

In this problem, you are given a number of encrypted messages along with partial decryption keys (mappings from an encrypted letter to the intended letter). For each test case, your task is to find the first letter in the message that is incorrectly substituted.

The decryption key is given as a list of character pairs. For each character c in the encrypted message:

  • If c is not present in the provided mappings, then c is considered to be an incorrect substitution.
  • If c is mapped to a decrypted letter d, then count the frequency of c in the message, and also count the total number of characters in the message that are mapped to d. If these two counts differ, then c is the incorrectly substituted letter.

If all letters are correctly substituted, output nothing (i.e. an empty line) for that test case.

Note: The substitution keys for some letters may be missing, in which case the very first occurrence of a letter that is unmapped should be output.

inputFormat

The input is read from standard input and has the following format:

T
message_1
k_1
enc1 dec1
enc2 dec2
... (k_1 lines)
message_2
k_2
enc1 dec1
enc2 dec2
... (k_2 lines)
...

Here, T is the number of test cases. For each test case, the first line is the encrypted message (a non-empty string without spaces), followed by an integer k representing the number of mapping pairs. Each of the next k lines contains two characters separated by a space: the encrypted character and its corresponding decrypted character.

outputFormat

For each test case, print the first incorrectly substituted letter on its own line. If all substitutions in the message are correct, print an empty line for that test case.

## sample
1
ifmmp
4
f o
m n
m l
p q
i