Dynamical origin of Theia, the last giant impactor on Earth