import sys prime = [1] * 5000001 prime[0] = prime[1] = 0 for i in range(2, 5000001): if prime[i]: for j in range(2 * i, 5000001, i): prime[j] = 0 for _ in range(int(sys.stdin.readline())): A, P = map(int, sys.stdin.readline().split()) if prime[P]: if A % P: print(1) else: print(0) else: print(-1)