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.
#include <bits/stdc++.h>
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; c > '9' || c < '0'; c = getchar()) {
    if (c == '-') f = -1;
  }
  for (; c >= '0' && c <= '9'; c = getchar()) {
    x = x * 10 + c - '0';
  }
  return x * f;
}
long long que[100005], top, sol[100005];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
inline void divide(long long x) {
  for (long long i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      que[top++] = i;
      for (; x % i == 0; x /= i)
        ;
    }
  }
  if (x != 1) que[top++] = x;
}
inline long long solve(long long x) {
  long long t = 0, sum = 0, z;
  sol[t++] = -1;
  for (long long i = 0; i < top; i++) {
    z = t;
    for (long long j = 0; j < z; j++) {
      sol[t++] = sol[j] * que[i] * (-1);
    }
  }
  for (long long i = 1; i < t; i++) sum += x / sol[i];
  return sum;
}
inline void work() {
  for (long long t = read(); t; t--) {
    long long a = read(), m = read();
    long long g = gcd(a, m);
    top = 0;
    a /= g, m /= g;
    divide(m);
    printf("%lld\n", m - solve(m + a - 1) + solve(a - 1));
  }
}
int32_t main() {
  work();
  return 0;
}
