Submit solution
Points:
50.00
Time limit:
1.0s
Memory limit:
256M
Input:
stdin
Output:
stdout
Author:
Suggester:
Problem type
In a multidimensional space consisting of many different planes, the planes are not gender defined, planes are called love triangles if plane A loves plane B, plane B loves plane C and plane C loves plane A. Then, the relationship between the three planes A, B, C is called a love triangle. There are n planes numbered 1, 2, ..., n. The ~i^{th}~ plane is called the ~j^{th}~ plane if f[i] = j and i ≠ j. Your task is to check whether a love triangle exists in the given n planes?
Input:
Each test contains multiple test cases.
- The first line contains a single integer t (1 ≤ t ≤ ~10^3~) — the number of test cases. Each test case consists of two lines.
- For each testcase consisting of two lines:
- The first line contains an integer n (2 ≤ n ≤ ~10^9~) – the number of planes;
- The second line contains n integers ~f_1, f_2, ..., f_n~ (1 ≤ ~f_i~ ≤ n, ~f_i~ ≠ i), that is, the ith member loves the fith member.
Output:
Each testcase prints on 1 line, each line prints YES if a love triangle exists, otherwise prints NO
Example
Input
2
5
2 4 5 1 3
5
5 5 5 5 1
Output
YES
NO
Comments