n=int(input())
a=list(map(int,input().split()))
arr= [0] * 10**5
for val in a:
  arr[val]+=1
sums=sum(a)
q=int(input())
for _ in range(q):
  b,c=map(int,input().split())
  diff=(c-b)*(arr[b])
  sums+=diff
  print(sums) 
  arr[c]+=arr[b]
  arr[b]=0