import sys input = sys.stdin.readline def calc(A,P): ANS=1 for i in range(1,P+1): ANS*=i return pow(A,ANS,P) import math x=5*10**6+10 L=math.floor(math.sqrt(x)) # 平方根を求める Primelist=[i for i in range(x+1)] Primelist[1]=0 # 1は素数でないので0にする. for i in Primelist: if i>L: break if i==0: continue for j in range(2*i,x+1,i): Primelist[j]=0 Primes=[Primelist[j] for j in range(x+1) if Primelist[j]!=0] S=set(Primes) T=int(input()) for tests in range(T): A,P=map(int,input().split()) if P in S: print(1) else: print(-1)