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