import sys input=sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 T=I() #nまでの素数を列挙(n loglogn) n = 5*(10**6)+10 #is_prime[i]にはi-1が素数か否かを示すboolが入る,[0]に1の素数判定がある. is_prime = [True]*(n+1) is_prime[0] = False for i in range(2, n+1): if is_prime[i-1]: j = 2 * i while j <= n: is_prime[j-1] = False j += i for i in range(T): A,P=MI() if is_prime[P+1]==1: if A%P==0: print(0) else: print(1) else: print(-1) main()