import scala.io.StdIn

object Main extends App {
  var numberCounter = scala.collection.mutable.Map[Long,Long]().withDefaultValue(0)
  StdIn.readLine()
  val numbers = StdIn.readLine().split(' ').map(_.toLong)
  for(num <- numbers){
    numberCounter(num) = numberCounter(num)+1
  }

  val opNum=StdIn.readLine().toInt
  val operations :Seq[(Long,Long)]=for(i <- 1 to opNum)yield{
    val Array(a,b)=StdIn.readLine().split(' ').map(_.toLong)
    (a,b)
  }
  var tmp = numberCounter.map{case (k,v) => k*v}
    .sum
  for((a,b) <- operations){
    tmp = tmp - a*numberCounter(a) + b*(numberCounter(a))
    numberCounter(b) = numberCounter(b)+numberCounter(a)
    numberCounter(a) = 0

    println(tmp)
  }


}
