PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000032. Mật thất Mount Rushmore (Secret Chamber at Mount Rushmore)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Mật thất Mount Rushmore (Secret Chamber at Mount Rushmore)

Secret Chamber at Mount Rushmore

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Given directed letter translation rules, determine for each pair of words whether the first can be transformed to the second, letter-by-letter, using zero or more translations.

Input

Integers m,qm,q; mm directed letter rules a,ba,b, followed by qq pairs of words.

Output

For every word pair print yes or no.

Subtasks

  • Subtask 1 (40 points): m≤25m\le25, q≤10q\le10; 1≤m≤5001\le m\le500, 1≤q≤501\le q\le50; distinct rules on different letters; word lengths 1…501\ldots50, lowercase a–z only.
  • Subtask 2 (60 points): 1≤m≤5001\le m\le500, 1≤q≤501\le q\le50; distinct rules on different letters; word lengths 1…501\ldots50, lowercase a–z only.

Examples

Input

3 3
a c
b a
a b
aaa abc
abc aaa
acm bcm

Output

yes
no
yes

Explanation

The letter a can become b or c, but c cannot be translated back into a. This validates the first and third word pairs, but not the second.