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.

import math
import collections

def solve():
    a,m = map(int,input().split())
    primes = []
    t = math.gcd(a,m)
    ta,tm = a//t,m//t
    t = tm
    for i in range(2,1 + int(math.sqrt(t))):
        if(t%i):continue
        primes.append(i)
        while(t%i == 0):
            t=t//i
    if(t>1):primes.append(t)
    
    def getSum(threshold):
        mask = [0]
        cnt = [0]
        ans = [1]
        res = [0]
        def dfs(start,ans,res,mask,cnt):
            n = len(primes)
            if(ans[0]>1):
                sign = 1
                if(cnt[0]%2 == 0):
                    sign = -sign
                res[0]+=sign * (threshold-1)//ans[0]
            if(start>=n):
                return
            for i in range(start,n):
                tmm = 1<<i
                if(mask[0]&tmm): continue
                mask[0] += tmm
                cnt[0]+=1
                ans[0] *= primes[i]
                dfs(i+1,ans,res,mask,cnt)
                mask[0]-=tmm
                cnt[0]-=1
                ans[0] = ans[0]//primes[i]

        dfs(0,ans,res,mask,cnt)
        return res[0]
    print(tm-getSum(ta+tm)+getSum(ta))
def main():
    T = 1
    T = int(input())
    while (T):
        solve()
        T-=1

if __name__ == "__main__":
    main()