N = 5 * 10 ** 6 prime = [True] * (N + 1) prime[0] = prime[1] = False for i in range(2, N + 1): if prime[i]: for j in range(2 * i, N + 1, i): prime[j] = False for _ in range(int(input())): A, P = map(int, input().split()) if prime[P]: if A % P == 0: print(0) else: print(1) else: print(-1)