m@main=getLine>>=putStrLn.last.f.words>>m f[a,b,c]=c:[w++b++z|(w,x)<-s c,(y,z)<-s x,y==a] s x=map(`splitAt`x)[0..999]