fun main() = Thread(null, ::abc171d, "(^o^)", 128 * 1024 * 1024).apply { start() }.join()

@OptIn(ExperimentalStdlibApi::class)
fun abc171d() {
    readLine()
    val aList = readLine()!!.split(' ').map { it.toLong() }

    val counter = MultiSet.of(aList)
    var sum = aList.sum()
    val answer = StringBuilder()

    repeat(readLine()!!.toInt()) {
        val (b, c) = readLine()!!.split(' ').map { it.toLong() }
        val countOfB = counter.count(b)

        sum -= (b - c) * countOfB
        answer.appendln(sum)

        counter.add(c, countOfB)
        counter.removeAll(b)
    }

    print(answer)
}

@Suppress("unused", "MemberVisibilityCanBePrivate")
abstract class Countable<K>(protected open val map: MutableMap<K, Int>) {
    fun add(k: K, v: Int = 1) = map.inc(k, v)
    fun remove(k: K, v: Int = 1) = map.dec(k, v)
    fun removeAll(k: K) = map.remove(k)
    fun contains(k: K) = map.contains(k)
    fun count(k: K) = map[k] ?: 0
    fun isEmpty() = map.isEmpty()
    fun isNotEmpty() = !isEmpty()

    protected fun <K> MutableMap<K, Int>.inc(k: K, v: Int) = merge(k, v, Int::plus)
    protected fun <K> MutableMap<K, Int>.dec(k: K, v: Int) =
        if ((this[k] ?: 0) <= v) remove(k) else merge(k, v, Int::minus)

    abstract override fun toString(): String
    abstract override fun hashCode(): Int
    abstract override fun equals(other: Any?): Boolean
}

@Suppress("unused", "MemberVisibilityCanBePrivate")
class MultiSet<K>(override val map: MutableMap<K, Int> = mutableMapOf()) : Countable<K>(map) {
    companion object {
        fun <K> of() = MultiSet<K>()
        fun <K> of(iterable: Iterable<K>) = fromGroup(iterable.groupBy { it })
        fun <K> of(vararg args: K) = of(args.toList())
        fun <K> fromGroup(map: Map<K, List<K>>) = fromCount(map.mapValues { it.value.size })
        fun <K> fromCount(map: Map<K, Int>) = MultiSet(map.toMutableMap())
    }

    override fun toString() = "MultiSet(map=$map)"
    override fun hashCode() = map.hashCode()
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as MultiSet<*>
        return map == other.map
    }
}

@Suppress("unused", "MemberVisibilityCanBePrivate")
class SortedMultiSet<K : Comparable<K>>(override val map: java.util.SortedMap<K, Int> = sortedMapOf()) :
    Countable<K>(map) {
    fun min() = if (map.isNotEmpty()) map.firstKey() else null
    fun max() = if (map.isNotEmpty()) map.lastKey() else null

    companion object {
        fun <K : Comparable<K>> of() = SortedMultiSet<K>()
        fun <K : Comparable<K>> of(vararg args: K) = of(args.toList())
        fun <K : Comparable<K>> of(iterable: Iterable<K>) = fromGroup(iterable.groupBy { it })
        fun <K : Comparable<K>> fromGroup(map: Map<K, List<K>>) = fromCount(map.mapValues { it.value.size })
        fun <K : Comparable<K>> fromCount(map: Map<K, Int>) = SortedMultiSet(map.toSortedMap())
    }

    override fun hashCode() = map.hashCode()
    override fun toString() = "SortedMultiSet(map=$map)"
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false
        other as SortedMultiSet<*>
        return map == other.map
    }
}


Measured memory_bytes: 79308