Who Do You Think You Are?

No attempts yetTime limit2sMemory limit256 MB

Problem

Aunt Clara-May is drawing the family tree of her relatives, but she keeps losing track of how any two of them are related. Write a program that reads the tree and names the relationship between two people.

The family tree always obeys these rules:

  • Nobody marries twice, so step relationships never appear.
  • Every child in the tree is the child of a married couple, a male father and a female mother.
  • No two siblings marry each other, and no two cousins of any kind marry each other.
  • Everyone in the tree is connected to everyone else through marriages and parent-child links.

The relationship names follow these definitions. They are English words and they are printed in English.

  • father and mother are the parents of a child.
  • brother and sister are the male and female siblings with the same parents.
  • son and daughter are the children of a parent.
  • uncle and aunt are the brother and the sister of a child's parent.
  • nephew and niece are the male and female children of a sibling.
  • grandfather and grandmother are the father and the mother of a child's parent.
  • great grandfather and great grandmother are the father and the mother of a child's grandparent.
  • great uncle and great aunt are the siblings of a child's grandparent.
  • Cousins that are not removed sit at the same level of the tree. 1st cousins share their grandparents, 2nd cousins share their great grandparents, and so on.
  • Removed cousins sit at different levels. A 1st cousins 1-time removed relationship holds between a first cousin and the child of that cousin, a 1st cousins 2-times removed relationship holds between a first cousin and the grandchild of that cousin, and so on.
  • Cousin relationships are symmetric. If A is the 1st cousins 2-times removed of B, then B is the 1st cousins 2-times removed of A.
  • A relationship that exists because of a marriage is an in-law relationship and carries the suffix -in-law. The parent of a spouse is a father-in-law or a mother-in-law, the sibling of a spouse is a brother-in-law or a sister-in-law, and the cousin of a spouse is a cousins-in-law.

Figure 1 shows the family tree of the first example. In that tree Claire and Carol are 1st cousins, Claire and Diva are 1st cousins 1-time removed, and Claire and Chris are 1st cousins-in-law.

Figure 1: the family tree of the first example

Input

The input holds one family tree followed by a list of queries.

The first line contains an integer rr (1r2001 \le r \le 200), the number of relationship lines that build the tree. Each of the next rr lines contains three strings name1, name2 and relation separated by single spaces. Every name is made of English letters and all names are distinct. relation is one of husband, wife, son and daughter, and the line reads as "name1 is the relation of name2".

The next line contains an integer qq (1q2001 \le q \le 200), the number of queries. Each of the next qq lines contains two different names name1 and name2 separated by a single space. Both names appear in the family tree.

The relationship lines are enough to decide the sex of every person in the tree. In each marriage at most one of the two spouses has parents given in the input. Every queried pair is related by exactly one of the relationships defined below.

Output

For each query print one line that names how name1 is related to name2.

First define the blood distance. The ancestors of a person are that person, the parents, the parents of the parents, and so on, and every person is an ancestor of themselves at distance 00. For two people XX and YY, take a common ancestor with the smallest a+ba + b, where aa is the number of generations from XX up to that ancestor and bb is the number of generations from YY up to the same ancestor, and call the result (a,b)(a, b). If XX and YY have no common ancestor, they are not blood relatives.

Resolve every query in this order:

  1. If name1 is the spouse of name2, print name1 is the husband of name2 or name1 is the wife of name2 by the sex of name1.
  2. Otherwise, if name1 and name2 are blood relatives, take their (a,b)(a, b) and build the name below with no -in-law suffix.
  3. Otherwise, if the spouse of name1 is a blood relative of name2, take the (a,b)(a, b) of that pair and build the name below with the -in-law suffix.
  4. Otherwise the spouse of name2 is a blood relative of name1. Take the (a,b)(a, b) of that pair and build the name below with the -in-law suffix.

In all four cases the choice between the male word and the female word follows the sex of name1. In the rules below WW is -in-law when the relationship comes from a marriage and the empty string otherwise.

  • a=0a = 0: name1 is an ancestor of name2, bb generations up. Start from father for a man and mother for a woman. Keep the word as it is when b=1b = 1, put grand in front when b=2b = 2, and put grand in front plus b2b - 2 copies of great before that when b3b \ge 3. Print name1 is the XW of name2, where X is the word built this way.
  • b=0b = 0: the opposite direction. Start from son for a man and daughter for a woman and apply the same rule, which gives son, grandson, great grandson, great great grandson and so on. Print name1 is the XW of name2.
  • a=1a = 1 and b=1b = 1: print name1 is the brotherW of name2 for a man and name1 is the sisterW of name2 for a woman.
  • a=1a = 1 and b2b \ge 2: start from uncle for a man and aunt for a woman, put b2b - 2 copies of great in front, and print name1 is the XW of name2.
  • a2a \ge 2 and b=1b = 1: start from nephew for a man and niece for a woman, put a2a - 2 copies of great in front, and print name1 is the XW of name2.
  • a2a \ge 2 and b2b \ge 2: the two are cousins. Let d=min(a,b)1d = \min(a, b) - 1 and k=abk = |a - b|. Print name1 and name2 are D cousinsW, where D is dd followed by its English ordinal suffix: th when the last two digits are 1111, 1212 or 1313, and otherwise st for a last digit of 11, nd for 22, rd for 33 and th for anything else, which gives 1st, 2nd, 3rd, 4th, 11th, 12th, 13th, 21st, 22nd, 23rd. If k1k \ge 1, append a space, then kk, then -time removed when k=1k = 1 and -times removed when k2k \ge 2.