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.
"""
Author: guiferviz
Time: 2020-01-29 20:26:47
"""

import math


def prime_factors(n):
    assert n > 0
    if n == 1:
        return [1]

    f = []
    # Check 2 first for saving half iterations in the next for loop.
    while n % 2 == 0:
        f.append(2)
        n /= 2
    # Check the odd numbers.
    for i in range(3, int(n**0.5) + 1, 2):
        while n % i == 0:
            f.append(i)
            n /= i

    # Prime number case.
    if n > 1:
        f.append(int(n))

    return f


def factors_in(i, e, p):
    d = 0
    n = i + (p - i % p)
    while n >= i and n < e:
        d += 1
        n += p
    return d


def solve_slow():
    a, m = map(int, input().split())
    gcd = math.gcd(a, m)
    sol = 0
    for x in range(m):
        if gcd == math.gcd(a + x, m):
            sol += 1
    print(sol)


def eulers_phi_fun(n):
    f = prime_factors(n)
    val = n
    for i in set(f):
        val *= 1 - 1 / i
    return int(val)


def solve():
    a, m = map(int, input().split())
    gcd = math.gcd(a, m)
    n = m / gcd
    phi = eulers_phi_fun(n)
    print(phi)


def main():
    t = int(input())
    for _ in range(t):
        solve()


if __name__ == "__main__":
    main()

