package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	var input string

	if sc.Scan() {
		input = sc.Text()
	}

	if sc.Scan() {
		input = sc.Text()
	}
	slice := strings.Split(input, " ")
	numMap := make(map[uint64]uint64)
	for _, numStr := range slice {
		numUint64, _ := strconv.ParseUint(numStr, 10, 64)
		numMap[numUint64]++
	}

	if sc.Scan() {
		input = sc.Text()
	}
	q, _ := strconv.ParseUint(input, 10, 64)
	for i := 0; uint64(i) < q; i++ {
		if sc.Scan() {
			input = sc.Text()
		}
		bcSlice := strings.Split(input, " ")
		b, _ := strconv.ParseUint(bcSlice[0], 10, 64)
		c, _ := strconv.ParseUint(bcSlice[1], 10, 64)
		if val, ok := numMap[b]; ok {
			numMap[c] += val
		} else {
			numMap[c] = val
		}
		delete(numMap, b)

		var sum uint64
		for key, val := range numMap {
			sum += key * val
		}
		println(sum)
	}

}
