Under construction

Still being built. Things may change

Finding Your Roots

Interview

Time limit1sMemory limit1024 MB

Summary
For each data set, count how many lions lie on the parent chain from lion L upward, including L, until the chain reaches an unknown parent.
Level

Easy2 of 10

Topics
Implementation, Linked list
Solved
No attempts yet

Problem

P-22's father was P-1, the first mountain lion tagged in the Santa Monica Mountains. P-22's mother is unknown. Unfortunately, we do not know anything about P-1's parents, so we can only trace back P-22's ancestry two generations (himself and his father). In some other cases, one can trace back ancestry quite a bit further, and you are supposed to find out just how far.

To keep things simple (and avoid exponential growth), each mountain lion is given only one parent, or none when no information is available. Return the total number of generations in the ancestry of a specific mountain lion that can be traced back.

Input

The first line contains the number of data sets 1≤K≤1001 \le K \le 100. This is followed by KK data sets.

The first line of a data set contains two integers 1≤L≤n≤100001 \le L \le n \le 10000. nn is the total number of mountain lions in the database, and LL is the mountain lion whose ancestry you are interested in.

The next line contains nn integers pip_i, where 0≤pi≤n0 \le p_i \le n is the parent of mountain lion ii. A 00 means the parent of lion ii is unknown. Because these are parent relationships, the input never contains cycles.

Output

For each data set, print Data Set x: on its own line, where xx is the data set number. Then print the number of generations of the ancestry of LL that are in the database, including LL himself or herself.

Print a blank line after each data set.

Examples1

  1. Example 1

    Input
    2
    4 6
    0 0 4 6 1 1
    5 5
    0 0 0 0 0
    
    Expected output
    Data Set 1:
    3
    
    Data Set 2:
    1