prime=set([]) def factorization(n): res=[0 for i in range(n+1)] for i in range(2,n+1): if not res[i]: prime.add(i) for j in range(1,(n)//i+1): res[i*j]+=1 factorization(5*10**6) import sys input=sys.stdin.buffer.readline for _ in range(int(input())): a,p=map(int,input().split()) if p in prime: if a==0: print(0) else: print(1) else: print(-1)