

The maze may contain self-cycles.
arr[i] = -1 means the ‘i’th cell doesn’t have an exit.
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains integer ‘N’, which denotes the number of cells in the maze.
The second line contains N integers, denoting the elements of the array ‘arr’.
For each test case, print the length of the largest cycle in the maze and -1 if there are no cycles.
Print the output of each test case in a separate line.
1<= T <= 50
1 <= N <= 10,000
-1 <= arr[i] <= N-1
Where ’T’ is the number of test cases, and N denotes the number of cells in the maze and arr[i] is the cell that can be reached from ‘i’th cell.
Time Limit: 1 sec
The idea is to do a depth-first search to find all the cycles which are formed and calculate the length of the largest cycle. We are treating the array as a graph of directed edges. Whenever we get into any of the cells in the cycle, using dfs we will visit all the subsequent cells in the cycle. Out of all the cycles, we will return the cycle of maximum length.
The steps are as follows: