n=int(input())
L=list(map(int, input().split()))
ans=sum(L)
import collections
D=collections.Counter(L)
q=int(input())
for i in range(q):
	b, c =map(int, input().split())
	ans=ans+(c-b)*D[b]
	D[c]+=D[b]
	D[b]=0
	print(ans)