n = 5*10**6+2 is_prime = [1]*(n + 1) is_prime[0] = 0 is_prime[1] = 0 for p in range(2, n + 1): if is_prime[p]: for q in range(2*p, n + 1, p): is_prime[q] = 0 primes = set() for i in range(n): if is_prime[i] == 1: primes.add(i) T = int(input()) for t in range(T): A, P = map(int, input().split()) if P not in primes: print(-1) else: if A%P == 0: print(0) else: print(1)