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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

/**
 *
 */

public class TaskD {

    public static void main(String[] arg) {
        final FastScanner in = new FastScanner(System.in);
        final PrintWriter out = new PrintWriter(System.out);
        int T = in.nextInt();
        for(int t=0;t<T;t++){
            final long a = in.nextLong();
            final long m = in.nextLong();
            final long mI=m/gcd(a,m);
            out.printf("%d%n",
                    mathSol(a,m));
        }
        out.close();
        in.close();
    }


    private static long mathSol(final long a,
                                final long m){
        long m1=m/gcd(a, m);
        return phi(m1);
    }

    private static long phi( long n){
        long result =n;
        for(long p=2;p*p<=n;p++){
            if(n%p == 0){
                while(n%p==0){
                    n/=p;
                }
                result-=result/p;
            }
        }
        if(n >1){
            result-=result/n;
        }
        return result;
    }

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


    private static long bf(final long a,
                           final long m){
        final long gcd = gcd(a, m);
        long count = 1;
        for(long i=a+1;i<a+m;i++){
            final long ngcd = gcd(i,m);
            if(ngcd==gcd){
                count++;
            }
        }
        return count;
    }


    private static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(InputStream stream) {
            try {
                br = new BufferedReader(new InputStreamReader(stream));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

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

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

        int[] readIntArr(int n) {
            int[] result = new int[n];
            for (int i = 0; i < n; i++) {
                result[i] = Integer.parseInt(next());
            }
            return result;
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

    }
}
