import sys import heapq input = sys.stdin.buffer.readline N = int(input()) A = tuple(map(int, input().split())) Q = int(input()) X = map(int, input().split()) h = [-a for a in A] heapq.heapify(h) cur = sum(A) for x in X: while -h[0] >= x: a = -heapq.heappop(h) cur -= a a %= x cur += a heapq.heappush(h, -a) print(cur)