def eratosthenes_sieve(n): table = [0] * (n+1) prime_list = [] for i in range(2, n+1): if table[i] == 0: prime_list.append(i) for j in range(i+i, n+1, i): table[j] = 1 return prime_list N = 5*(10**6)+1 E = set(eratosthenes_sieve(N)) T = int(input()) for _ in range(T): A,P = map(int,input().split()) if P not in E: print(-1) else: if A%P != 0: print(1) else: print(0)