fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val aa = readLine()!!.split(" ").map { it.toInt() }.groupBy { it }
    val q = readLine()!!.toInt()

    val aMap = mutableMapOf<Int, Int>()
    var sum = 0L
    aa.forEach { a ->
        aMap[a.key] = a.value.size
        sum += a.key * a.value.size
    }

    repeat(q) {
        val (b, c) = readLine()!!.split(" ").map { it.toInt() }
        if ((aMap[b] ?: 0) > 0) {
            sum -= b * aMap[b]!!
            sum += c * aMap[b]!!

            aMap[c] = (aMap[c] ?: 0) + aMap[b]!!
            aMap[b] = 0
        }
        println(sum)
    }
}