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;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using VecVec = vector<Vec>;
using Graph = vector<VecP>;
template <typename T>
using Vec1 = vector<T>;
template <typename T>
using Vec2 = vector<Vec1<T> >;
template <typename T>
bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
ll pow2(const T n) {
  return (1LL << n);
}
template <typename T>
void cosp(const T n) {
  cout << n << ' ';
}
template <typename T>
void co(const T n) {
  cout << n << endl;
}
template <typename T1, typename T2>
void co(pair<T1, T2> p) {
  cout << p.first << ' ' << p.second << endl;
}
template <typename T>
void co(const Vec1<T> &v) {
  for (T i : v) cosp(i);
  cout << endl;
}
template <typename T>
void co(initializer_list<T> v) {
  for (T i : v) cosp(i);
  cout << endl;
}
void sonic() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ld EPS = 1e-11;
Vec pn;
void init() {
  pn.push_back(2);
  for (ll(num) = (3); (num) < (2e5); ++(num)) {
    bool flg = true;
    for (ll i : pn) {
      if (num % i == 0) {
        flg = false;
        break;
      }
      if (i * i > num) break;
    }
    if (flg) pn.push_back(num);
  }
}
ll gcd(ll a, ll b) {
  if (!b) return a;
  return gcd(b, a % b);
}
bool isPrime(ll n) {
  for (ll i : pn) {
    if (i * i > n) return true;
    if (n % i == 0) return false;
  }
  return true;
}
void solve() {
  ll a, m;
  cin >> a >> m;
  ll g = gcd(a, m);
  bool flg = true;
  if (isPrime(m)) {
    co(m - 1);
    return;
  }
  ll p = m / g;
  if (isPrime(p)) {
    co(p - 1);
    return;
  }
  ll ans = p;
  for (ll i : pn) {
    if (p % i == 0) {
      ans /= i;
      ans *= i - 1;
      while (p % i == 0) p /= i;
    }
  }
  if (p != 1) {
    ans /= p;
    ans *= p - 1;
  }
  co(ans);
}
int main(void) {
  ll t;
  cin >> t;
  init();
  for (ll(i) = (0); (i) < (t); ++(i)) solve();
  return 0;
}
