import math def is_prime(n): if n == 1: return False for k in range(2, int(math.sqrt(n)) + 1): if n % k == 0: return False return True T=int(input()) for i in range(T): A,P=map(int,input().split()) if is_prime(P)==False: print(-1) else: if A%P==0: print(0) else: print(1)