fun main() {
    val N = readLine()!!.toInt()
    val AN = mutableMapOf<Int, Long>()

    readLine()!!.split(" ").map(String::toInt).forEach {
        AN.compute(it) { k, v -> (v ?: 0) + 1 }
    }
    val Q = readLine()!!.toInt()
    val BCN: Array<Pair<Int, Int>> = Array(Q) { readLine()!!.split(" ").map(String::toInt).let { it[0] to it[1] } }


    var sum = 0L
    AN.toList().forEach { (k, v) -> sum += k * v }
    for ((B, C) in BCN) {
        val CC = AN.getOrElse(C, { 0 })
        val BC = AN.getOrElse(B, { 0 })
        AN.remove(B)
        AN.put(C, CC + BC)
        sum = sum - (BC * B) + (BC * C)
        println(sum)
    }

}