fun main(args: Array<String>) {
    val n = readInt()
    val a = readLongList()
    val q = readInt()
    val arr = mutableListOf<Pair<Int, Int>>()
    repeat(q) {
        val (b, c) = readIntList()
        arr.add(Pair(b, c))
    }

    val num = Array(100001) { 0L }
    a.forEach {
        num[it.toInt()]++
    }

    var sum = a.sum()
    for (i in 0 until q) {
        val tmpNum = num[arr[i].first]
        num[arr[i].first] = 0
        num[arr[i].second] += tmpNum
        sum += (arr[i].second - arr[i].first) * tmpNum
        println(sum)
    }
}

// Tools
fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readStringList() = readLine()!!.split(" ")
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)