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>
using namespace std;
long long int mgcd(long long int a, long long int b) {
  if (b % a == 0)
    return (a);
  else
    return (mgcd(b % a, a));
}
long long int cal(long long int s, long long int e, long long int p) {
  long long int l, r;
  if (s % p == 0)
    l = s;
  else {
    l = s / p;
    l = (p * (l + 1));
  }
  if (e % p == 0)
    r = e;
  else {
    r = e / p;
    r = r * p;
  }
  long long int an = (r - l) / p + 1;
  return (an);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (; t > 0; --t) {
    long long int a, b, a1, b1;
    cin >> a1 >> b1;
    a = min(a1, b1);
    b = max(a1, b1);
    long long int gcd = 1;
    vector<long long int> fac;
    gcd = mgcd(a, b);
    long long int x = b / gcd, s = sqrt(x);
    for (long long int i = 2; i <= s; ++i) {
      if (x % i == 0) {
        fac.push_back(i);
        while (x % i == 0) {
          x /= i;
        }
      }
    }
    if (x != 1) {
      fac.push_back(x);
    }
    x = pow(2, fac.size());
    long long int ans = 0;
    for (long long int i = 1; i < x; ++i) {
      long long int c = 0, bi = 0, pr = 1;
      while (bi != fac.size()) {
        if ((i & (1LL << bi)) > 0) {
          c++;
          pr *= fac[bi];
        }
        bi++;
      }
      long long int cans = cal(a, a + b - 1, pr * gcd);
      if (c % 2 == 1)
        ans += cans;
      else
        ans -= cans;
    }
    cout << cal(a, a + b - 1, gcd) - ans << endl;
  }
  return (0);
}
