This problem deals with family trees. A family tree (or, more accurately, a family graph) is a set of nodes, representing individuals, connected by directed edges, representing the "offspring" relation. An edge from one node to another means that the latter node is an offspring of the former. In general, one node is a descendant of another (its ancestor) if there is a directed sequence of edges from the ancestor node to the descendant node. Nodes can have at most two edges in (one for each parent), and an arbitrary number of edges out (one for each offspring).
The distance between an ancestor and a descendant is the number of edges in the graph between their nodes. Thus, the distance between a parent and child is one; between a grandparent and child is two.
Two people are related if they share a common ancestor; that is, if there exists a person from whom they are both desdended. The distance between two related people via a common ancestor is the sum of the distances between each person and the common ancestor. A closest common ancestor is one who minimizes the distance between the two related people. Note that, in general, two related people may have several closest common ancestors (for example, if twins marry twins, their children will have the four grandparents as closest common ancestors).
For this problem you are to read a set of family history records - each record containing a father, mother and list of offspring - and then accept queries concerning pairs of people in the database. The input is divided into two sections: a data section and a query section. The data section is a sequence of family records, each record containing the names of a single family, in the order father, mother, children. A name is a sequence of alphabetic characters followed by one or more spaces. The data section is terminated by a line containing a peroid (".") in column one.
The second section of the input consists of queries, one per line, each line containing two names. The query section is terminated by end of file. For each query your program is to print a line containing the query (i.e., echo the input line) and one of the following:
robert loretta bob jerry lauri bob linda dave dan dede donna todd dona robby dan heather billy dave mary george ernie dorothy linda todd terry tim tom marianne bill jim tom cathy betty . robby donna dorothy loretta bob dede billy george betty jim
robby donna
ernie
dorothy
dorothy loretta
unrelated
bob dede
descendant
billy george
bob
linda
betty jim
tom