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

public class Main {

    static long gcd(long a,long b) {
        long temp;
        while ((temp=a%b)!=0) {
            a=b;
            b=temp;
        }
        return b;
    }

    static ArrayList<long[]> factorization (long n) {
        ArrayList<long[]> list = new ArrayList<long[]>();
        long tmp= n ;
        for (int i=2;i<=(int)Math.sqrt(n);i++) {
            if (tmp%i==0) {
                long cnt=0;
                while (tmp%i==0) {
                    cnt++;
                    tmp/=i;
                }
                long[] x = {i, cnt};
                list.add(x);
            }
        }
        if (tmp!=1) {
            long[] x = {tmp, 1};
            list.add(x);
        }

        if (list.size()==0) {
            long[] x = {n, 1};
            list.add(x);
        }
        return list;
    }

    static long f(long x, long m) {
        // System.out.println("m "+m);
        ArrayList<long[]> tmp = factorization(m);
        // for (long[] aaa : tmp) System.out.println("m_fact "+Arrays.toString(aaa));
        ArrayList<Long> list = new ArrayList<Long>();
        int n = tmp.size();
        for (long[] arr : tmp) list.add(arr[0]);

        long ans = 0L;
        for (int i=0;i<(1<<n);i++) {
            if (i==0) continue;
            int[] arr = new int[n];
            int cnt = 0;
            for (int j=0;j<n;j++) {
                if ((i&(1<<j))==(1<<j)) arr[j]=1;
                cnt+=arr[j];
            }

            long num = 1L;
            for (int j=0;j<n;j++) {
                if (arr[j]==1) {
                    num *= list.get(j);
                }
            }
            long add = x/num;
            if (cnt%2==1) {
                ans+=add;
            } else { // cnt%2==0
                ans-=add;
            }
            // System.out.println(add);
            // System.out.println(Arrays.toString(arr));
        }
        // System.out.println(list);
        return ans;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i=0;i<t;i++) {
            long a = sc.nextLong();
            long m = sc.nextLong();
            long gcd = gcd(a, m);
            a/=gcd;
            m/=gcd;
            // System.out.println(f(a+m/gcd-1, m));
            // System.out.println(f(a, m));
            // System.out.println(f(a+m/gcd-1, m)-f(a, m));
            System.out.println(m-(f(a+m-1, m)-f(a, m)));
        }
    }
}