use std::io::*; fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let t: usize = itr.next().unwrap().parse().unwrap(); const MAX: usize = 5000005; let mut is_prime: Vec = vec![true; MAX]; is_prime[0] = false; is_prime[1] = false; for i in 2..MAX { if is_prime[i] { let mut j = i * 2; while j < MAX { is_prime[j] = false; j += i; } } } let mut out = Vec::new(); for _ in 0..t { let a: u64 = itr.next().unwrap().parse().unwrap(); let p: u64 = itr.next().unwrap().parse().unwrap(); if is_prime[p as usize] { writeln!(out, "{}", if a % p == 0 { 0 } else { 1 }).ok(); } else { writeln!(out, "-1").ok(); } } stdout().write_all(&out).unwrap(); }