program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, q
    integer(int32),allocatable :: a(:), b(:), c(:)
    integer(int32) :: i
    integer(int64) :: s
    integer(int64),allocatable :: tmp(:)
    integer(int32),allocatable :: ind(:)
    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)

    s = sum(a)
    ind = (/(i, i = 1, n)/)

    do i = 1, q
        tmp = pack(a(:), a == b(i))
        s = s - sum(tmp) + c(i) * size(tmp)
        tmp = pack(ind, a == b(i))
        a(tmp) = c(i)
        print *, s
    enddo


end program main