from collections import Counter
n=int(input())
a=list(map(int,input().split()))
anss=sum(a)
ac=Counter(a)
q=int(input())
num=[0]*(10**5+1)
for i in range(q):
  b,c=map(int,input().split())
  anss=anss+(c-b)*ac[b]
  ac[c]+=(ac[b])
  print(anss)
  

