n = int(input())
a = list(map(int, input().split()))
q = int(input())
s = sum(a)

cnt = [0]*(10**5+1)
for i in a:
  cnt[i] += 1
  
for i in range(q):
  b, c = map(int, input().split())
  s = s - cnt[b]*b + cnt[b]*c
  cnt[c] += cnt[b]
  cnt[b] = 0
  print(s)
