program sample
    implicit none
  
    integer(8) :: i,j,m,n,k,l,q,t(100000)
    
    integer(8),allocatable :: a(:),b(:),c(:)
  
    read(*,*) n
    allocate(a(n))
    read(*,*)a
    read(*,*)q
    allocate(b(q))
    allocate(c(q))
    do i=1,q
        read(*,*)b(i),c(i)
    end do
    do i=1,10000
        t(i)=0
    end do
    do i=1,n
        m=a(i)
        t(m)=t(m)+1
    end do
    m=sum(a)


    do i=1,q
        k=b(i)
        l=c(i)
        m=m+(l-k)*t(k)
        t(l)=t(l)+t(k)
        t(k)=0
        
        write(*,*)m
    end do
    
    
    stop
end program sample
  

