fun main() {
    val builder = StringBuilder()

    val n = readInputLine().toInt()

    val cnt = LongArray(100000 + 1)

    var sum = 0L

    readInputLine().split(" ").forEach {
        cnt[it.toInt()]++
        sum += it.toLong()
    }

    repeat(readInputLine().toInt()) {
        val (b, c) = readInputLine().split(" ").map { it.toInt() }
        if (cnt[b] != 0L) {
            sum -= cnt[b] * b.toLong()
        }
        sum += cnt[b] * c.toLong()
        cnt[c] += cnt[b]
        cnt[b] = 0L

        builder.appendln(sum)
    }

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}
