input()
arr = [int(num) for num in input().strip().split(' ')]
q = int(input())

queries = []

for i in range(q):
    query = [ int(num) for num in input().strip().split(' ')]
    queries.append(query)

nums = dict()
summ = 0

for number in arr:
    summ += number
    if number not in nums:
        nums[number] =  1
    else:
        nums[number] += 1

for query in queries:
    curr, new = query
    if curr not in nums: continue
    count = nums[curr]
    diff = (new-curr)*count
    summ += diff
    del nums[curr]
    if new not in nums:
        nums[new] = count
    else:
        nums[new] += count
    print(summ)

