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.
/*
javac d.java && java d
*/

import java.io.*;
import java.util.*;

public class d {
	public static void main(String[] args) { new d(); }
	FS in = new FS();
	PrintWriter out = new PrintWriter(System.out);

	int t;
	long a, m, g;
	int ptr;
	long[] use;
	ArrayList<long[]> mf, gf;

	d() {
		t = in.nextInt();
		while (t-- > 0) {
			a = in.nextLong();
			m = in.nextLong();
			long tmp = m;
			mf = new ArrayList<>();	
			for (long i = 2; i * i <= m; i++)
				if (tmp %i == 0) {
					long[] p = {i, 0};
					while (tmp %i == 0) {
						tmp /= i;
						p[1]++;
					}
					mf.add(p);
				}
			if (tmp > 1) mf.add(new long[]{tmp, 1});

			g = gcd(a, m);
			tmp = g;
			gf = new ArrayList<>();
			for (long i = 2; i * i <= g; i++)
				if (tmp %i == 0) {
					long[] p = {i, 0};
					while (tmp %i == 0) {
						tmp /= i;
						p[1]++;
					}
					gf.add(p);
				}
			if (tmp > 1) gf.add(new long[]{tmp, 1});

			ptr = 0;
			use = new long[30];
			int msz = mf.size(), gsz = gf.size();
			for (int i = 0; i < msz; i++) {
				int st = -1;
				long bt = 0;
				long[] p = mf.get(i);
				for (int j = 0; j < gsz; j++) {
					long[] q = gf.get(j);
					if (p[0] == q[0]) 
						st = (p[1] == q[1]) ? 0 : 1;
				}
				if (st == -1) use[ptr++] = p[0];
				if (st == 1) use[ptr++] = p[0];
			}
		//	out.println(Arrays.toString(use));

			long lo = a, hi = a + m - 1;
	    //  out.println(lo + " " + hi);
			long ans = count(hi) - count(lo - 1);
			out.println(ans);
		}
		out.close();
	}

	long count(long b) {
		long ans = 0;
		b /= g;
		for (int i = 0; i < (1 << ptr); i++) {
			int sgn = ((Integer.bitCount(i) & 1) > 0) ? -1 : 1;
			long pd = 1L;
			for (int j = 0; j < ptr; j++)
				if ((i & (1 << j)) > 0)
					pd *= use[j];
			ans += sgn * (b / pd);
		}
	//	out.printf("---------------> %d %d%n", b, ans);
		return ans;
	}
	long pow(long x, long y) {
		long res = 1L;
		while (y > 0) {
			if ((y & 1) > 0) res = res * x;
			x = x * x;
			y >>= 1L;
		} return res;
	}
	
	int abs(int x) { if (x < 0) return -x; return x; }
	long abs(long x) { if (x < 0) return -x; return x; }
	int min(int a, int b) { if (a < b) return a; return b; }	
	int max(int a, int b) { if (a > b) return a; return b; }	
	long min(long a, long b) { if (a < b) return a; return b; }	
	long max(long a, long b) { if (a > b) return a; return b; }	
	int gcd(int a, int b) { while (b > 0) { a = b^(a^(b = a)); b %= a; } return a; }
	long gcd(long a, long b) { while (b > 0) { a = b^(a^(b = a)); b %= a; } return a; }
	long lcm(int a, int b) { return (((long) a) * b) / gcd(a, b); }
	long lcm(long a, long b) { return (a * b) / gcd(a, b); }

	void sort(int[] arr) {
		int sz = arr.length, j;
		Random r = new Random();
		for (int i = 0; i < sz; i++) {
			j = r.nextInt(i + 1);
			arr[i] = arr[j]^(arr[i]^(arr[j] = arr[i]));
		} Arrays.sort(arr);
	}

	class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch (Exception e) {}
			} return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next()); }
		long nextLong() { return Long.parseLong(next()); }
		double nextDouble() { return Double.parseDouble(next()); }
	}
}

