fun main() {
    val n = readInt()
    val a = readInts().map { it.toLong() }

    val occurrences = mutableMapOf<Long, Int>()
    a.forEach { num -> occurrences[num] = occurrences[num]?.plus(1) ?: 1 }

    var currSum = a.sum()

    val q = readInt()
    repeat(q) {
        val (b, c) = readInts().map { it.toLong() }
        if (occurrences.containsKey(b)) {
            val occur = occurrences[b]!!
            occurrences[c] = occur + (occurrences[c] ?: 0)
            occurrences[b] = 0

            currSum -= occur * b
            currSum += occur * c
        }

        println(currSum)
    }
}

// Input Reader
private fun readLn() = readLine()!!

private fun readStrings() = readLn().trim().split(" ")
private fun readInt() = readLn().toInt()
private fun readInts() = readStrings().map { it.toInt() }
