fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toLong() }.toMutableList()
    val q = readLine()!!.toInt()
    var sum = a.reduce { acc, l -> acc + l }
    repeat(q) {
        val (b,c) = readLine()!!.split(" ").map { it.toLong() }
        sum += (c-b) * a.count{ it == b }
        a.forEachIndexed { index, l -> if (l == b) a[index] = c }
        println(sum)
    }
}
