N = int(input())
A = list(map(int, input().split()))
Q = int(input())

ans = [0]*(100000+1)
for a in A:
    ans[a] += a

for i in range(Q):
    b, c = map(int, input().split())
    ans[c] += (ans[b]//b) * c
    ans[b] = 0
    print(int(sum(ans)))
