#P6937. Deciphering the Historical Documents

    ID: 20144 Type: Default 1000ms 256MiB

Deciphering the Historical Documents

Deciphering the Historical Documents

Dr. Dakota Jones discovered that some of the historical U.S. documents at Mount Rushmore were written in a secret code. The key to decoding these documents lies in a set of letter translations. Each available translation indicates that a letter can be converted into another letter. Note that a letter can always remain unchanged, since zero translations is allowed.

Formally, you are given a list of translations. Each translation is a directed mapping from one letter to another. Then, you are given a list of pairs of words. Two words of equal length match if, for each i, the i-th letter in the original word can be turned into the i-th letter in the deciphered word by applying the translations zero or more times.

This condition can be stated in mathematical form as follows: given letters \(s_i\) and \(t_i\) in the original and target words, respectively, we require that \(s_i \to^* t_i\) for all valid indices i, where \(\to^*\) indicates the reflexive transitive closure of the translation relation.

inputFormat

The input begins with a positive integer n (\(1 \le n \le 1000\)) representing the number of available letter translations. Each of the next n lines contains two space-separated lowercase letters, indicating that the first letter can be translated into the second letter.

After that, there is an integer q (\(1 \le q \le 100\)) representing the number of word pairs to verify. Each query consists of two lines: the first line is the original (possibly gibberish) word and the second line is the deciphered word. Both words contain only lowercase letters and have a length between 1 and 100. It is guaranteed that the two words in each query have the same length.

outputFormat

For each query, output a single line with yes if the two words match based on the given translations, or no otherwise.

sample

3
a b
b c
a c
3
abc
bca
aaa
bbb
ab
ac
yes

yes yes

</p>