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;
const int INF = 1e9;
int n, h;
long long dp[(long long)2e4 + 1];
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long solve_stupid(long long a, long long m) {
  long long g = gcd(a, m);
  int cnt = 0;
  for (int x = 0; x < m; x++) {
    if (gcd(x + a, m) == g) cnt++;
  }
  return cnt;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long a, m;
    cin >> a >> m;
    vector<long long> factors;
    long long num = m;
    long long g = gcd(m, a);
    num /= g;
    for (long long i = 2; i * i <= m; i++) {
      if (num % i) continue;
      factors.push_back(i);
      while (num % i == 0) num /= i;
    }
    if (num > 1) factors.push_back(num);
    int k = factors.size();
    long long cnt = 0;
    for (int i = 0; i < (1 << k); i++) {
      long long div = g;
      int tot = 0;
      for (int j = 0; j < k; j++) {
        if ((1 << j) & i) {
          div *= factors[j];
          tot++;
        }
      }
      if (tot % 2) {
        cnt -= (m + a) / div;
        cnt += (a) / div;
      } else {
        cnt += (m + a) / div;
        cnt -= (a) / div;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
