fun op(m: MutableMap<Long, Pair<Long, Long>>, sum: Long, b: Long, c: Long): Long {
  if (m[b] == null) return sum
  val (b_ct, b_sum) = m[b]!!
  if (m[c] == null) m[c] = (0L to 0L)
  val (c_ct, c_sum) = m[c]!!
  m[c] = (b_ct + c_ct to c_sum.toLong() + (b_ct.toLong() * c.toLong()))
  m.remove(b)
  return sum - b_sum + (b_ct * c)
}

fun main()
{
  readLine()!!.toLong()
  val initialAs = readLine()!!.split(' ').map{it.toLong()}
  val m = mutableMapOf<Long, Pair<Long, Long>>()
  for (a in initialAs) {
    if (m[a] == null) {
      m[a] = 0L to 0L
    }
    val (ct, sum) = m[a]!!
    m[a] = (ct+1 to sum+a)
  }
  var sum: Long = m.values.map{(_, sum) -> sum}.sum()

  val q = readLine()!!.toLong()
  for (x in 1..q) {
    val (b, c) = readLine()!!.split(' ').map{it.toLong()}
    sum = op(m, sum, b, c)
    //println("m: $m")
    println(sum)
  }
}
