import collections
n = int(input())
a = list(map(int, input().split()))
C = collections.Counter(a)
ans = sum(a)
q = int(input())
for _ in range(q):
  b,c = map(int, input().split())
  ans -= C[b]*b 
  ans += C[b]*c
  print(ans)
  C[c] += C[b]
  C[b] = 0