import math 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 P = sieve_of_eratosthenes(5*10**6) T = int(input()) for t in range(T): a,p = map(int,input().split()) if P[p]: if a % p == 0: print(0) else: print(1) else: print(-1)