#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, n) for (int i = 0; i < (int)(n); ++i) //#define cerr if(false) cerr #ifdef DEBUG #define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__); #else #define show(...) 42 #endif using namespace std; using ll = long long; using pii = pair; template ostream& operator<<(ostream& os, pair a) { os << '(' << a.first << ',' << a.second << ')'; return os; } template ostream& operator<<(ostream& os, vector v) { for (auto x : v) os << x << ' '; return os; } void debug() { cerr << '\n'; } template void debug(H a, T... b) { cerr << a; if (sizeof...(b)) cerr << ", "; debug(b...); } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; priority_queuepq; ll sum = 0; rep(i,n){ int x; cin >> x; pq.push(x); sum += x; } int q; cin >> q; while(q--){ int mod; cin >> mod; while(pq.size() and pq.top() >= mod){ int x = pq.top(); pq.pop(); sum -= x; x %= mod; sum += x; pq.push(x); } cout << sum << "\n"; } }