#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif struct dsu { int cc; vector p, sz; dsu(int n = 0) : cc(n), p(n, -1), sz(n, 1) {} int root(int v) const { while (p[v] != -1) v = p[v]; return v; } bool unite(int u, int v) { u = root(u), v = root(v); if (u == v) return false; --cc; if (sz[u] < sz[v]) swap(u, v); p[v] = u; sz[u] += sz[v]; return true; } bool same(int u, int v) const { return root(u) == root(v); } int size(int v) const { return sz[root(v)]; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); for (auto&& e : a) cin >> e; dsu d(n); for (int i = 0; i < n; ++i) { d.unite(i, (i + a[i]) % n); } int k = d.size(0); vector b(k + 1); for (int t = 0; t < k; ++t) { b[t + 1] = b[t] + a[b[t] % n]; } int q; cin >> q; while (q--) { long long m; cin >> m; cout << m / k * b[k] + b[m % k] << '\n'; } }