from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    raw_numbers = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_numbers:
        numbers[num] += 1

    total = sum(raw_numbers)

    Q = int(input())

    for _ in range(Q):
        b, c = list(map(int, input().split()))
        taken = numbers[b]
        total -= b * numbers[b]
        numbers[c] += numbers[b]
        total += c * taken
        print(total)
        
main()