use std::io::{self, Read as _, Write as _}; struct Scanner<'a>(std::str::SplitWhitespace<'a>); impl<'a> Scanner<'a> { fn new(s: &'a str) -> Self { Self(s.split_whitespace()) } fn next(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, { let s = self.0.next().expect("found EOF"); match s.parse() { Ok(v) => v, Err(msg) => { println!( "parse error. T = {}, s = \"{}\": {:?}", std::any::type_name::(), s, msg ); panic!() } } } } fn main() { let mut stdin = String::new(); std::io::stdin().read_to_string(&mut stdin).unwrap(); let mut sc = Scanner::new(&stdin); let stdout = io::stdout(); let mut stdout = io::BufWriter::new(stdout.lock()); let n: usize = sc.next(); let a = (0..n) .map(|_| sc.next()) .collect::>() .into_boxed_slice(); let q: usize = sc.next(); let b = (0..q) .map(|_| sc.next()) .collect::>() .into_boxed_slice(); let mut rem: u64 = !(!0 << 60); let (a, has_front) = { let mut c = vec![]; let mut has_front = false; for a in a.into_vec().into_iter().rev() { if rem & a != 0 { c.push(a & rem); rem &= !a; has_front = false; } else { has_front = true; } } (c.into_boxed_slice(), has_front) }; dbg!(&a); for mut b in b.into_vec() { let mut count: usize = 0; let mut valid: bool = true; for &c in a.iter() { if b & c == 0 { count += 1; b = !b; } else if !b & c == 0 { } else { valid = false; break; } } if b & rem == 0 { } else if !b & rem == 0 { if has_front { count += 1; } else { // valid = false; } } else { valid = false; } if !valid { writeln!(stdout, "-1").unwrap(); } else { writeln!(stdout, "{}", count).unwrap(); } } stdout.flush().unwrap(); }