package main

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

var sc = bufio.NewScanner(os.Stdin)
var spaceSc = bufio.NewScanner(os.Stdin)

func main() {
	var input string

	if sc.Scan() {
		input = sc.Text()
	}
	n, _ := strconv.ParseUint(input, 10, 64)

	spaceSc.Split(bufio.ScanWords)
	numMap := make(map[uint64]uint64)
	for i := uint64(0); i < n; i++ {
		if spaceSc.Scan() {
			input = spaceSc.Text()
		}
		num, _ := strconv.ParseUint(input, 10, 64)
		numMap[num]++
	}
	// 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)
	// 	if val, ok := numMap[numUint64]; ok {
	// 		numMap[numUint64] = uint64(val + 1)
	// 	} else {
	// 		numMap[numUint64] = uint64(1)
	// 	}
	// }

	if sc.Scan() {
		input = sc.Text()
	}
	q, _ := strconv.ParseUint(input, 10, 64)

	var result []uint64
	for i := uint64(0); i < q; i++ {
		if spaceSc.Scan() {
			input = spaceSc.Text()
		}
		b, _ := strconv.ParseUint(input, 10, 64)
		// fmt.Printf("Debug b: %d\n", b)
		if spaceSc.Scan() {
			input = spaceSc.Text()
		}
		c, _ := strconv.ParseUint(input, 10, 64)
		// fmt.Printf("Debug c: %d\n", c)
		if val, ok := numMap[b]; ok {
			numMap[c] += val
			delete(numMap, b)
		}

		sum := uint64(0)
		for key, val := range numMap {
			sum += uint64(uint64(key) * uint64(val))
		}
		result = append(result, sum)
	}

	for _, val := range result {
		fmt.Println(val)
	}
}
