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

    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.toLong() }
        result += (c - b) * (group[b] ?: 0L)
        resultList.add(result)

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

    resultList.forEach { println(it) }
}