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.
def gcd(a, b):
    if a < b:
        a, b = b, a
    n = a//b
    r = a%b
    if r==0: return b
    return gcd(b, r)

def gcds(a, m):
    d = gcd(a, m)
    n = m//d
    add_primes(n)
    ans = n
    n0 = n
    for p in primes:
        if n0%p == 0:
            ans = ans*(p - 1)//p
        while n0%p ==0: n0 = n0//p
    if n0!=1: ans = ans*(n0 - 1)//n0
    if ans == n : ans = ans -1
    return int(max(1, ans))

def is_prime(n):
    for p in primes:
        if p*p>n: break
        if n%p ==0: return False
    return True

def add_primes(n):
   for i in range(n):
       if i*i>n: break
       if i<primes[-1]: continue
       if is_prime(i): primes.append(i)


if __name__ == '__main__':
    primes = [2, 3, 5, 7]
    t = int(input())
    for _ in range(t):
        a, m = [int(s) for s in input().split(' ')]
        print(gcds(a, m))




