import sys # import pypyjit import itertools import heapq import math from collections import deque, defaultdict import string import functools import bisect input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) # pypyjit.set_param('max_unroll_recursion=-1') def readints(): return map(int, input().split()) def readlist(): return list(map(int, input().split())) def readstr(): return input()[:-1] class PrimeTable: def __init__(self, N): self.is_prime = [True] * (N + 1) self.is_prime[0] = False self.is_prime[1] = False for i in range(2, N + 1): if i * i > N: break if self.is_prime[i] is False: continue for j in range(2, N + 1): if i * j > N: break self.is_prime[i * j] = False self.primes = [n for n in range(2, N + 1) if self.is_prime[n]] L = 5 * 10 ** 6 is_prime = PrimeTable(L).is_prime T = int(input()) for _ in range(T): a, p = readints() if is_prime[p]: print(0 if a % p == 0 else 1) else: print(-1)