import math import random import math n = 5000001 A=list(range(2,n+1)) Q=list() while A[0]**2<=n: tmp=A[0] Q.append(tmp) A=[e for e in A if e%tmp!=0] from sys import stdin #print(Q[0]) Q = set(Q) t = int(input()) for i in range(t): a,p = map(int,stdin.readline().rstrip().split()) if p in Q: if a % p == 0: print(0) else: print(1) else: print(-1)