n=int(input())
A=list(map(int,input().split()))
s=sum(A)
q=int(input())
from collections import Counter
a=Counter(A)
r=[0]*q
for i in range(q):
	b,c=map(int,input().split())
	s+=(c-b)*a[b]
	a[c]+=a[b]
	a[b]=0
	r[i]=s
for i in r:print(i)