n=int(input())
a=list(map(int,input().split()))
ans=sum(a)
li=[0 for i in range(100000+1)]
for i in range(n):
  li[a[i]]+=1
q=int(input())
for dummy in range(q):
  b,c=map(int,input().split())
  ans+=li[b]*(c-b)
  print(ans)
  li[c]+=li[b]
  li[b]=0