#P1327D. Infinite Path
Infinite Path
No submission language available for this problem.
Description
You are given a colored permutation . The -th element of the permutation has color .
Let's define an infinite path as infinite sequence where all elements have same color ().
We can also define a multiplication of permutations and as permutation where . Moreover, we can define a power of permutation as .
Find the minimum such that has at least one infinite path (i.e. there is a position in such that the sequence starting from is an infinite path).
It can be proved that the answer always exists.
The first line contains single integer () — the number of test cases.
Next lines contain test cases — one per three lines. The first line contains single integer () — the size of the permutation.
The second line contains integers (, for ) — the permutation .
The third line contains integers () — the colors of elements of the permutation.
It is guaranteed that the total sum of doesn't exceed .
Print integers — one per test case. For each test case print minimum such that has at least one infinite path.
Input
The first line contains single integer () — the number of test cases.
Next lines contain test cases — one per three lines. The first line contains single integer () — the size of the permutation.
The second line contains integers (, for ) — the permutation .
The third line contains integers () — the colors of elements of the permutation.
It is guaranteed that the total sum of doesn't exceed .
Output
Print integers — one per test case. For each test case print minimum such that has at least one infinite path.
Samples
Note
In the first test case, and the sequence starting from : is an infinite path.
In the second test case, and it obviously contains several infinite paths.
In the third test case, and the sequence starting from : is an infinite path since .