from collections import defaultdict
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[a] = 0
    print(sum([v*k for (k, v) in numbers.items()]))