n = int(input())
a = list(map(int, input().split()))

cnt = [0]*100001
for c in a:
    cnt[c] += 1

sm = sum(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    sm += (cnt[b]*(c-b))
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(sm)