n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
from collections import Counter
f = Counter(a)
t = sum(a)

for i in range(q):
    b, c = map(int, input().split())
    t += (c - b) * f[b]
    f[c] += f[b]
    f[b] = 0
    print(t)
