from collections import Counter

n = int(input())
count = Counter(input().split())
ans = 0
for v, c in count.items():
    ans += int(v) * c
for i in range(int(input())):
    b, c = input().split()
    if b in count:
        ans -= count.get(b, 0) * int(b)
        ans += count.get(c, 0) * int(c)
        count[c] = count[b] + count.get(c, 0)
        count[b] = 0
    print(ans)