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.
//package t;

import java.io.*;
import java.util.*;
 
public class utkarsh {
    
    BufferedReader br;
    PrintWriter out;
    
    long mod = (long) (1e9 + 7), inf = (long) (3e18);
    
    void solve() {
        int t = ni();
        while(t-- > 0) {
            long a = nl(), m = nl();
            long d = gcd(a, m);
            long L = a / d;
            long R = L + m / d - 1;
            ArrayList <Long> pf = new ArrayList<>();
            m /= d;
            for(long i = 2; i*i <= m; i++) {
                if(m % i == 0) {
                    pf.add(i);
                    while(m % i == 0)   m /= i;
                }
            }
            if(m > 1)   pf.add(m);
            int n = pf.size();
            long ans = R - L + 1;
            //out.println(n +" "+ L +" "+ R);
            for(int i = 1; i < (1 << n); i++) {
                int cnt = 0;
                long p = 1;
                for(int j = 0; j < n; j++) {
                    if((i & (1 << j)) > 0) {
                        cnt++;  p *= pf.get(j);
                    }
                }
                //out.println("p = " + p);
                if(cnt % 2 == 0)    ans += (R / p) - ((L-1) / p);
                else                ans -= (R / p) - ((L-1) / p);
            }
            out.println(ans);
        }
    }
    
    long gcd(long a, long b) {
        if(b == 0)  return a;
        return gcd(b, a % b);
    }
    
    long mp(long b, long e) {
        long r = 1;
        while(e > 0) {
            if( (e&1) == 1 )    r = (r * b) % mod;
            b = (b * b) % mod;
            e >>= 1;
        }
        return r;
    }
    
    // -------- I/O Template -------------
    
    char nc() {
        return ns().charAt(0);
    }
    
    String nLine() {
        try {
            return br.readLine();
        } catch(IOException e) {
            return "-1";
        }
    }
    
    double nd() {
        return Double.parseDouble(ns());
    }
    
    long nl() {
        return Long.parseLong(ns());
    }
    
    int ni() {
        return Integer.parseInt(ns());
    }
    
    StringTokenizer ip;
    
    String ns() {
        if(ip == null || !ip.hasMoreTokens()) {
            try {
                ip = new StringTokenizer(br.readLine());
            } catch(IOException e) {
                throw new InputMismatchException();
            }
        }
        return ip.nextToken();
    }
    
    void run() {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }
    
    public static void main(String[] args) {
        new utkarsh().run();
    }
}
