n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
hako = [0]*(10^6+1)
ind = list(range(10^6+1))
S = sum(a)
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]
    s.append(S)
    hako[c] += hako[b]
    hako[b] = 0
for i in range(q):
    print(s[i])