package main

import (
	"fmt"
)

func main() {
	var n, q int
	var a []int

	// 特定の値が何個あるか
	nums := make(map[int]int, 100005)
	var sum int

	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var temp int
		fmt.Scan(&temp)
		a = append(a, temp)
		nums[temp]++
		sum += temp
	}
	fmt.Scan(&q)
	for j := 0; j < q; j++ {
		//log.Println("----------")
		//log.Println("変換前", nums)
		var b, c int
		fmt.Scan(&b, &c)

		//
		nums[c] += nums[b]
		sum += (c - b) * nums[b]
		nums[b] = 0
		//log.Println(b, "=>", c)
		//log.Println("変換後", nums)
		//log.Println("sum =", sum)
	}
	fmt.Println(sum)
}
