Three Kingdoms of Bourdelot

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 MB

Problem

You 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.

  • In a positive document, the person on the left of each line is an ancestor of the person on the right. Read as a positive document, the document above says "Alice is an ancestor of Bob, and Bob is an ancestor of Clare".
  • In a negative document, the person on the left of each line is not an ancestor of the person on the right. Read as a negative document, the document above says "Alice is not an ancestor of Bob, and Bob is not an ancestor of Clare".

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 xx, yy and zz, if xx is an ancestor of yy and yy is an ancestor of zz, then xx is an ancestor of zz. Read as a positive document, the document above therefore also gives the ancestor pair "Alice Clare".

You want to test one hypothesis: person pp is an ancestor of person qq. Which documents are positive and which are negative is unknown. Given the documents and two distinct names pp and qq, 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 xx and yy, either

  • xx is an ancestor of yy and yy is an ancestor of xx, or
  • xx is an ancestor of yy and xx is not an ancestor of yy.

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 AA is an ancestor of person BB, then AA is a parent, a grandparent, a great-grandparent, or so on, of BB. 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".

Family tree

Figure 1. A family tree

Input

The input is one test case in the following format.

p q
n
c_1
...
c_n

The first line has two distinct names pp and qq, separated by a space. The second line has one integer nn, the number of documents. The descriptions of the nn documents follow.

The ii-th document cic_i 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 mim_i, the number of name pairs in the document. Each of the next mim_i lines has a pair of distinct names xi,jx_{i,j} and yi,jy_{i,j} (1jmi1 \le j \le m_i), 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.

  • 1n10001 \le n \le 1000
  • 1mi1 \le m_i
  • i=1nmi100000\sum_{i=1}^{n} m_i \le 100000, that is, the total number of name pairs in the documents is at most 100000100000.
  • The number of distinct names in the test case is at most 300300.

Output

Print Yes in one line if some reading of the documents does not contradict the hypothesis that pp is an ancestor of qq. Print No otherwise.