fun main() {
    val N = readLine()!!.toInt()
    val AN = mutableMapOf<Int, Long>()

    readLine()!!.split(" ").map(String::toInt).forEach {
        AN.compute(it) { k, v -> v ?: 0 + 1 }
    }
    val Q = readLine()!!.toInt()
    val BCN: Array<Pair<Int, Int>> = Array(Q) { readLine()!!.split(" ").map(String::toInt).let { it[0] to it[1] } }

    for ((B, C) in BCN) {
        AN.compute(C) { k, v -> (v ?: 0L) + AN.getOrDefault(B, 0L) }
        AN.remove(B)

        var sum = 0L
        AN.toList().forEach { (k, v) -> sum += k * v }
        println(sum)
    }

}