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 T = int(input()) for t in range(T): A, P = map(int, input().split()) if is_prime[P] == 0: print(-1) else: if A%P == 0: print(0) else: print(1)