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: usize = 0; let mut uses: Vec = vec![0]; let mut used: Vec = vec![0; n]; let mut count: usize = 1; used[0] = count; while used[(x + a[x % n]) % n] == 0 { x += a[x % n]; count += 1; let val = x % n; used[val] = count; uses.push(val); } let geta_count = used[(x + a[x % n]) % n] - 1; let mut geta: Vec = if geta_count > 0 { vec![0; geta_count] } else { vec![] }; let mut loopsum: Vec = vec![0; count - geta_count]; for i in 0..geta_count { if i > 0 { geta[i] = geta[i-1] + a[uses[i]]; } else { geta[i] = a[uses[0]]; } } for i in geta_count..count { if i > geta_count { loopsum[i-geta_count] = loopsum[i-geta_count-1] + a[uses[i]]; } else { loopsum[i-geta_count] = a[uses[i]]; } } for _ in 0..q { let mut k = String::new(); std::io::stdin().read_line(&mut k).ok(); let k: usize = k.trim().parse().unwrap(); let mut result = 0usize; if geta_count > 0 { result = if k > geta_count { geta[geta_count-1] } else { geta[k-1]}; } if k > geta_count { result += (k - geta_count) / loopsum.len() * loopsum[loopsum.len()-1]; if (k - geta_count) % loopsum.len() > 0 { result += loopsum[(k - geta_count) % loopsum.len() - 1]; } } println!("{}", result); } }