fun main() {
    readLine() // skip
    val list = readLine()!!.split(" ").map { it.toInt() }.toIntArray()

    val group = list
        .asSequence()
        .groupBy { it }
        .mapValues { it.value.count().toLong() }
        .toMutableMap()

    val resultList = mutableListOf<Long>()
    var result = list.sum().toLong()

    val q = readLine()!!.toInt()
    (0 until q).map {
        val (b, c) = readLine()!!.split(" ").map { it.toInt() }
        result += (c - b) * (group[b] ?: 0L)
        resultList.add(result)

        group[c] = (group[c] ?: 0L) + (group[b] ?: 0L)
        group[b] = 0
    }

    resultList.forEach { println(it) }
}