def seachPrimeNum(N): max = int(N**0.5) seachList = [i for i in range(2,N+1)] primeNum = [] while seachList[0] <= max: primeNum.append(seachList[0]) tmp = seachList[0] seachList = [i for i in seachList if i % tmp != 0] primeNum.extend(seachList) return primeNum PL = set(seachPrimeNum(5*10**6)) for _ in range(int(input())): a,p = map(int,input().split()) if p not in PL: print(-1) elif a % p == 0: print(0) else: print(1)