#include #define rep(i,n) for (int i = 0; i < (n); i ++) using namespace std; using ll = long long; using PL = pair; using P = pair; constexpr int INF = 1000000000; constexpr long long HINF = 1000000000000000; constexpr long long MOD = 1000000007; constexpr double EPS = 1e-4; constexpr double PI = 3.14159265358979; struct Debug { template static void print_value(T value,const char* name) { cout << name << ": " << value << '\n'; } template static void print_vector(vector &vec,const char* name) { for (int i = 0;i < vec.size();++i) { cout << " " << name; printf("[%d]: ",i); cout << vec[i]; } cout << '\n'; } template static void print_2dvector(vector> &vec,const char* name) { for (int i = 0;i < vec.size();++i) { for (int j = 0;j < vec[i].size();++j) { cout << " " << name; printf("[%d][%d]: ",i,j); cout << vec[i][j]; } cout << '\n'; } } template static void print_set(set &st,const char* name) { int i = 0; for (auto itr = st.begin();itr != st.end(); ++itr,++i) { cout << " " << name; printf("[%d]: ",i); cout << *itr; } cout << '\n'; } template static void print_map(map &mp,const char* name) { for (auto p: mp) { cout << " " << name << '[' << p.first << ']' << ": " << p.second; } cout << '\n'; } }; int main() { int N; cin >> N; vector A(N); rep(i,N) cin >> A[i]; ll X = 0; vector Xs; Xs.push_back(0); vector remainder(N + 1,false); remainder[0] = true; X = A[X]; while (!remainder[X%N]) { Xs.push_back(X); remainder[X%N] = true; X += A[X%N]; } Xs.push_back(X); //Debug::print_vector(Xs,"Xs"); ll before_loop,size_of_loop; for (int i = 0;i < Xs.size();++i) { if (Xs[i]%N == X%N) { before_loop = i; size_of_loop = Xs.size() - i - 1; break; } } for (ll i = before_loop + 1;i < Xs.size();++i) { Xs[i] -= Xs[before_loop]; } //Debug::print_vector(Xs,"Xs"); int Q; cin >> Q; int Xsize = Xs.size() - 1; rep(i,Q) { ll K; cin >> K; ll ans; if (K < before_loop) { ans = Xs[K]; } else { ans = Xs[before_loop]; K -= before_loop; ans += (ll)(K/size_of_loop) * Xs[Xsize] + Xs[before_loop + K%size_of_loop]; } cout << ans << endl; } return 0; }