n=int(input())
a=[]
cnt = [0]*100005
for i in input().split():
	cnt[int(i)]+=1
	a+=[int(i)]
ans = sum(a)
Q=int(input())
for i in range(Q):
	b,c=map(int,input().split())
	ans += cnt[b]*c - cnt[b]*b
	cnt[c]+=cnt[b]
	cnt[b]=0
	print(ans)
