local mfl, mce = math.floor, math.ceil local function getprimes(x) local primes = {} local allnums = {} for i = 1, x do allnums[i] = true end for i = 2, x do if allnums[i] then table.insert(primes, i) local lim = mfl(x / i) for j = 2, lim do allnums[j * i] = false end end end return primes end local primes = getprimes(2400) local ffi = require("ffi") local C = ffi.C ffi.cdef[[ long long atoll(const char*); ]] local function lltonumber(str) return C.atoll(str) end local q = io.read("*n", "*l") for iq = 1, q do local s = io.read() local a, p = s:match("(%d+) (%d+)") p = tonumber(p) a = lltonumber(a) if p == 1 then print(-1) else local v = true for i = 1, #primes do if p % primes[i] == 0 and p ~= primes[i] then v = false break end end if v then p = 1LL * p if a % p == 0LL then print(0) else print(1) end else print(-1) end end end