program d171

implicit none
integer :: n, q, i, j, s, b, c
integer,allocatable :: a(:)

s = 0

read *, n

allocate(a(1:n))

read *, a(:)
read *, q

do i = 1, q
 read *, b, c
  do j = 1, n
   if (a(j) == b) then
    a(j) = c
   end if
   s = s + a(j)
  end do
 print *, s
 s = 0
end do

end program d171