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