from collections import defaultdict
import sys

input = sys.stdin.readline

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

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

    Q = int(input())

    for _ in range(Q):
        a, b = list(map(int, input().split()))
        numbers[b] += numbers[a]
        numbers.pop(a)
        print(sum([v*k for (k, v) in numbers.items()]))
    
main()