fn get_primes() -> Vec { const LIM: usize = 2 * 1e5 as usize; let mut flgs = vec![true; LIM+1]; flgs[0] = false; flgs[1] = false; let limit = (LIM as f64).sqrt().ceil() as usize; for i in 0..=limit { if !flgs[i] { continue; } for j in i..=limit/i { flgs[i*j] = false; } } flgs } fn main() { let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let t: usize = t.trim().parse().unwrap(); let primes = get_primes(); for _ in 0..t { let mut ap = String::new(); std::io::stdin().read_line(&mut ap).ok(); let ap: Vec = ap.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = ap[0]; let p = ap[1]; if !primes[p] { println!("-1"); } else { println!("{}", if a % p == 0 { 0 } else { 1 }); } } }