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