Dr. Montgomery Moreau has spent years observing a wild population of Northern Madagascar pie-bald shrews. He has carefully catalogued every shrew in the area, recorded its distinctive physical characteristics, and given each animal a name.
For every characteristic (for example: brown fur, red eyes, white feet, or prominent incisor teeth) he noted whether it is dominant or recessive:
His funding lapsed and he was away for several months, during which a new generation was born. Now that he has returned, he wants to determine, for each juvenile, every pair of adults that could be its parents.
A juvenile's parents are exactly one adult female (the mother) and one adult male (the father). A (mother, father) pair could be the parents of a juvenile if, for every characteristic, the rule above applied to the two adults yields exactly the juvenile's value for that characteristic.
The first line contains a string of 1 to 80 consecutive D and R characters. The i-th character states whether the i-th characteristic is dominant (D) or recessive (R).
Each of the following lines describes one adult shrew: a name of 1 to 32 non-blank characters, a single space, a single character M or F giving the animal's sex, another single space, and finally a string of 0/1 characters (one per characteristic, in order) in which 1 means the animal has that characteristic and 0 means it does not. The list of adults ends with a line containing only ***.
After that, one or more lines describe the juvenile shrews. Each juvenile line contains a name and a 0/1 characteristic string in the same format, separated by a single space, but without a sex field (a juvenile's sex is irrelevant). The list of juveniles ends with a line containing only ***.
For each juvenile, in the order given, print one line: the juvenile's name, the text by, and then the list of all pairs of adults that could be its parents. Write each pair as the mother's name, a hyphen, and then the father's name.
If more than one pair is possible, sort the pairs in lexicographic order first by the mother's name and then, among pairs with the same mother, by the father's name; separate consecutive pairs with the text or. If no pair of adults could be the parents, print the juvenile's name followed by by with nothing after it.