n=int(input())
A=list(map(int,input().split()))
q=int(input())
dic={i+1:0 for i in range(10**5)}
for a in A:
  dic[a]+=1

s=sum(A)

for i in range(q):
  b,c=map(int,input().split())
  nom=dic[b]
  dic[c]+=nom
  dic[b]=0
  s+=nom*(c-b)
  print(s)
  