You are given two integers a and m. Calculate the number of integers x such that 0 ≤ x < m and \gcd(a, m) = \gcd(a + x, m).

Note: \gcd(a, b) is the greatest common divisor of a and b.

Input

The first line contains the single integer T (1 ≤ T ≤ 50) — the number of test cases.

Next T lines contain test cases — one per line. Each line contains two integers a and m (1 ≤ a < m ≤ 10^{10}).

Output

Print T integers — one per test case. For each test case print the number of appropriate x-s.

Example

Input


3
4 9
5 10
42 9999999967


Output


6
1
9999999966

Note

In the first test case appropriate x-s are [0, 1, 3, 4, 6, 7].

In the second test case the only appropriate x is 0.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

import static java.lang.Math.sqrt;

public class SolutionD extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionD(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    public static long gcd(long a, long b) {
        return (a == 0) ? b : gcd(b % a, a);
    }


    static class PowerPair {
        public long base;
        int exp;

        public PowerPair(long base, int exp) {
            this.base = base;
            this.exp = exp;
        }

        @Override
        public String toString() {
            return base + "^" + exp;
        }
    }

    public static List<PowerPair> getPrimeFactorsOfNumber(long n) {
        List<PowerPair> result = new ArrayList<>();

        for (int i = 2; i <= sqrt(n); i++) {
            PowerPair pair = new PowerPair(i, 0);
            while (n % i == 0) {
                pair.exp++;
                n /= i;
            }

            if (pair.exp > 0) {
                result.add(pair);
            }
        }

        if (n != 1) {
            result.add(new PowerPair(n, 1));
        }

        return result;
    }

    private static void solve() {
        long a = scanner.nextLong();
        long m = scanner.nextLong();

        long d = gcd(a, m);
        List<PowerPair> primeFactorsM = getPrimeFactorsOfNumber(m);

        long y = d;

        List<Long> remainingPrimeFactors = new ArrayList<>();
        for (PowerPair primeFactor: primeFactorsM) {
            while (y % primeFactor.base == 0) {
                y /= primeFactor.base;
                primeFactor.exp--;
            }

            if (primeFactor.exp > 0) {
                remainingPrimeFactors.add(primeFactor.base);
            }
        }

        long result = 0;
        for (int mask = 0; mask < (long) Math.pow(2, remainingPrimeFactors.size()); mask++) {
            int amountBits = Integer.bitCount(mask);
            long primeFactor = 1;
            for (int i = 0; i < remainingPrimeFactors.size(); i++) {
                if (((mask & (1 << i)) != 0)) {
                    primeFactor *= remainingPrimeFactors.get(i);
                }
            }

            long amount = m / (d * primeFactor) - 1;
            if (amountBits % 2 == 0) {
                result += amount;
            } else {
                result -= amount;
            }
        }
        out.println(result);
    }
}