      PROGRAM replacing
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 16
      integer(p) :: n,q,S
      integer(p),allocatable :: a(:),b(:),c(:)
      
      integer(p) :: i, table(10**5)=0
      
      
      read*,n
      allocate( a(n) )
      read*,a
      read*,q
      allocate( b(q),c(q) )
      do i = 1,q
        read*,b(i),c(i)
      end do
      
      S = sum( a )
      do i = 1,n
        table(a(i)) = table(a(i)) + 1
      end do
      
      do i = 1,q
        S = S - table(b(i))*b(i) + c(i)*table(b(i))
        table(c(i)) = table(c(i)) + table(b(i))
        table(b(i)) = 0
        print*,S
      end do
      
      
      
      
      stop
      !debugg
      
      print'(*(x,i0))',table(1:maxval(a))
      
      
      END PROGRAM