fun intList() = readLine()?.split(" ")?.map(String::toInt) ?: TODO()
fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: return
    val a = intList()
    val q = readLine()?.toInt() ?: return
    var t: Long = a.sum().toLong()
    val map = hashMapOf<Int, Int>()
    a.forEach { i ->
        map[i] = map[i]?.let { it + 1 } ?: 1
    }
    buildString {
        repeat(q) {
            val (b, c) = intList()
            val bb = map[b] ?: 0
            val cc = map[c] ?: 0
            val d = c - b
            t += d * bb
            map[b] = 0
            map[c] = cc + bb
            appendln(t)
        }
    }.let {
        println(it.trimIndent())
    }
}
