from collections import defaultdict

n = int(input())
A = list(map(int, input().split()))
q = int(input())
BC = [ list(map(int, input().split())) for _ in range(q)]

d = defaultdict(int)
for a in A:
    d[a] += 1

for (b,c) in BC:
    d[c] += d[b]
    d[b] = 0
    print(sum([d[k]*k for k in d]))

