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


    do i=1,q
        m=0
        read(*,*)b,c
        t(c)=t(c)+t(b)
        t(b)=0
        do j=1,100000
            m=m+j*t(j)
            
        end do
        write(*,*)m
    end do
    
    
    stop
end program sample
  

