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.
from collections import Counter
from math import gcd


def get_prime_set(ub):
    from itertools import chain
    from math import sqrt

    if ub < 4:
        return ({}, {}, {2}, {2, 3})[ub]

    ub, ub_sqrt = ub+1, int(sqrt(ub))+1
    primes = {2, 3} | set(chain(range(5, ub, 6), range(7, ub, 6)))
    du = primes.difference_update
    for n in chain(range(5, ub_sqrt, 6), range(7, ub_sqrt, 6)):
        if n in primes:
            du(range(n*3, ub, n*2))

    return primes


primes = get_prime_set(10**5+10)

for _ in range(int(input())):
    _a, _m = map(int, input().split())
    a, m = _a, _m
    d1, d2 = Counter(), Counter()
    for p in primes:
        while a % p == 0:
            d1[p] += 1
            a //= p
        while m % p == 0:
            d2[p] += 1
            m //= p
    if a > 1:
        d1[a] += 1
    if m > 1:
        d2[m] += 1

    a, m = _a, _m
    g = gcd(a, m)
    ans = (a+m-1) // g - (a-1) // g
    d = []
    for k, v in d2.items():
        if d1[k] < v:
            d.append(k)

    len_d = len(d)
    for bit in range(1, 2**(len_d)):
        popcount = 0
        cnt = 0
        div = g
        for i in range(len_d):
            if bit & (1 << i):
                popcount ^= 1
                div *= d[i]
        cnt += (a+m-1) // div - (a-1) // div
        ans += -cnt if popcount else cnt

    print(ans)
