fun main() {
    readLine() // skip
    val list = readLine()!!.split(" ").map { it.toInt() }.toIntArray()
    val q = readLine()!!.toInt()
    val qList = (0 until q).map {
        val (b, c) = readLine()!!.split(" ").map { it.toInt() }
        b to c
    }

    val group = list
        .asSequence()
        .groupBy { it }
        .mapValues { it.value.count() }
        .toMutableMap()

    qList.forEach { (b, c) ->
        group[c] = (group[c] ?: 0) + (group[b] ?: 0)
        group[b] = 0

        println(group.map { (key, value) -> key * value }.sum())
    }
}