Decide whether some assignment of positive or negative polarity to each document is consistent with the hypothesis that person p is an ancestor of person q.
Hard9GraphUnion-findImplementationMathNo attempts yetTime limit4sMemory limit512 MBYou are an archaeologist at a pedigree research institute that studies the dynasties of the three ancient kingdoms of Bourdelot. During a dig you recovered a stack of documents about those dynasties. The early history of the kingdoms is unknown and the names of many kings and queens are lost, so these documents are the only record of the blood relations of the royal families.
A document is a list of lines, and each line holds the names of two royal family members. Here is a document with two lines.
Alice Bob
Bob Clare
Each line was once a full sentence, but the documents are badly damaged and only two names per line can still be read. Reading every line as a true ancestor relation leads to a contradiction. Some documents must be read negatively before the ancestor relations make sense. Every document is either a positive document or a negative document.
A single document never mixes positive and negative lines. The document above can never be read as "Alice is an ancestor of Bob, and Bob is not an ancestor of Clare".
Ancestor pairs that no line states directly can still be derived by one rule: for any persons x, y and z, if x is an ancestor of y and y is an ancestor of z, then x is an ancestor of z. Read as a positive document, the document above therefore also gives the ancestor pair "Alice Clare".
You want to test one hypothesis: person p is an ancestor of person q. Which documents are positive and which are negative is unknown. Given the documents and two distinct names p and q, decide whether some reading of the documents does not contradict the hypothesis. A reading contradicts the hypothesis exactly when that reading together with the hypothesis lets you derive, for some persons x and y, either
Every person named in the documents has exactly one name. No two people share a name, and no person appears under two names.
When person A is an ancestor of person B, then A is a parent, a grandparent, a great-grandparent, or so on, of B. People and ancestor pairs that appear in no document may still exist. For the family tree in Figure 1, this positive document is possible:
A H
B H
D H
F H
E I
Here C and G never appear, and neither do ancestor pairs such as "A E", "D F" and "C I".

Figure 1. A family tree
The input is one test case in the following format.
p q
n
c_1
...
c_n
The first line has two distinct names p and q, separated by a space. The second line has one integer n, the number of documents. The descriptions of the n documents follow.
The i-th document ci has this format.
m_i
x_{i,1} y_{i,1}
...
x_{i,m_i} y_{i,m_i}
The first line has one integer mi, the number of name pairs in the document. Each of the next mi lines has a pair of distinct names xi,j and yi,j (1≤j≤mi), separated by a space.
Each name consists of lowercase and uppercase letters and has length between 1 and 5, inclusive. Two names are the same name only when they match exactly, so a and A are names of two different people.
The test case satisfies the following constraints.
Print Yes in one line if some reading of the documents does not contradict the hypothesis that p is an ancestor of q. Print No otherwise.