use std::collections::BTreeMap; fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut q = String::new(); std::io::stdin().read_line(&mut q).ok(); let q: usize = q.trim().parse().unwrap(); let mut x = String::new(); std::io::stdin().read_line(&mut x).ok(); let x: Vec = x.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut mapping = BTreeMap::new(); for i in 0..n { *mapping.entry(a[i] % x[0]).or_insert(0usize) += 1; } let mut summary = mapping.iter().map(|(&val, &cnt)| val * cnt).sum::(); println!("{}", summary); for &v in x.iter().skip(1) { let stack = mapping.range(v..).map(|(&key, &val)| (key, val)).collect::>(); summary -= stack.iter().map(|&(val, cnt)| val * cnt).sum::(); summary += stack.iter().map(|&(val, cnt)| (val % v) * cnt).sum::(); println!("{}", summary); for &(val, cnt) in stack.iter() { mapping.remove(&val); *mapping.entry(val%v).or_insert(0usize) += cnt; } } }