fun main() {
  val n = readLine()!!.toInt()
  val a = LongArray(100001){0}
  val c = readLine()!!.split(' ').map { it.toLong() }.toLongArray()
  c.forEach{a[it.toInt()]++}
  var sum = c.sum()

  val q = readLine()!!.toInt()
  for(i in 0 until q){
    val (v,newV) = readLine()!!.split(' ').map { it.toInt() }
    if(v > newV){
      sum -= (v-newV)*a[v]
      a[newV] += a[v]
      a[v] = 0
    }else {
      sum += (newV - v)*a[v]
      a[newV] += a[v]
      a[v] = 0
    }
    println(sum)
  }
}