n = int(input())
a = list(map(int, input().split()))
s = sum(a)
d = [0] * (10 ** 5 + 1)
for x in a:
    d[x] += 1
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s += (c - b) * d[b]
    d[c] += d[b]
    d[b] = 0
    print(s)
