If you've ever been to Gibraltar, the Mediterranean rock run by the British, you know about the monkeys. About 200 of them run wild on the tiny peninsula, and they're the only free-ranging monkeys in Europe. They love to climb on tourists' heads and will gladly accept a snack.

What you didn't know is where they came from. Scientists haven't even known for sure.

Gibraltar, a tiny British territory jutting out from southern Spain, sits at the strategically crucial pinched entrance to the Mediterranean. During World War II, there were as few as three macaques left on the rocky outpost, historians say. There was a legend back then that Britain would lose the territory if the monkeys died out. So in 1942, Prime Minister Winston Churchill ordered that their numbers be replenished. Details were kept secret.

Many scientists suspected the imported macaques came from Morocco. Some thought they might also have come from Algeria.

In a new study, researchers analyzed the monkey's DNA to learn that they are descended from macaques in both Morocco and Algeria.

The isolation of the Gibraltar monkeys since the war mimics a phenomenon in nature caused by forest clearing and other pressures.

"The Gibraltar colony of Barbary macaques provided an ideal example of genetic isolation of a small population, which is now a regular occurrence among wild primate populations because of forest fragmentation," said Robert Martin, a primatologist and Field Museum provost. "To our surprise, we found a relatively high level of genetic variability in the Gibraltar macaques. This is now explained by our conclusion that the population was founded with individuals from two genetically distinct populations in Algeria and Morocco."

That still leaves a mystery of where the original Gibraltar macaques came from.

Some scientists think they were brought by the Moors, who occupied Spain between 711 and 1492. Others figure they were a remnant of macaques that lived throughout southern Europe 5.5 million years ago and possibly, some speculate, as recently as the 1800s in Spain.

The research was detailed Monday in the early edition of the Proceedings of the National Academy of Sciences.