#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; map m; ll ans = 0; for(ll i = 0; i < N; i++) { ll a; cin >> a; m[a]++; ans += a; } ll Q; cin >> Q; while(Q--) { ll x; cin >> x; auto it = m.lower_bound(x); map add; for(auto i = it; i != m.end(); i++) { auto [v, c] = *i; add[v % x] += c; ans -= (v - v % x) * c; } m.erase(it, m.end()); for(auto &[v, c] : add) { m[v] += c; } cout << ans << "\n"; } }