import numpy as np

N = int(input())

arr = np.array(list(map(int, input().split())))
unique, cnts = np.unique(arr, return_counts=True)

counts = {}
sum = arr.sum()

for key, val in zip(unique, cnts):
    counts[key] = val

Q = int(input())

for i in range(Q):
    B, C = list(map(int, input().split()))
    counts[C] += counts[B]
    sum += counts[B] * (C - B)
    counts[B] = 0
    print(sum)
