fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val numbers = Array(1000000) { 0 }
    val input = readLine()!!.split(" ").map { it.toInt() }
    input.forEach { numbers[it]++ }
    var sum = input.sum().toLong()
    val q = readLine()!!.toInt()
    repeat(q) {
        val (b,c) = readLine()!!.split(" ").map { it.toInt() }
        val numberB = numbers[b]
        sum += (c-b) * numberB.toLong()
        println(sum)
        numbers[c] += numberB
        numbers[b] = 0
    }
}
