fun list() = readLine()!!.split(" ").map {it.toInt()}
fun int() = readLine()!!.toInt()

fun main() {
	val n = int()
	val m = mutableMapOf<Int,Long>()
	var r = 0L

	for(i in list()) {
		m.merge(i,1) {i,j-> i+j}
		r += i
	}

	for(q in 1..int()) {
		val (b,c) = list()
		val a = m.remove(b) ?: 0
		r += (c-b)*a
		m.merge(c,a) {i,j-> i+j}
		println(r)
	}
}
