import java.util.Scanner

fun main() {
	val scn = Scanner(System.`in`)
	val m = mutableMapOf<Int, Int>()
	val n = scn.nextInt()
	repeat(n){
		val a = scn.nextInt()
		m[a] = m.getOrElse(a){0} + 1
	}
	var s = m.entries.sumByDouble { it.key*it.value.toDouble() }.toLong()
	val q = scn.nextInt()
	repeat(q){
		val b = scn.nextInt()
		val c = scn.nextInt()
		val mb = m.remove(b)
		if (mb!=null){
			m[c] = m.getOrElse(c){0} + mb
			s += mb.toLong() * (c-b)
		}
		println(s)
	}
}
