require 'prime' T = gets.to_i MAX_P = 5 * 10 ** 6 is_prime = Array.new(MAX_P + 1, false) is_prime[1] = true Prime.each(MAX_P) do |n| is_prime[n] = true end ans = [] STDIN.readlines.each do |line| a, pv = line.split.map(&:to_i) break if a.nil? if is_prime[pv] if a.gcd(pv) == 1 ans << 1 else ans << 0 end else ans << -1 end end puts ans