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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Jenish
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DSameGCDs solver = new DSameGCDs();
        solver.solve(1, in, out);
        out.close();
    }

    static class DSameGCDs {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {


        /*

                gcd(1,15) = 1
                gcd(2,15) = 1
                gcd(4,15) = 1
                gcd(7,15) = 1
                gcd(8,15) = 1
                gcd(11,15) = 1
                gcd(13,15) = 1
                gcd(14,15) = 1


                gcd(3,15) = 3
                gcd(6,15) = 3
                gcd(9,15) = 3
                gcd(12,15) = 3


                gcd(5,15) = 5
                gcd(10,15) = 5

                gcd(15,15) = 15

         */


            int t = in.scanInt();
            while (t-- > 0) {
                long a = in.scanLong();
                long m = in.scanLong();
                out.println(CodeX.totient(m / CodeX.GCD(a, m)));
            }
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        public int scanInt() {
            int I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

        public long scanLong() {
            long I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

    }

    static class CodeX {
        public static long totient(long x) {
            long ans = x;
            for (long i = 2; i * i <= x; i++) {
                if ((x % i) != 0) continue;
                while (x % i == 0) x /= i;
                ans -= ans / i;
            }
            if (x > 1)
                ans -= ans / x;
            return ans;
        }

        public static long GCD(long A, long B) {
            if (B == 0)
                return A;
            else
                return GCD(B, A % B);
        }

    }
}

