Relative Relatives
InterviewTime limit1sMemory limit128 MB
Given Ted's age of 100 and each descendant's father name plus the father's age at the child's birth, compute every descendant's age and list them oldest first, ties broken by name.
Problem
Today is Ted's 100th birthday. A few weeks ago the family chose you to track down all of Ted's descendants and throw a surprise party. To make the job easier, you decide to build a single list of everyone descended from Ted, ordered from oldest to youngest. Descendants who are the same age are listed in dictionary order.
The only records you have are birth certificates. Strangely, none of them are dated. Each certificate lists only three things: the father's name, the child's name, and the father's exact age (in whole years) at the moment the child was born.
Because every descendant of Ted shares Ted's birthday, the age gap between any two people is always a whole number of years. Ted turns 100 today, so his age is 100.
Input
The first line contains a single integer , the number of data sets. Each data set is formatted as follows.
A data set has two parts:
- Descendant count — a line with a single integer (), the number of Ted's descendants.
- Birth certificate list — lines, one certificate per line, each in the format
FNAME CNAME FAGE, where:FNAMEis the father's name.CNAMEis the child's name.FAGEis the father's whole-year age on the dayCNAMEwas born.
Notes:
- Names are unique identifiers and contain no embedded whitespace.
- Every descendant of Ted shares Ted's birthday, so the age difference between any two of them is a whole number of years.
- The certificates are a complete collection: there is exactly one certificate for every descendant of Ted.
Output
For each data set, print lines. The first line is DATASET Y, where is for the first data set, for the second, and so on. The next lines are the age-prioritized list of Ted's descendants, one per line in the format NAME AGE, ordered from oldest to youngest. Descendants of the same age are listed in dictionary order.