program abc171
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,q,ans
    integer(int64):: cnt(100000)=0
    integer(int32), allocatable:: a(:),b(:),c(:)


    read*, n
    allocate(a(n))
    read*, a(:)
    ans = sum(a)
    do i=1,n
        cnt(a(i))=cnt(a(i))+1
    end do
    read*, q
    allocate(b(q),c(q))
    do i=1,q
        read*, b(i),c(i)
    end do
    do i=1,q
        ans = ans+(c(i)-b(i))*cnt(b(i))
        cnt(c(i)) = cnt(c(i)) + cnt(b(i))
        cnt(b(i)) = 0
        print'(i0)', ans
    end do
end program abc171