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.*;
import java.util.*;


public class Code0911 {


    public static void main(String[] args) {
        Reader s = new Reader();
        int t = s.nextInt();

        while (t-- > 0){
            long a = s.nextLong();
            long m = s.nextLong();

            long d = gcd(a,m);
            long k1 = a/d;
            long k3 = m/d;
            // gcd((a + x),m) = 1
            // x = d,2d,3d,...pd where p = (m-1)/d;
            // k2 = 1,2,3, ... m-1
            // k1 + k2 = 1 + k1, 2+ k1 ... p+k1
            // count[l , r] which has not common factor p1,p2,p3

            long p = (m-1)/d;
            List<Long> primes = findPrime(k3);
            System.out.println(count(p+k1,primes) - count(k1-1, primes));
        }
    }

    private static long count(long k1, List<Long> primes) {
          long ans = k1;

          for (long x:primes){
             ans = ans - ans/x;
          }

          return ans;
    }

    private static List<Long> findPrime(long m) {
        List<Long> primes = new ArrayList<>();
        long tm = m;
        for (long i = 2; i <= Math.sqrt(tm); i++) {
            if(m%i == 0){
                primes.add(i);
                while (m%i == 0){
                    m = m/i;
                }
            }
        }

        if(m > 1){
            primes.add(m);
        }
        return primes;
    }

    private static long gcd(long a, long m) {
        if(a%m == 0){
            return m;
        }
        return gcd(m,a%m);
    }

    static class Reader {

        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public Reader() {
            this(System.in);
        }

        public Reader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();

            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

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

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }

}
