import sys input=sys.stdin.readline import random def is_prime(q,k=50): q = abs(q) #計算するまでもなく判定できるものははじく if q == 2: return True if q < 2 or q&1 == 0: return False #n-1=2^s*dとし(但しaは整数、dは奇数)、dを求める d = (q-1)>>1 while d&1 == 0: d >>= 1 #判定をk回繰り返す for i in range(k): a = random.randint(1,q-1) t = d y = pow(a,t,q) #[0,s-1]の範囲すべてをチェック while t != q-1 and y != 1 and y != q-1: y = pow(y,2,q) t <<= 1 if y != q-1 and t&1 == 0: return False return True for _ in range(int(input())): a,p=map(int,input().split()) if is_prime(p):print(1) else:print(-1)