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

q = int(input())

cnt = [0] * 100001

sum = 0
for i in a:
	cnt[i] += 1
	sum += i

while q:
	b, c = map(int, input().split())
	sum += (c - b) * cnt[b]
	cnt[c] += cnt[b]
	cnt[b] = 0
	q -= 1
	print(sum)
