n=int(input())
a=list(map(int,input().split()))
counter = [0] * (10**5+1)
for i in a:
  counter[i] += 1
q=int(input())
res=sum(a)
for i in range(q):
  b,c=map(int,input().split())
  res += counter[b]*(c-b)
  counter[c] += counter[b]
  counter[b]=0
  print(res)