import collections
n=int(input())
a=list(map(int,input().split()))
q=int(input())
numl=collections.Counter(a)
s=sum(a)
for qq in range(q):
  x,y=map(int,input().split())
  s+=numl[x]*(y-x)
  print(s)
  numl[y]+=numl[x]
  numl[x]=0
  