import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
q = int(input())
hako = np.array([0]*(10**6))
ind = np.array(list(range(10**6)))
s = []

for i in a:
    hako[i] += 1

for i in range(q):
    b, c = [int(i) for i in input().split()]
#     S += (ind[c] - ind[b])*hako[b]
    hako[c] += hako[b]
    hako[b] = 0
    S = np.sum(hako*ind)
    s.append(S)
for i in range(q):
    print(s[i])