You are given a colored permutation p_1, p_2, ..., 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]]] ... where all elements have same color (c[i] = c[p[i]] = c[p[p[i]]] = ...).

We can also define a multiplication of permutations a and b as permutation c = a × b where c[i] = b[a[i]]. Moreover, we can define a power k of permutation p as p^k=\underbrace{p × p × ... × p}_{k  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 ≤ T ≤ 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 ≤ n ≤ 2 ⋅ 10^5) — the size of the permutation.

The second line contains n integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ n, p_i ≠ p_j for i ≠ j) — the permutation p.

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

It is guaranteed that the total sum of n doesn't exceed 2 ⋅ 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, ... 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, ... is an infinite path since c_4 = c_8 = 4.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class infpath {
    public static void shift(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i]--;
        }
    }
    public static class F {
        int prime, count;
        public F(int prime, int count) {
            this.prime = prime;
            this.count = count;
        }

        @Override
        public String toString() {
            return "F{" +
                "prime=" + prime +
                ", count=" + count +
                '}';
        }
    }
    public static ArrayList<Integer> getFactors(ArrayList<F> pfact) {
        ArrayList<Integer> factors = new ArrayList<>();
        factors.add(1);
        for (F f : pfact) {
            int[] powers = new int[f.count + 1];
            powers[0]=1;
            for (int i = 1; i <= f.count; i++) {
                powers[i] = powers[i-1] * f.prime;
            }
            int fsize = factors.size();
            for (int i = 0; i < fsize; i++) {
                for (int j = 1; j <= f.count; j++) {
                    factors.add(factors.get(i)*powers[j]);
                }
            }
        }
        return factors;
    }
    public static void main(String[] args) throws Exception {
        R in = new R();
        ArrayList<Integer> primes = new ArrayList<>();
        ArrayList<ArrayList<F>> primeFactors = new ArrayList<>();
        primeFactors.add(new ArrayList<>());
        primeFactors.add(new ArrayList<>());
        for (int i = 2; i < 200005; i++) {
            int k = i;
            ArrayList<F> curr = new ArrayList<>();
            primeFactors.add(curr);
            for (int p: primes) {
                int count = 0;
                while (k % p == 0) {
                    k /= p;
                    count++;
                }
                if (count != 0) curr.add(new F(p, count));
                if (p * p > k) break;
            }
            if (k!=1) {
                curr.add(new F(k, 1));
                if (k==i) {
                    primes.add(i);
                }
            }
        }
        int TESTCASES = in.nextInt();
        StringBuilder out = new StringBuilder();
        for (int TC = 0; TC < TESTCASES; TC++) {
            int n = in.nextInt();
            int[] p = in.nextIntLine();
            int[] c = in.nextIntLine();
            boolean[] visited = new boolean[n];
            shift(p);
            ArrayList<ArrayList<Integer>> paths = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if (!visited[i]) {
                    ArrayList<Integer> curr = new ArrayList<>();
                    paths.add(curr);
                    int i2 = i;
                    do {
                        curr.add(i2);
                        visited[i2]=true;
                        i2 = p[i2];
                    } while (i2 != i);
                }
            }
            int min = Integer.MAX_VALUE;
            for (ArrayList<Integer> path : paths) {
                ArrayList<F> pfact = primeFactors.get(path.size());
                ArrayList<Integer> factors = getFactors(pfact);
                int minfactor = path.size();
                for (int i = 0; i < factors.size() - 1; i++) {
                    int factor = factors.get(i);
                    for (int j = 0; j < factor; j++) {
                        boolean flag = true;
                        for (int k = j+factor; k < path.size(); k+=factor) {
                            if (c[path.get(k)] != c[path.get(j)]) {
                                flag = false;
                                break;
                            }
                        }
                        if (flag) {
                            minfactor=Math.min(minfactor,factor);
                            break;
                        }
                    }
                }
                min = Math.min(minfactor, min);
            }
            out.append(min);
            out.append('\n');
        }
        System.out.print(out);
        System.out.flush();
    }
    //<editor-fold desc="R">

    /**
     * This class is for fast input. Please ignore.
     */
    public static class R {
        private BufferedReader br;
        /**
         * Should be set to null at end of line
         */
        private StringTokenizer st;

        public R() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public R(String filename) throws IOException {
            br = new BufferedReader(new FileReader(filename + ".in"));
        }

        public R(BufferedReader reader) {
            br = reader;
        }

        public BufferedReader getReader() {
            return br;
        }

        public StringTokenizer getStringTokenizer() {
            return st;
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s == null) return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        /**
         * Note:
         * CAN MODIFY the BufferedReader's location and the string tokenizer!!!
         * Recommended to only use with next().
         */
        public boolean lineHasNext() throws IOException {
            if (st == null) {
                String s = br.readLine();
                if (s == null) return false;
                st = new StringTokenizer(s);
            }
            return st.hasMoreTokens();
        }

        /**
         * Note:
         * CAN MODIFY the BufferedReader's location and the string tokenizer!!!
         * Recommended to only use with next().
         */
        public boolean hasNext() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s == null) return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        /**
         * Skips a line. Sets st to null if has tokens left, and otherwise
         * reads a line.
         */
        public void skipLine() throws IOException {
            if (st == null || !st.hasMoreTokens()) {
                br.readLine(); // Otherwise, would do nothing.
            }
            st = null;
        }

        /**
         * This will set st to null, and this ignores current line
         */
        public String[] nextLine() throws IOException {
            String s = br.readLine();
            if (s == null) return null;
            st = new StringTokenizer(s);
            ArrayList<String> result = new ArrayList<>();
            while (st.hasMoreTokens()) {
                result.add(st.nextToken());
            }
            st = null;
            return result.toArray(new String[0]);
        }

        /**
         * sets st to null!
         */
        public String[] nextTower(int lines) throws IOException {
            String[] tower = new String[lines];
            st = null;
            for (int i = 0; i < lines; i++) {
                tower[i] = br.readLine();
            }
            return tower;
        }

        public int[] nextIntLine() throws IOException {
            return intArr(nextLine());
        }

        public long[] nextLongLine() throws IOException {
            return longArr(nextLine());
        }

        public int[] nextIntTower(int lines) throws IOException {
            return intArr(nextTower(lines));
        }

        public long[] nextLongTower(int lines) throws IOException {
            return longArr(nextTower(lines));
        }

        public int[] intArr(String[] strings) throws IOException {
            int[] ints = new int[strings.length];
            int i = 0;
            for (String s : strings) {
                ints[i] = Integer.parseInt(s);
                i++;
            }
            return ints;
        }

        public long[] longArr(String[] strings) throws IOException {
            long[] longs = new long[strings.length];
            int i = 0;
            for (String s : strings) {
                longs[i] = Long.parseLong(s);
                i++;
            }
            return longs;
        }

        public double[] doubleArr(String[] strings) {
            double[] doubles = new double[strings.length];
            int i = 0;
            for (String s : strings) {
                doubles[i] = Double.parseDouble(s);
                i++;
            }
            return doubles;
        }

        /**
         * This will set st to null
         */
        public char[] nextCharArray() throws IOException {
            st = null;
            String s = br.readLine();
            return s == null ? null : s.toCharArray();
        }

        /**
         * This will set st to null
         * Boolean at pos i true if char at pos i == c
         */
        public boolean[] nextBoolArray(char c) throws IOException {
            char[] chars = nextCharArray();
            if (chars == null) return null;
            boolean[] booleans = new boolean[chars.length];
            for (int i = 0; i < chars.length; i++) {
                booleans[i] = chars[i] == c;
            }
            return booleans;
        }

        public int[][] next2Dint(int lines) throws IOException {
            int[][] result = new int[lines][];
            for (int i = 0; i < lines; i++) {
                result[i] = nextIntLine();
            }
            return result;
        }

        public long[][] next2Dlong(int lines) throws IOException {
            long[][] result = new long[lines][];
            for (int i = 0; i < lines; i++) {
                result[i] = nextLongLine();
            }
            return result;
        }

        public char[][] next2Dchar(int lines) throws IOException {
            char[][] result = new char[lines][];
            for (int i = 0; i < lines; i++) {
                result[i] = nextCharArray();
            }
            return result;
        }

        public boolean[][] next2Dbool(int lines, char c) throws IOException {
            boolean[][] result = new boolean[lines][];
            for (int i = 0; i < lines; i++) {
                result[i] = nextBoolArray(c);
            }
            return result;
        }
    }
    //</editor-fold>
}