n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [map(int, input().split()) for _ in range(5)]
b, c = [list(i) for i in zip(*xy)]

x = 0

while x < q:
    a = [c[x] if i > b[x] else i for i in a]
    print(sum(a))
    x += 1




