#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using pll = pair; using tlll = tuple; constexpr ll INF = 1LL << 60; template bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;} template bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;} ll safemod(ll A, ll M) {ll res = A % M; if (res < 0) res += M; return res;} ll divfloor(ll A, ll B) {if (B < 0) A = -A, B = -B; return (A - safemod(A, B)) / B;} ll divceil(ll A, ll B) {if (B < 0) A = -A, B = -B; return divfloor(A + B - 1, B);} template void unique(vector &V) {V.erase(unique(V.begin(), V.end()), V.end());} template void sortunique(vector &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());} #define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false) template void printvec(const vector &V) {int _n = V.size(); for (int i = 0; i < _n; i++) cout << V[i] << (i == _n - 1 ? "" : " ");cout << '\n';} template void printvect(const vector &V) {for (auto v : V) cout << v << '\n';} template void printvec2(const vector> &V) {for (auto &v : V) printvec(v);} //* #include using namespace atcoder; using mint = modint998244353; //using mint = modint1000000007; //using mint = modint; //*/ /* x[0] = start, x[i+1] = next_index(x[i]) の漸化式で定まるインデックス列に対して get(x[0]) op … op get(x[K-1]) を求める (K はクエリで渡される) Index: x_i の型 Data: 求める値の型、モノイド op: 結合則を満たす演算 e: 単位元 power(b, t) = b^t = b op … op b (t 回) 計算量: 前計算 O(周期)、クエリ O(1) */ template struct Period { private: vector dat, prod_head, prod_tail; int lambda, mu; Data head, tail; public: Period(Index start) { Index a = start, b = start; do { dat.emplace_back(get(a)); a = next_index(a); b = next_index(next_index(b)); } while (a != b); mu = dat.size(); Index c = start; while (a != c) { dat.emplace_back(get(a)); a = next_index(a); c = next_index(c); } lambda = (int)dat.size() - mu; prod_head.resize((int)dat.size() + 1); prod_tail.resize((int)dat.size() + 1); prod_head[0] = e(), prod_tail[lambda] = e(); for (int i = 1; i < (int)prod_head.size(); i++) prod_head[i] = op(prod_head[i - 1], dat[i - 1]); for (int i = lambda + 1; i < (int)prod_tail.size(); i++) prod_tail[i] = op(prod_tail[i - 1], dat[i - 1]); head = prod_head[lambda]; tail = prod_tail[lambda + mu]; } Data query(ll k) { if (k <= (int)dat.size()) return prod_head[k]; Data mid = prod_tail[lambda + (k - lambda) % mu]; return op(op(head, power(tail, (k - lambda) / mu)), mid); } }; ll N; vector A; ll next_index(ll i) { return (i + A.at(i)) % N; } ll get(ll i) { return A.at(i); } ll op(ll a, ll b) { return a + b; } ll e() { return 0; } ll power(ll a, ll t) { return t * a; } int main() { cin >> N; A.resize(N); for (ll i = 0; i < N; i++) { cin >> A.at(i); } Period pe(0); ll Q; cin >> Q; while (Q--) { ll K; cin >> K; cout << pe.query(K) << endl; } }