program replacing
  implicit none
  integer :: n, a(100000) = 0, q, b, c, i
  integer(8) :: s = 0, x(100000) = 0
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    s = s + a(i)
    x(a(i)) = x(a(i)) + 1
  end do
  read(*,*) q
  do i = 1, q
    read(*,*) b, c
    s = s + x(b) * (c - b)
    x(c) = x(c) + x(b)
    x(b) = 0
    write(*,'(i0)') s
  end do
end program replacing