from collections import defaultdict def Sieve_of_Eratosthenes(maxA): lst = [-1]*(maxA+1) lst[0] = 0 lst[1] = 1 for i in range(2, maxA+1): if lst[i] == -1: for g in range(i, maxA+1, i): if lst[g] == -1: lst[g] = i return lst maxA = 5*10**6 + 1 lst = Sieve_of_Eratosthenes(maxA) t = int(input()) for _ in range(t): a, p = map(int, input().split()) if p == lst[p]: if a % p != 0: print(1) else: print(0) else: print(-1)