#pragma region #pragma GCC target("avx2") #pragma GCC optimize("03") #pragma GCC optimize("unroll-loops") #include using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; #define endl "\n" #define MP make_pair #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORR(x,arr) for(auto& x:arr) #define PII pair #define PLL pair #define VPII vector #define VPLL vector #define FI first #define SE second #define ALL(x) (x).begin(), (x).end() constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1; templateinline bool chmax(T &a, const T &b) { if (ainline bool chmin(T &a, const T &b) { if (b visited(100000+5, false); int main(){ cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15); cin >> n; FOR(i,0,n-1) { cin >> A[i]; } int x = 0; vector v; while(true) { v.emplace_back(x); if(visited[x]) break; visited[x] = true; (x += A[x]) %= n; } int twice = v.back(); v.pop_back(); vector loop; while(v.back() != twice) { loop.emplace_back(v.back()); v.pop_back(); } // cout << v.size() << endl; loop.emplace_back(twice); v.pop_back(); reverse(ALL(loop)); int p = v.size(); int q = loop.size(); // for(auto &x: v) cout << x << " "; // for(auto &x:loop) cout << x << " "; // cout << p << " " << q << endl; vector front_sum(p+1, 0LL); vector loop_sum(q+1, 0LL); FOR(i,1,p) front_sum[i] = front_sum[i-1] + A[v[i-1]]; FOR(i,1,q) loop_sum[i] = loop_sum[i-1] + A[loop[i-1]]; int Q; cin >> Q; while(Q--) { ll k; cin >> k; if(k <= p) { cout << front_sum[k] << endl; continue; } ll ans = front_sum[p]; k -= p; cout << ans + loop_sum[q] * (k/q) + loop_sum[k%q] << endl; } return 0; }