main=do
 getLine
 ss<-lines<$>getContents
 let x=last ss
 let st=map((\[s,t]->(s==x,read t)).words)$init ss
 print$f False st
f x[]=0
f True((s,t):st)=t+f True st
f False((s,t):st)=if s then t+f True st else f False st