------
## 最终结论
```python
def find_min_k(test_cases):
    results = []
    
    for n, p, c in test_cases:
        visited = [False] * n
        min_k = float('inf')
        
        for i in range(n):
            if not visited[i]:
                cycle = []
                current = i
                
                # Find the cycle starting from i
                while not visited[current]:
                    visited[current] = True
                    cycle.append(current)
                    current = p[current] - 1  # Convert to 0-indexed
                
                # Check if all colors in the cycle are the same
                color_set = {c[idx] for idx in cycle}
                
                if len(color_set) == 1:  # All colors are the same
                    cycle_length = len(cycle)
                    
                    # Find all factors of cycle_length
                    for k in range(1, cycle_length + 1):
                        if cycle_length % k == 0:
                            min_k = min(min_k, k)
        
        results.append(min_k)
    
    return results

# Example usage with input reading code included
import sys

input = sys.stdin.read
data = input().splitlines()

T = int(data[0])
index = 1
test_cases = []

for _ in range(T):
    n = int(data[index])
    p = list(map(int, data[index + 1].split()))
    c = list(map(int, data[index + 2].split()))
    test_cases.append((n, p, c))
    index += 3

results = find_min_k(test_cases)
print("\n".join(map(str, results)))
```