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.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author KharYusuf
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(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, FastReader s, PrintWriter w) {
            int tt = s.nextInt();
            while (tt-- > 0) {
                long a = s.nextLong(), m = s.nextLong();
                long gcd = func.gcd(a, m);
                a /= gcd;
                m /= gcd;
                long md = m - 1, t = m;
                int sq = (int) Math.sqrt(m);
                ArrayList<Long> pr = new ArrayList<>();
                for (long i = 2; i <= sq; i++) {
                    if (t % i == 0) {
                        while (t % i == 0) t /= i;
                        pr.add(i);
                    }
                }
                if (t != 1) pr.add(t);
                int MX = 1 << pr.size();
                long ans = 0;
                for (int i = 1; i < MX; i++) {
                    long num = 1;
                    int temp = i, cur = 0, o = 0;
                    while (temp > 0) {
                        if (temp % 2 == 1) {
                            o++;
                            num *= pr.get(cur);
                        }
                        cur++;
                        temp >>= 1;
                    }
                    if (o % 2 == 0) {
                        ans -= (a + md) / num - a / num;
                        //w.println((a+md)/num-a/num+" "+num+" "+i+" "+ans);
                    } else {
                        ans += (a + md) / num - a / num;
                        //w.println((a+md)/num-a/num+" "+num+" "+i+" "+ans);
                    }
                }
                w.println(md - ans + 1);
            }
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {

            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {

                curChar = 0;

                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }

            return buf[curChar++];
        }

        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 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 boolean isSpaceChar(int c) {

            if (filter != null)
                return filter.isSpaceChar(c);

            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

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

    }
}

