n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[map(int,input().split()) for _ in range(q)]
b,c=[list(i) for i in zip(*bc)]
a.sort
count=[0 for _ in range(100000)]
sum=0
for i in range(n):
  count[a[i]]=count[a[i]]+1
  sum=sum+a[i]
for i in range(q):
  sum=sum+(c[i]-b[i])*count[b[i]]
  print(sum)
  count[c[i]]=count[c[i]]+count[b[i]]
  count[b[i]]=0
