import math def isprime(n): if n < 2: return False if n == 2: return True if n % 2 == 0: return False for i in range(3, int(math.sqrt(n)) + 1, 2): if n % i == 0: return False return True t=int(input()) for _ in range(t): a,p=map(int,input().split()) if isprime(p)==False: print(-1) else: if a%p==0: print(0) else: print(1)