fun intList() = readLine()?.split(" ")?.map(String::toInt) ?: TODO()
fun longList() = readLine()?.split(" ")?.map(String::toLong) ?: TODO()
fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: return
    val a = longList()
    val q = readLine()?.toInt() ?: return
    var t: Long = a.sum()
    val map = hashMapOf<Long, Int>()
    a.forEach { i ->
        map[i] = map[i]?.let { it + 1 } ?: 1
    }
    buildString {
        repeat(q) {
            val (b, c) = longList()
            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())
    }
}
