import math t = int(input()) ap = [[int(i) for i in input().split()] for j in range(t)] def sieve_of_eratosthenes(n): prime = [True for i in range(n+1)] prime[0] = False prime[1] = False sqrt_n = math.ceil(math.sqrt(n)) for i in range(2, sqrt_n): if prime[i]: for j in range(2*i, n+1, i): prime[j] = False return prime val = 5*10**1 pri = sieve_of_eratosthenes(val) #print(kai) #exit() for i in range(t): a,p = ap[i] if pri[p] == True: if math.gcd(a,p) == 1: print(1) else: print(0) else: print(-1)