import numpy as np
n = int(input())
arr = np.array(list(map(int, input().split())))
q = int(input())

cnt = np.bincount(arr)
cnt = np.concatenate([cnt, np.zeros(10**5 - cnt.shape[0], dtype=int)])

for i in range(q):
    b, c = map(int, input().split())
    cnt[c] += cnt[b]
    cnt[b] = 0
    print((cnt * np.arange(cnt.shape[0])).sum())