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

fun sum(m: MutableMap<Int, Pair<Int, Int>>): Long {
  return m.values.map{(_, sum) -> sum.toLong()}.sum()
}

fun main()
{
  readLine()!!.toInt()
  val initialAs = readLine()!!.split(' ').map{it.toInt()}
  val m = mutableMapOf<Int, Pair<Int, Int>>()
  for (a in initialAs) {
    if (m[a] == null) {
      m[a] = 0 to 0
    }
    val (ct, sum) = m[a]!!
    m[a] = (ct+1 to sum+a)
  }


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