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, Long>()
    var sum = 0L
    aa.forEach { a ->
        aMap[a.key] = a.value.size.toLong()
        sum += a.key * a.value.size
    }

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

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