n = int(input())
a = list(map(int, input().split()))
ind = [0]*(10**6)

for i in range(n):
	ind[a[i]] += 1

q = int(input())
s = 0
for v in a:
	s += v

for _ in range(q):
	b, c = map(int, input().split())
	ab = ind[b]
	if ab == 0:
		print(s)
		continue

	s += (ab*c)-(ab*b)
	ind[c] += ab

	print(s)
