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

l = [0] * (10 ** 5 + 1)
ans = []
for e in a:
    l[e] += 1

for _ in range(q):
    b, c = map(int, input().split())
    l[c] += l[b]
    l[b] = 0
    sum = 0
    for i, e in enumerate(l):
        sum += i * e
    ans.append(sum)

for sum in ans:
    print(sum)