N = input()
bucket = {}
for i in map(int, input().split()):
    if i in bucket:
        bucket[i] += i
    else:
        bucket[i] = i

Q = int(input())

s = []
for i in range(Q):
    b, c = map(int, input().split())
    num = bucket.pop(b, 0)
    now = bucket.get(c, 0)
    bucket[c]  = now + (num // b * c)
    s.append(sum(bucket.values()))
    
for ans in s:
    print(ans)
    