fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val arr = IntArray(100001)
    var sum = 0L
    val a = readLine()!!.split(' ').map {
        val num = it.toInt()
        arr[num]++
        sum += num
        num
    }
    val q = readLine()!!.toInt()

    repeat(q) {
        val (b, c) = readLine()!!.split(' ').map { it.toInt() }
        val cnt = arr[b]
        sum -= cnt*b
        sum += cnt*c
        println(sum)
        arr[b] -= cnt
        arr[c] += cnt
    }
}
