program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, q
    integer(int64) :: an(10000000)
    integer(int32),allocatable :: a(:), b(:), c(:)
    integer(int32) :: i
    integer(int64) :: s

    read *, n
    allocate(a(n))
    read *, (a(i), i = 1, n)
    read *, q
    allocate(b(q), c(q))
    read *, (b(i), c(i), i = 1, q)

    an = 0
    do i = 1, n
        an(a(i)) = an(a(i)) + 1
    enddo

    s = sum(a)

    do i = 1, q
        s = s + (c(i) - b(i)) * an(b(i))
        an(c(i)) = an(c(i)) + an(b(i))
        an(b(i)) = 0
        print *, s
    enddo

end program main