A context free grammar is given in Chomsky Normal Form (CNF). Such a grammar consists of the following.
For A∈N, the language L(A) generated by A is defined as follows.
L(A)={wz∣w∈L(B), z∈L(C), A→BC∈R}∪{a∣A→a∈R}
The language generated by the grammar is the language of the start symbol, L(S). Given a string x, decide whether x belongs to L(S).
The first line contains the string x. It consists of lowercase letters only and its length is between 1 and 1000.
From the second line to the end of the file, each line holds one grammar rule. Nonterminal symbols are written as uppercase letters and terminal symbols as lowercase letters. A line ABC of length 3 means the rule A→BC, and a line Aa of length 2 means the rule A→a. The start symbol is always S. At least one rule is given, and the same rule can appear more than once.
Print 1 on one line if x belongs to the language generated by the grammar, and 0 otherwise.