import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
MAX = 10**5
X = np.zeros(MAX)
Y = np.array(range(MAX))

for i in range(len(A)): X[A[i]] += 1

Q = int(input())
for i in range(Q):
    [b, c] = list(map(int, input().split()))
    X[c] += X[b]
    X[b] = 0
    print(int(np.sum(X*Y)))

