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