fun main() {
    nextInt()
    val valueMap = mutableMapOf<Int, Int>()
    var sum = 0L
    nextIntList().forEach {
        sum += it
        valueMap[it] = valueMap.getOrDefault(it, 0) + 1
    }
    val q = nextInt()
    repeat(q) {
        val (b, c) = nextIntList()
        val bNum = valueMap.getOrDefault(b, 0)
        val cNum = valueMap.getOrDefault(c, 0)
        sum += c.minus(b).times(bNum)
        valueMap.remove(b)
        valueMap[c] = cNum + bNum
        println(sum)
    }
}

// # Utils
// ## Input
fun next() = readLine()!!
fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun nextList() = next().split(" ")
fun nextIntList() = next().split(" ").map(String::toInt)
fun nextLongList() = next().split(" ").map(String::toLong)
fun nextDoubleList() = next().split(" ").map(String::toDouble)

// ## Array
inline fun <reified T> sizedArray(size: Int, default: T) = Array(size) { default }
