fun main(args: Array<String>) {
    val n = readInt()
    var a = readListLong().sorted().toMutableList()
    val q = readInt()
    var bc = readMatrixLong(q)
    var list = LongArray(100007).toMutableList()
    a.forEach {
        list[it.toInt()]++
    }

    var ans:Long = a.sum().toLong()


    for (i in 0 until q) {

        val b = bc[i][0]
        val c = bc[i][1]
        var sa = c - b
        var count: Long = list[b]

        list[b] -= count
        list[c] += count

        ans += sa * count
        println(ans)
    }
}

private fun read() = readLine()!!

private fun readInt() = read().toInt()

private fun readLong() = read().toLong()

private fun readDouble() = read().toDouble()

private fun readListString() = read().split(" ")

private fun readListInt() = readListString().map { it.toInt() }

private fun readListLong() = readListString().map { it.toLong() }

private fun readListDouble() = readListString().map { it.toDouble() }

private fun readMatrixString(n: Int): List<List<String>> {
    val matrix = mutableListOf<List<String>>()
    for (i in 0 until n) {
        val list = readListString()
        matrix.add(list)
    }
    return matrix
}

private fun readMatrixInt(n: Int): List<List<Int>> {
    val matrix = mutableListOf<List<Int>>()
    for (i in 0 until n) {
        val list = readListInt()
        matrix.add(list)
    }
    return matrix
}

private fun readMatrixDouble(n: Int): List<List<Double>> {
    val matrix = mutableListOf<List<Double>>()
    for (i in 0 until n) {
        val list = readListDouble()
        matrix.add(list)
    }
    return matrix
}

private fun readMatrixLong(n: Int): List<List<Long>> {
    val matrix = mutableListOf<List<Long>>()
    for (i in 0 until n) {
        val list = readListLong()
        matrix.add(list)
    }
    return matrix
}

