import sys input = lambda: sys.stdin.readline().rstrip() from heapq import heappush, heappop H = [] hpush = lambda x: heappush(H, -x) hpop = lambda: -heappop(H) N = int(input()) A = [int(a) for a in input().split()] ans = 0 for a in A: ans += a hpush(a) Q = int(input()) X = [int(a) for a in input().split()] for x in X: while -H[0] >= x: a = hpop() ans -= a a %= x hpush(a) ans += a print(ans)