package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)
	defer out.Flush()

	var sum int
	nums := make(map[int]int, 100001)
	n := nextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt()
		nums[a[i]]++
		sum += a[i]
	}
	q := nextInt()
	for i := 0; i < q; i++ {
		a = quickSort(a)
		b, c := nextInt(), nextInt()
		nums[c] += nums[b]
		sum += (c - b) * nums[b]
		nums[b] = 0
		// for j := 0; j < n; j++ {
		// 	if a[j] == b {
		// 		a[j] = c
		// 	} else if a[j] > b {
		// 		break
		// 	}
		// }
		fmt.Fprintf(out, "%d\n", sum)
	}
}

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func quickSort(val []int) []int {
	if len(val) < 2 {
		return val
	}

	pivot := val[0]

	left := []int{}
	right := []int{}

	for _, v := range val[1:] {
		if pivot > v {
			left = append(left, v)
		} else {
			right = append(right, v)
		}
	}

	left = quickSort(left)
	right = quickSort(right)

	ret := append(left, pivot)
	ret = append(ret, right...)

	return ret
}

func sum(a []int) int {
	var sum int
	for i := 0; i < len(a); i++ {
		sum += a[i]
	}
	return sum
}
