from collections import Counter

n = int(input())
a = list(map(int, input().split()))
q = int(input())

cnt = Counter(a)

s = sum(cnt)

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