fun main(args: Array<String>) {
     readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toInt() }
    var map = mutableMapOf<Int, Int>()

    a.forEach {
        map[it] = (map[it] ?: 0) + 1
    }
    var sum = a.map { it.toLong() }.sum()

    val q = readLine()!!.toInt()
    repeat(q){
        val (b, c) = readLine()!!.split(" ").map { it.toInt() }
        val count = map[b] ?: 0
        sum += (count * (c - b))

        map[c] = (map[c] ?: 0) + count
        map[b] = 0

        println(sum)
    }
}

