The dog is rescued from the rails at the last second