package main

import (
	"bufio"
	"fmt"
	"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)
		if val, ok := numMap[numUint64]; ok {
			numMap[numUint64] = val + 1
		} else {
			numMap[numUint64] = 1
		}
	}

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

	var result []uint64
	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)
		// fmt.Printf("Debug b: %d\n", b)
		c, _ := strconv.ParseUint(bcSlice[1], 10, 64)
		// fmt.Printf("Debug c: %d\n", c)
		if val, ok := numMap[b]; ok {
			if _, ok := numMap[c]; ok {
				numMap[c] += val
			} else {
				numMap[c] = val
			}
			delete(numMap, b)
		}

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

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

}
