ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()
nums = [0]*10**6
for i in range(n):
  nums[a[i]]+=1
q=ni()
for i in range(q):
  b,c=nm()
  nums[c]+=nums[b]
  nums[b]=0
  ans=0
  for j in range(len(nums)):
    ans+=nums[j]*j

  print(ans)
