Twibet (Large)
InterviewTime limit5sMemory limit512 MB
In a graph where each monk follows exactly one other, count for every starter how many monks hear the whisper passed down through followers.
Problem
The holy country of Twibet has monks. Each monk carries a distinct number from to , 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 , monk 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 from to , how many monks whisper the 140 Words of Wisdom on day ?
Input
The first line contains the number of test cases .
Each test case starts with a line containing one integer . The next line contains space-separated integers . Monk follows monk .
Constraints
- and , so no monk directly follows himself.
Output
For each test case, first print one line Case #x:, where is the test case number starting from .
Then print lines, one per day. The first of those lines contains the number of monks who whisper the Words on day , the next line the number for day , and so on through day .
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.