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