is_Prime = [True for i in range(5*10**6+1)] is_Prime[0] = is_Prime[1] = False for i in range(2, 5*10**6+1): if is_Prime[i] == False: continue for j in range(2, 5*10**6+1): if i*j > 5*10**6: break is_Prime[i*j] = False T = int(input()) que = [tuple(map(int, input().split())) for i in range(T)] for A, P in que: if is_Prime[P]: if A % P == 0: print(0) else: print(1) else: print(-1)