from collections import Counter
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
cnt = Counter(a)
q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    cnt_b = cnt.pop(b)
    cnt[c] += cnt_b
    s += (c - b)*cnt_b
    print(s)