#include #ifdef DEBUG #include #else #define dump(...) #endif int main(){ int N; while(std::cin >> N){ std::vector A(N); for(int i = 0; i < N; ++i) std::cin >> A[i]; int64_t len = 0, loop = 0; std::vector p(N, -1), v(N, -1); int X = 0; for(int i = 0; i < 2 * N; ++i){ if(p[X] != -1){ len = i; loop = i - p[X]; break; } p[X] = i; v[i] = X; (X += A[X]) %= N; } int tail = len - loop; //std::cerr << len << " " << loop << " " << tail << "\n"; std::vector tail_sum(tail + 1); for(int i = 0; i < tail; ++i) tail_sum[i+1] = tail_sum[i] + A[v[i]]; dump(tail_sum); std::vector loop_sum(loop + 1); for(int i = 0; i < loop; ++i){ loop_sum[i+1] = loop_sum[i] + A[v[tail + i]]; } dump(loop_sum); int Q; std::cin >> Q; while(Q--){ int64_t K; std::cin >> K; int64_t ans = 0; if(K <= tail){ ans = tail_sum[K]; }else{ ans += tail_sum.back(); K -= tail; ans += loop_sum.back() * (K / loop); ans += loop_sum[K % loop]; } std::cout << ans << "\n"; } } return 0; }