eolymp
bolt
Try our new interface for solving problems

BFFs

You are a teacher at the brand new Little Coders kindergarten. You have n kids in your class, and each one has a different student ID number from 1 through n. Every kid in your class has a single best friend forever (BFF), and you know who that BFF is for each kid. BFFs are not necessarily reciprocal - that is, B being A's BFF does not imply that A is B's BFF.

Your lesson plan for tomorrow includes an activity in which the participants must sit in a circle. You want to make the activity as successful as possible by building the largest possible circle of kids such that each kid in the circle is sitting directly next to their BFF, either to the left or to the right. Any kids not in the circle will watch the activity without participating.

What is the greatest number of kids that can be in the circle?

Input

The first line gives the number of test cases t (1t100). t test cases follow. Each test case consists of two lines. The first line of a test case contains a single integer n (3n1000), the total number of kids in the class. The second line of a test case contains n integers F1, F2, ..., Fn, where Fi (1Fin) is the student ID number of the BFF of the kid with student ID i.

Output

For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the maximum number of kids in the group that can be arranged in a circle such that each kid in the circle is sitting next to his or her BFF.

Time limit 1 second
Memory limit 128 MiB
Input example #1
4
4
2 3 4 1
4
3 3 4 1
4
3 3 4 3
10
7 8 10 10 9 2 9 6 3 3
Output example #1
Case #1: 4
Case #2: 3
Case #3: 3
Case #4: 6
Author Pablo Heiber
Source Google Code Jam 2016 Round 1A Problem C