import sys input = sys.stdin.readline IsPrime = [True for _ in range(5 * 10**6 + 1)] IsPrime[0] = IsPrime[1] = False for i in range(2, 5 * 10**6 + 1): if IsPrime[i]: for j in range(i + i, 5 * 10**6 + 1, i): IsPrime[j] = False t = int(input()) for _ in range(t): a, p = map(int, input().split()) if IsPrime[p]: if a % p == 0: print(0) else: print(1) else: print(-1)