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.*;
//import java.math.*;
//import java.awt.Point;
 
public class Main {
	//static final long MOD = 998244353L;
	//static final long INF = -1000000000000000007L;
	static final long MOD = 1000000007L;
	//static final int INF = 1000000007;
	
	
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		int Q = sc.ni();
		for (int q = 0; q < Q; q++) {
			long A = sc.nl();
			long M = sc.nl();
			long G = gcd(A,M);
			HashMap<Long,Integer> Gfactor = new HashMap<Long,Integer>();
			long Gtemp = G;
			for (long i = 2; i*i <= G; i++) {
				int pow = 0;
				while (Gtemp % i == 0) {
					Gtemp /= i;
					pow++;
				}
				if (pow > 0) {
					Gfactor.put(i,pow);
				}
			}
			if (Gtemp > 1) {
				Gfactor.put(Gtemp,1);
			}
			
			HashMap<Long,Integer> Mfactor = new HashMap<Long,Integer>();
			long Mtemp = M;
			for (long i = 2; i*i <= M; i++) {
				int pow = 0;
				while (Mtemp % i == 0) {
					Mtemp /= i;
					pow++;
				}
				if (pow > 0) {
					Mfactor.put(i,pow);
				}
			}
			if (Mtemp > 1) {
				Mfactor.put(Mtemp,1);
			}
			
			long div = 1;
			ArrayList<Long> noDiv = new ArrayList<Long>();
			for (long divisor: Mfactor.keySet()) {
				int powM = Mfactor.get(divisor);
				int powG = Gfactor.getOrDefault(divisor, 0);
				if (powM == powG) {
					div *= ((long)Math.pow(divisor,powM));
				} else {
					div *= ((long)Math.pow(divisor,powG));
					noDiv.add(divisor);
				}
			}
			long ans = 0;
			for (int i = 0; i < (1<<noDiv.size()); i++) {
				long D = 1;
				for (int j = 0; j < noDiv.size(); j++) {
					if ((i&(1<<j)) > 0) {
						D *= noDiv.get(j);
					}
				}
				if (((double)D)*div > 20000000000.0) {
					//no change
					continue;
				}
				D *= div;
				long cnt = (A+M-1)/D-(A-1)/D;
				if (Integer.bitCount(i) % 2 == 0) {
					ans += cnt;
				} else {
					ans -= cnt;
				}
			}
			//pw.println(div);
			//pw.println(noDiv);
			pw.println(ans);
		}
		pw.close();
	}
	
	public static long dist(long[] p1, long[] p2) {
		return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1]));
	}
	
	//Find the GCD of two numbers
	public static long gcd(long a, long b) {
		if (a < b) return gcd(b,a);
		if (b == 0)
			return a;
		else
			return gcd(b,a%b);
	}
	
	//Fast exponentiation (x^y mod m)
	public static long power(long x, long y, long m) { 
		long ans = 1;
		x %= m;
		while (y > 0) { 
			if(y % 2 == 1) 
				ans = (ans * x) % m; 
			y /= 2;  
			x = (x * x) % m;
		} 
		return ans; 
	}
	
	public static int[][] shuffle(int[][] array) {
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    int[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		return array;
	}
	
    public static int[][] sort(int[][] array) {
    	//Sort an array (immune to quicksort TLE)
 
		Arrays.sort(array, new Comparator<int[]>() {
			  @Override
        	  public int compare(int[] arr1, int[] arr2) {
				  return arr1[0]-arr2[0]; //ascending order
	          }
		});
		return array;
	}
    
    public static long[][] sort(long[][] array) {
    	//Sort an array (immune to quicksort TLE)
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    long[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		Arrays.sort(array, new Comparator<long[]>() {
			  @Override
        	  public int compare(long[] arr1, long[] arr2) {
				  //return 0;
				  if (arr1[0] < arr2[0]) {
					  return -1;
				  } else if (arr1[0] > arr2[0]) {
					  return 1;
				  } else {
					  return 0;
				  }
	          }
		});
		return array;
	}
    
    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int ni() { 
            return Integer.parseInt(next()); 
        } 
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() { 
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
}