from collections import Counter
N = int(input())
A = map(int, input().split())
Q = int(input())
ops = []
for _ in range(Q):
  b, c = map(int, input().split())
  ops.append((b,c))

tot = []
cnt = Counter(A)
for b, c in ops:
  #print(cnt)
  #print(b, c)
  cnt[c] += cnt[b]
  cnt[b] = 0
  s = sum(k*v for k, v in cnt.items())
  print(s)