import math import sys def input(): return sys.stdin.buffer.readline()[:-1] MAXX = 5*10**6 + 5 isprime = [1]*MAXX isprime[0] = isprime[1] = 0 #theratos for num in range(2, MAXX): if isprime[num]: for k in range(num*2, MAXX, num): isprime[k] = 0 T = int(input()) for tt in range(T): A, P = map(int, input().split()) if isprime[P]: if A % P == 0: print(0) else: #ferma print(1) else: print(-1)