import java.util.*
import kotlin.collections.HashMap
import kotlin.math.*

private fun readLn() = readLine()!! // string line
private fun readInt() = readLn().toInt() // single int
private fun readLong() = readLn().toLong() // single long
private fun readStrings() = readLn().split(" ") // list of strings
private fun readInts() = readStrings().map(String::toInt) // list of ints
private fun readLongs() = readStrings().map(String::toLong) // list of longs

fun main() {
    val n = readInt()
    val a = readInts().groupingBy { it }.eachCount().toMutableMap()
    val q = readInt()
    (1..q).forEach { _ ->
        val (b, c) = readInts()
        a.remove(b)?.let {
            a[c] = a.getOrDefault(c, 0) + it
        }
        println(a.asSequence().map { (k, v) -> k * v }.sum())
    }
}
