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