#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define pb push_back #define eb emplace_back #define clock chrono::steady_clock::now().time_since_epoch().count() using namespace std; template ostream& print_tuple(ostream& os, const tuple tu) { os << get(tu); if constexpr (I + 1 != sizeof...(args)) { os << ' '; print_tuple(os, tu); } return os; } template ostream& operator<<(ostream& os, const tuple tu) { return print_tuple(os, tu); } template ostream& operator<<(ostream& os, const pair pr) { return os << pr.first << ' ' << pr.second; } template ostream& operator<<(ostream& os, const array &arr) { for(size_t i = 0; T x : arr) { os << x; if (++i != N) os << ' '; } return os; } template ostream& operator<<(ostream& os, const vector &vec) { for(size_t i = 0; T x : vec) { os << x; if (++i != size(vec)) os << ' '; } return os; } template ostream& operator<<(ostream& os, const set &s) { for(size_t i = 0; T x : s) { os << x; if (++i != size(s)) os << ' '; } return os; } template ostream& operator<<(ostream& os, const multiset &s) { for(size_t i = 0; T x : s) { os << x; if (++i != size(s)) os << ' '; } return os; } template ostream& operator<<(ostream& os, const map &m) { for(size_t i = 0; pair x : m) { os << x.first << " : " << x.second; if (++i != size(m)) os << ", "; } return os; } #ifdef DEBUG #define dbg(...) cerr << '(', _do(#__VA_ARGS__), cerr << ") = ", _do2(__VA_ARGS__) template void _do(T &&x) { cerr << x; } template void _do(T &&x, S&&...y) { cerr << x << ", "; _do(y...); } template void _do2(T &&x) { cerr << x << endl; } template void _do2(T &&x, S&&...y) { cerr << x << ", "; _do2(y...); } #else #define dbg(...) #endif using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair; using pll = pair; //#define double ldb template using vc = vector; template using vvc = vc>; template using vvvc = vc>; using vi = vc; using vll = vc; using vvi = vvc; using vvll = vvc; template using min_heap = priority_queue, greater>; template using max_heap = priority_queue; template concept R_invocable = requires(F&& f, Args&&... args) { { std::invoke(std::forward(f), std::forward(args)...) } -> std::same_as; }; template, typename F> requires R_invocable void pSum(rng &&v, F f) { if (!v.empty()) for(T p = *v.begin(); T &x : v | views::drop(1)) x = p = f(p, x); } template> void pSum(rng &&v) { if (!v.empty()) for(T p = *v.begin(); T &x : v | views::drop(1)) x = p = p + x; } template void Unique(rng &v) { ranges::sort(v); v.resize(unique(v.begin(), v.end()) - v.begin()); } template rng invPerm(rng p) { rng ret = p; for(int i = 0; i < ssize(p); i++) ret[p[i]] = i; return ret; } template vi argSort(rng p) { vi id(size(p)); iota(id.begin(), id.end(), 0); ranges::sort(id, {}, [&](int i) { return pair(p[i], i); }); return id; } template, typename F> requires invocable vi argSort(rng p, F proj) { vi id(size(p)); iota(id.begin(), id.end(), 0); ranges::sort(id, {}, [&](int i) { return pair(proj(p[i]), i); }); return id; } template vvi read_graph(int n, int m, int base) { vvi g(n); for(int i = 0; i < m; i++) { int u, v; cin >> u >> v; u -= base, v -= base; g[u].emplace_back(v); if constexpr (!directed) g[v].emplace_back(u); } return g; } template vvi adjacency_list(int n, vc e, int base) { vvi g(n); for(auto [u, v] : e) { u -= base, v -= base; g[u].emplace_back(v); if constexpr (!directed) g[v].emplace_back(u); } return g; } template void setBit(T &msk, int bit, bool x) { (msk &= ~(T(1) << bit)) |= T(x) << bit; } template void onBit(T &msk, int bit) { setBit(msk, bit, true); } template void offBit(T &msk, int bit) { setBit(msk, bit, false); } template void flipBit(T &msk, int bit) { msk ^= T(1) << bit; } template bool getBit(T msk, int bit) { return msk >> bit & T(1); } template T floorDiv(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a - b + 1) / b; } template T ceilDiv(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? (a + b - 1) / b : a / b; } template bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template requires R_invocable struct doubling { const int LOG; const T id; F op; vvi jp; vvc data; doubling(int _LOG, vi to, vc init, T _id, F f) : LOG(_LOG), id(_id), op(f), jp(LOG, vi(size(to), -1)), data(LOG, vc(size(to), id)) { assert(size(init) == size(to)); jp[0] = std::move(to), data[0] = std::move(init); for(int i = 1; i < LOG; i++) for(int j = 0; j < ssize(jp[i]); j++) if (int k = jp[i - 1][j]; k != -1 and jp[i - 1][k] != -1) jp[i][j] = jp[i - 1][k], data[i][j] = op(data[i - 1][j], data[i - 1][k]); } auto jump(int s, ll step) { assert(0ll <= step and step < (1ll << LOG)); T prod = id; for(; step > 0; step -= step & (-step)) { if (int to = jp[countr_zero((uint64_t)step)][s]; to != -1) { prod = op(prod, data[countr_zero((uint64_t)step)][s]); s = to; } } return pair(s, prod); } template requires R_invocable auto jump_while_true(int s, P pred) { ll step = 0; T prod = id; for(int i = LOG - 1; i >= 0; i--) if (jp[i][s] != -1) if (T tmp = op(prod, data[i][s]); pred(tmp)) step += 1ll << i, prod = tmp, s = jp[i][s]; return tuple(s, step, prod); } }; signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n; cin >> n; vll a(n); for(ll &x : a) cin >> x; vi to(n); for(int i = 0; i < n; i++) to[i] = (i + a[i]) % n; auto add = [](ll a, ll b) { return a + b; }; doubling db(40, to, a, 0ll, add); int q; cin >> q; while(q--) { ll k; cin >> k; cout << db.jump(0, k).second << '\n'; } return 0; }