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

# ans=0
# for k,v in d.items():
#   ans+=k*v
# print(ans)

