n = int(input())
a = [int(i) for i in input().split()]
cnt = [0]*(10**5+1)
for i in range(n): cnt[a[i]] += 1
q = int(input())
s = sum(a)
for _ in range(q):
    b,c = map(int,input().split())
    s += (-b+c)*cnt[b]
    cnt[c] += cnt[b]
    cnt[b] = 0
    print(s)