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

m = 10**5+1
nums = [0]*m
for i in a: nums[i] += 1

for _ in range(q):
  b,c = map(int, input().split())
  nums[c] += nums[b]
  nums[b] = 0
  ans = 0
  for i in range(1,m): ans += i*nums[i]
  print(ans)