n=int(input())
A=list(map(int,input().split()))
q=int(input())
from collections import Counter
C=Counter(A)

ans=sum(A)
for i in range(q):
  a,b=map(int,input().split())
  ans+=(b-a) * C[a]
  C[b]+=C[a] ; C[a]=0
  print(ans)