You are given a colored permutation $p_1, p_2, \dots, p_n$. The $i$-th element of the permutation has color $c_i$.

Let's define an infinite path as infinite sequence $i, p[i], p[p[i]], p[p[p[i]]] \dots$ where all elements have same color ($c[i] = c[p[i]] = c[p[p[i]]] = \dots$).

We can also define a multiplication of permutations $a$ and $b$ as permutation $c = a \times b$ where $c[i] = b[a[i]]$. Moreover, we can define a power $k$ of permutation $p$ as $p^k=\underbrace{p \times p \times \dots \times p}_{k \text{ times}}$.

Find the minimum $k > 0$ such that $p^k$ has at least one infinite path (i.e. there is a position $i$ in $p^k$ such that the sequence starting from $i$ is an infinite path).

It can be proved that the answer always exists.


-----Input-----

The first line contains single integer $T$ ($1 \le T \le 10^4$) — the number of test cases.

Next $3T$ lines contain test cases — one per three lines. The first line contains single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the size of the permutation.

The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, $p_i \neq p_j$ for $i \neq j$) — the permutation $p$.

The third line contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le n$) — the colors of elements of the permutation.

It is guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$.


-----Output-----

Print $T$ integers — one per test case. For each test case print minimum $k > 0$ such that $p^k$ has at least one infinite path.


-----Example-----
Input
3
4
1 3 4 2
1 2 2 3
5
2 3 4 5 1
1 2 3 4 5
8
7 4 5 6 1 8 3 2
5 3 6 4 7 5 8 4

Output
1
5
2



-----Note-----

In the first test case, $p^1 = p = [1, 3, 4, 2]$ and the sequence starting from $1$: $1, p[1] = 1, \dots$ is an infinite path.

In the second test case, $p^5 = [1, 2, 3, 4, 5]$ and it obviously contains several infinite paths.

In the third test case, $p^2 = [3, 6, 1, 8, 7, 2, 5, 4]$ and the sequence starting from $4$: $4, p^2[4]=8, p^2[8]=4, \dots$ is an infinite path since $c_4 = c_8 = 4$.

Difficulty: interview

Starter code:

Solutions:
from sys import stdin
input = stdin.readline
q = int(input())
for rwerew in range(q):
	n = int(input())
	p = list(map(int,input().split()))
	c = list(map(int,input().split()))
	for i in range(n):
		p[i] -= 1
	przyn = [0] * n
	grupa = []
	i = 0
	while i < n:
		if przyn[i] == 1:
			i += 1
		else:
			nowa_grupa = [i]
			j = p[i]
			przyn[i] = 1
			while j != i:
				przyn[j] = 1
				nowa_grupa.append(j)
				j = p[j]
			grupa.append(nowa_grupa)
	grupacol = []
	for i in grupa:
		cyk = []
		for j in i:
			cyk.append(c[j])
		grupacol.append(cyk)
	#print(grupacol)
	mini = 234283742834
	for cykl in grupacol:
		dziel = []
		d =  1
		while d**2 <= len(cykl):
			if len(cykl)%d == 0:
				dziel.append(d)
			d += 1
		dodat = []
		for d in dziel:
			dodat.append(len(cykl)/d)
		dziel_ost = list(map(int,dziel + dodat))
		#print(dziel_ost, len(cykl))
		for dzielnik in dziel_ost:
			for i in range(dzielnik):
				indeks = i
				secik = set()
				chuj = True
				while indeks < len(cykl):
					secik.add(cykl[indeks])
					indeks += dzielnik
					if len(secik) > 1:
						chuj = False
						break
				if chuj:
					mini = min(mini, dzielnik)
	print(mini)
			


T = int(input())
for _ in range(T):
    n = int(input())
    perm = list([int(x) - 1 for x in input().split()])
    color = list(map(int, input().split()))

    SMALL = n

    unseen = set(range(n))
    while unseen:
        start = unseen.pop()
        l = [start]
        nex = start
        while perm[nex] != start:
            nex = perm[nex]
            l.append(nex)
            unseen.remove(nex)

        size = len(l)
        factors = []

        curr = 1
        while curr * curr <= size:
            if size % curr == 0:
                factors.append(curr)
                factors.append(size//curr)
            curr += 1

        for f in factors:
            works = [True] * f
            for i in range(size):
                if color[l[i]] != color[l[i % f]]:
                    works[i % f] = False

            for b in works:
                if b:
                    SMALL = min(SMALL, f)
                    break

    print(SMALL)
        


import sys

T = int(sys.stdin.readline().strip())
Q = []
P = [0] * (10 ** 6)
for i in range (2, 2 * 10 ** 5 + 10):
    v = True
    if P[i] == 0:
        Q.append(i)
        j = i
        while j < 10 ** 6:
            P[j] = 1
            j = j + i

def factors(n):
    i2 = 0
    ans = [1]
    while n > 1:
        m = 0
        while n % Q[i2] == 0:
            m = m + 1
            n = n // Q[i2]
        ans2 = []
        for h in range (0, m+1):
            for j in ans:
                ans2.append(j * (Q[i2] ** h))
        i2 = i2 + 1
        ans = ans2[:]
    return ans

for t in range (0, T):
    n = int(sys.stdin.readline())
    p = list(map(int, sys.stdin.readline().strip().split()))
    c = list(map(int, sys.stdin.readline().strip().split()))
    for i in range (0, n):
        p[i] = p[i] - 1
    P = []
    L = []
    i = 0
    ans = n
    while i < n:
        if p[i] == -1:
            i = i + 1
        else:
            j = i
            x = [j]
            while p[j] != i:
                x.append(p[j])
                j2 = p[j]
                p[j] = -1
                j = j2
            p[j] = -1
            P.append(x)
            l = len(x)
            F = factors(l)
            for f in F:
                for j in range (0, f):
                    v = True
                    for h in range (0, l // f):
                        if c[x[j + h * f]] != c[x[j]]:
                            v = False
                    if v == True:
                        ans = min(ans, f)   
    print(ans)



import sys
input = lambda: sys.stdin.readline().rstrip()

def primeFactor(N):
    i, n, ret, d, sq = 2, N, {}, 2, 99
    while i <= sq:
        k = 0
        while n % i == 0: n, k, ret[i] = n//i, k+1, k+1
        if k > 0 or i == 97: sq = int(n**(1/2)+0.5)
        if i < 4: i = i * 2 - 1
        else: i, d = i+d, d^6
    if n > 1: ret[n] = 1
    return ret

def divisors(N):
    pf = primeFactor(N)
    ret = [1]
    for p in pf:
        ret_prev = ret
        ret = []
        for i in range(pf[p]+1):
            for r in ret_prev:
                ret.append(r * (p ** i))
    return sorted(ret)

def chk(X):
    n = len(X)
    XX = X * 2
    mi = 1 << 30
    for d in divisors(n):
        if d >= mi: break
        for i in range(d):
            for j in range(i, n+i, d):
                if XX[j] != XX[j+d]:
                    break
            else:
                mi = min(mi, d)
    return mi

T = int(input())
for _ in range(T):
    N = int(input())
    A = [int(a) - 1 for a in input().split()]
    C = [int(a) - 1 for a in input().split()]
    done = [0] * N
    L = []
    for i in range(N):
        if done[i]: continue
        t = [C[i]]
        j = A[i]
        while j != i:
            t.append(C[j])
            done[j] = 1
            j = A[j]
        L.append(t)
    print(min([chk(a) for a in L]))


import sys



def bestval(pp, cc):
    # print("BESTVAL:")
    # print(pp)
    # print(cc)
    k = len(pp)
    k_2 = k//2+1
    for f in range(1, k_2):
        if k % f == 0:
            for offs in range(f):
                
                good = True
                num = cc[offs]
                # print(f"{f}, {offs}, {num}: ")
                upp = (k//f)//2+1
                for j in range(1, upp):
                    v1 = f*j 
                    v2 = k - v1 + offs
                    v1 += offs
                    # print(pp[v1], pp[v2])
                    if cc[v1] != num or cc[v2] != num:
                        good = False
                        break
                if good:
                    return f
    return k



for q in range(int(sys.stdin.readline())):
    n = int(sys.stdin.readline())
    p = [int(j)-1 for j  in sys.stdin.readline().split()]
    c = [int(j)-1 for j  in sys.stdin.readline().split()]
    fnd = [0]*n
    ans = n+1
    for i in range(n):
        if not fnd[i]:
            ppp = [i]
            ccc = [c[i]]
            fnd[i] = 1
            j = p[i]
            while j != i:
                fnd[j] = 1
                ppp.append(j)
                ccc.append(c[j])
                j = p[j]
            # bb = 
            # print(bb)
            ans = min(ans, bestval(ppp, ccc))
    sys.stdout.write(str(ans) + '\n')