from collections import Counter


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

count = Counter(a)

total = sum(a)
for _ in range(q):
    b, c = map(int, input().split())
    if count[b] != 0:
        total += c * count[b] - b * count[b]
        count[c] += count[b]
    print(total)
