fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toInt() }
        .groupBy { it }.mapValues { it.value.size }.toMutableMap()

    val q = readLine()!!.toInt()
    var total = a.map { it.key * it.value }.sum().toLong()
    for (i in 0 until q) {
        val input = readLine()!!.split(" ").map { it.toInt() }
        val b = input[0]
        val c  = input[1]
        val add = (c - b) * (a[b] ?: 0)
        a[c] = (a[c] ?: 0) + (a[b] ?: 0)
        a[b] = 0
        total = total + add
        println(total)
    }
}