from collections import Counter
def get_sum(count):
    return sum([key * count[key] for key in count.keys()])
n = int(input())
a = list(map(int, input().split()))
q = int(input())
count = Counter(a)
for i in range(q):
    b, c = list(map(int, input().split()))
    count[c] += count[b]
    count[b] = 0
    print(get_sum(count))