import math import random import math def eratosthenes(n): a = [True] * n a[0] = False a[1] = False for i in range(2, math.ceil(math.sqrt(n - 1))): if not a[i]: continue for j in range(2, (n - 1) // i + 1): a[i * j] = False return a n = 5000001 Q = eratosthenes(n) from sys import stdin #print(Q[0]) t = int(input()) for i in range(t): a,p = map(int,stdin.readline().rstrip().split()) if Q[p]: if a % p == 0: print(0) else: print(1) else: print(-1)