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 const N = 1000005, mod = 1000000007, M = 6005;
long long n, m, k, t, i, j, ans, kaskad[N];
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
void solve() {
  long long a, b;
  cin >> a >> b;
  long long g = gcd(a, b);
  a /= g;
  b /= g;
  m = b;
  vector<long long> vec;
  for (long long i = 2; i * i <= b; ++i) {
    if (b % i == 0) {
      vec.push_back(i);
      while (b % i == 0) b /= i;
    }
  }
  if (b != 1) vec.push_back(b);
  long long SIZE = vec.size();
  long long bin = (1 << SIZE);
  ans = 0;
  for (int i = 0; i < bin; ++i) {
    long long x = i;
    long long y = 1;
    long long cnt = 0;
    for (int j = 0; j < SIZE; ++j) {
      if (x % 2 == 1) {
        y *= vec[j];
        cnt++;
      }
      x /= 2;
    }
    if (cnt % 2 == 0)
      ans += m / y;
    else
      ans -= m / y;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
