// WRONG #8 (PREFIX_INT): prefix sums and cycle sum are stored in 32-bit int. // Input: T, then T cases of (N / A / Q / K_1..K_Q). // Fails when (tail + one cycle) sum exceeds 2^31-1 (big A, long path). #include using namespace std; typedef long long ll; static void solve() { int n; scanf("%d", &n); vector a(n); for (auto &x : a) scanf("%lld", &x); vector seen(n, -1); vector pre(1, 0); // BUG: should be long long int r = 0, steps = 0; while (seen[r] == -1) { seen[r] = steps; pre.push_back(pre.back() + (int)a[r]); // overflows past 2^31-1 steps++; r = pre[steps] % n; // overflowed value -> wrong/negative index } int mu = seen[r], lam = steps - mu; int S = pre[steps] - pre[mu]; // BUG: cycle sum in int int q; scanf("%d", &q); while (q--) { ll k; scanf("%lld", &k); if (k <= steps) { printf("%lld\n", (ll)pre[k]); continue; } ll t = k - mu, full = t / lam, rem = t % lam; ll ans = (ll)pre[mu] + full * (ll)S + (ll)(pre[mu + rem] - pre[mu]); printf("%lld\n", ans); } } int main() { int T; scanf("%d", &T); while (T--) solve(); }