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.
#!/usr/bin/env python3
import os
import sys
from atexit import register
from io import StringIO

# Buffered output, at least 2x faster.
sys.stdout = StringIO()
register(lambda: os.write(1, sys.stdout.getvalue().encode()))

import math
###############################################################################

def totient(n):
    if n == 1:
        return 1
    for k in range(2,math.ceil(math.sqrt(n)+1)):
        if n % k == 0:
            p = 0
            while n%k == 0:
                p += 1
                n /= k
            return ((k**p) - k**(p-1))*totient(n)
    return n-1

T = int(input())
for _ in range(T):
    [a,m] = [int(k) for k in input().split()]
    gcd = math.gcd(m,a)
    print(int(totient(m/gcd)))
