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