fun main() {
    val N = readLine()!!.toInt()
    val AN = readLine()!!.split(" ").map(String::toLong).groupingBy { it }.eachCount()
        .mapValuesTo(mutableMapOf()) { (k, v) -> v.toLong() }
    val Q = readLine()!!.toInt()
    val BCN: Array<Pair<Long, Long>> = Array(Q) { readLine()!!.split(" ").map(String::toLong).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)
    }

}