program abc171d
	implicit none
    integer(16) :: n, q, i, b, c
    integer(16) :: s
    integer(16),allocatable :: a(:)
    integer :: d(100000)
    
    read(*,*) n
    allocate(a(n))
    read(*,*) a
    do i = 1, n
    	d(a(i)) = d(a(i)) + a(i)
    end do
    
    read(*,*) q
    
    s = sum(d)
    
	do i = 1, q
    	read(*,*) b, c
        s = s + c*(d(b)/b) - d(b)
        d(c) = d(c) + c*(d(b)/b)
        d(b) = 0
        write(*,*) s
    end do
    stop
end program abc171d