Under construction

Still being built. Things may change

Twibet (Large)

Interview

Time limit5sMemory limit512 MB

Summary
In a graph where each monk follows exactly one other, count for every starter how many monks hear the whisper passed down through followers.
Level

Medium4 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

The holy country of Twibet has NN monks. Each monk carries a distinct number from 11 to NN, and for religious reasons they use no names. The monks walk slowly around Twibet, and each monk follows exactly one other monk.

Most of the time every monk is silent. On day KK, monk KK stops, turns around and whispers the 140 Words of Wisdom. The whisper is quiet, so only the monks who follow him directly can hear it. Each of those followers then stops, turns around and whispers the same words to his own followers. The chain keeps going. Every monk who has just heard the Words today, and has not whispered them yet today, stops and whispers to his followers.

Once every monk who could hear the Words has whispered them, they all turn back and walk on as usual. The next day the same thing happens again, this time starting from a different monk.

For each KK from 11 to NN, how many monks whisper the 140 Words of Wisdom on day KK?

Input

The first line contains the number of test cases TT.

Each test case starts with a line containing one integer NN. The next line contains NN space-separated integers F1,F2,…,FNF_1, F_2, \dots, F_N. Monk ii follows monk FiF_i.

Constraints

  • 1≤T≤1001 \le T \le 100
  • 2≤N≤10002 \le N \le 1000
  • 1≤Fi≤N1 \le F_i \le N and Fi≠iF_i \ne i, so no monk directly follows himself.

Output

For each test case, first print one line Case #x:, where xx is the test case number starting from 11.

Then print NN lines, one per day. The first of those lines contains the number of monks who whisper the Words on day 11, the next line the number for day 22, and so on through day NN.

Explanation

In the first test case of the example, all 3 monks walk in a circle. Whoever whispers first, his follower whispers next and the remaining monk whispers after that. So on each of the 3 days all 3 monks whisper.

In the second test case, monk 1 follows monk 2, monk 2 follows monk 3, monk 3 follows monk 2, and monk 4 follows monk 1. On day 1 monk 1 whispers first and monk 4 hears him and whispers next, while monks 2 and 3 hear nothing that day. On day 2 monk 2 whispers first, monks 1 and 3 hear him and whisper next, and monk 4 hears monk 1 and whispers last. On day 3 the monks whisper in the order 3, 2, 1, 4. On day 4 monk 4 whispers and nobody hears him.

Examples3

  1. Example 1

    Input
    2
    3
    2 3 1
    4
    2 3 2 1
    
    Expected output
    Case #1:
    3
    3
    3
    Case #2:
    2
    4
    4
    1
    
  2. Example 2

    Input
    1
    2
    2 1
    
    Expected output
    Case #1:
    2
    2
    
  3. Example 3

    Input
    1
    6
    2 1 1 1 1 1
    
    Expected output
    Case #1:
    6
    6
    1
    1
    1
    1