from collections import Counter
n = int(input())
nums = list(map(int, input().split()))
q = int(input())
vals = [tuple(map(int, input().split())) for _ in range(q)]

c = Counter(nums)

for b, k in vals:
    o = c[b]
    del c[b]
    c[k] += o
    total = sum([x*y for x, y in c.items()])
    print(total)