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.
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
# mod=1000000007
mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('0')

file = 1
def ceil(a,b):
    return (a+b-1)//b

# write fastio for getting fastio template.
    

def func(m):
	x = []
	for i in range(2,int(sqrt(m))+1):
		c = 0
		while m%i==0:
			m //= i
			c +=1
		if c > 0:
			x.append(i)
	if m > 1:
		x.append(m)
	return x


def solve():


    for _ in range(ii()):
        a,m = mi()
        r = a+m-1

        def get(g):
        	return r//g - (a-1)//g
        gcd1 = gcd(a,m)
        tot = get(gcd1)
        m //= gcd1
        x = func(m)
        n = len(x)
        sub = 0
        for i in range(1,1<<n):
        	c,g = 0,1
        	for j in range(n):
        		if (i>>j)&1:
        			g1 = gcd(g,x[j])
        			g = (g*x[j])//g1
        			c+=1
        	if c&1:
        		sub += get(g*gcd1)
        	else:
        		sub -= get(g*gcd1)
        print(tot-sub)










            





    
        








        
if __name__ =="__main__":

    if(file):

        if path.exists('input.txt'):
            sys.stdin=open('input.txt', 'r')
            sys.stdout=open('output.txt','w')
        else:
            input=sys.stdin.readline
    solve()
