Countdown

Time limit1sMemory limit128 MB

Summary
Given one family tree per test case, count for each person how many descendants sit exactly d generations below, then rank the top holders.
Level

Medium4 of 10

Topics
Graph, DFS, Implementation
Solved
No attempts yet

Problem

A genealogy service stores family trees for its clients. A client recently asked a question the software could not answer: "Which member of my family has the most grandchildren?" — along with similar questions about great-grandchildren, great-great-grandchildren, and so on.

For a given family tree and a distance dd, a person's relevant descendants are the people exactly dd generations below them: d=1d = 1 means their children, d=2d = 2 their grandchildren, d=3d = 3 their great-grandchildren, and so on. Find the people with the most relevant descendants.

Input

The first line contains a single integer, the number of test cases.

Each test case begins with a line containing two positive integers nn and dd: nn is the number of description lines that follow, and dd is the generation distance of the question (d=1d = 1 for children, d=2d = 2 for grandchildren, and so on).

Each of the next nn lines has the form

name m dname1 dname2 ... dnamem

where name is a family member, m is that member's number of children, and dname1 ... dnamem are the children's names. The lines are given in no particular order. Together the nn lines describe one single connected tree. A tree has at most 1000 people, and every name is at most 10 characters long.

Output

For each test case, first print the line

Tree i:

where i is the test case number, starting at 1.

Then, among the people who have at least one relevant descendant, order them by their number of relevant descendants (largest first), breaking ties by name in alphabetical order. Print the first three people of this ordering; however, if the third-place count is tied so that more than three people qualify, print every person whose count is at least that third-place count (all people tied at the bottom are included). Print fewer than three names when fewer than three people have any relevant descendants, and print no names at all if nobody does. Give each printed person on their own line as the name, a single space, and the count.

Separate consecutive test cases with a blank line.

Examples1

  1. Example 1

    Input
    3
    8 2
    Barney 2 Fred Ginger
    Ingrid 1 Nolan
    Cindy 1 Hal
    Jeff 2 Oliva Peter
    Don 2 Ingrid Jeff
    Fred 1 Kathy
    Andrea 4 Barney Cindy Don Eloise
    Hal 2 Lionel Mary
    6 1
    Phillip 5 Jim Phil Jane Joe Paul
    Jim 1 Jimmy
    Phil 1 Philly
    Jane 1 Janey
    Joe 1 Joey
    Paul 1 Pauly
    6 2
    Phillip 5 Jim Phil Jane Joe Paul
    Jim 1 Jimmy
    Phil 1 Philly
    Jane 1 Janey
    Joe 1 Joey
    Paul 1 Pauly
    
    Expected output
    Tree 1:
    Andrea 5
    Don 3
    Cindy 2
    
    Tree 2:
    Phillip 5
    Jane 1
    Jim 1
    Joe 1
    Paul 1
    Phil 1
    
    Tree 3:
    Phillip 5