

fun main() {
    val n = readLine()!!.trim().toInt()
    val series = readLine()!!.trim().split(' ').map(String::toLong)
    val count = LongArray(100001){0L}
    for (i in series) count[i.toInt()]++
    var currentSum = series.sum()
    repeat(readLine()!!.trim().toInt()) {
        val (b, c) = readLine()!!.trim().split(' ').map(String::toInt)
        val move = count[b]
        count[b] = 0
        count[c] += move
        currentSum += (c - b) * move
        println(currentSum)
    }
}