import strutils,sequtils
var
  _,A,Q=stdin.readLine.split.map parseInt
  C=newseq[int]A.max
  s,b,c=0
for a in A:
  s+=a
  C[a]+=1
for _ in 1..Q[0]:
  (b,c)=stdin.readLine.split.map parseInt
  s+=(c-b)*C[b]
  C[c]+=C[b]
  C[b]=0
  echo s