program ABC171D
    implicit none
    integer(8)::N,Numbers(100000),i,Q,ans,B,C
    integer(8),allocatable::A(:)
    read*,N
    allocate(A(N))
    read*,A
    read*,Q
    Numbers=0
    ans=sum(A)

    do i=1,N
        Numbers(A(i))=Numbers(A(i))+1
    end do

    do i=1,Q
        read*,B,C
        ans=ans+Numbers(B)*(C-B)
        print'(i0)',ans
        Numbers(C)=Numbers(C)+Numbers(B)
        Numbers(B)=0
    end do
end program ABC171D