Afghanistan and Pakistan, home to al-Qaida and Taliban militants and the focus of the longest war in U.S. history, face a new, emerging threat from the Islamic State group, officials have told The Associated Press.

Disenchanted extremists from the Taliban and other organizations, impressed by the Islamic State group's territorial gains and slick online propaganda, have begun raising its black flag in extremist-dominated areas of both countries.

In Pakistan, an online video purportedly shows militants beheading a man while pledging their allegiance to the IS. In Afghanistan, there have even been reports of militant rivalries, with clashes erupting between Taliban fighters and Islamic State militants.

Analysts and officials say the number of IS supporters in the Afghan-Pakistan region remains small and that the group faces resistance from militants with strong tribal links. However, the rise of even a small Islamic State affiliate could further destabilize the region and complicate U.S. and NATO efforts to end the 13-year Afghan war.

The Taliban remain the region's pre-eminent insurgency, with nearly 20 years of experience battling Afghan warlords and international troops. But the Taliban are "not a particularly sexy ideology or military force, and the risk lies in the Taliban looking increasingly out of date," said a Western diplomat, speaking on condition of anonymity to discuss intelligence matters.

"It could be that young Afghans look to the more extreme tactics and the great glitzy publicity of IS," the diplomat said. "They might find it attractive, or the Taliban might feel the need to compete and therefore become a bit more extreme and start carrying out horrific acts the way you see IS doing."

The Islamic State group controls a third of both Syria and Iraq, where it declared a caliphate governed by a harsh interpretation of Islamic law, or Sharia, and demanded the allegiance of the world's Muslims. The Taliban, by contrast, are focused on Afghanistan and Pakistan, and some leaders have even responded to past peace overtures.

Smaller militant groups in Libya, Egypt, Lebanon and elsewhere have pledged allegiance to the IS group's leader and self-proclaimed caliph Abu Bakr al-Baghdadi, with some referring to themselves as "provinces."

In Afghanistan's southern Helmand province, residents say a former Taliban commander named Mullah Abdul Rauf has begun recruiting fighters for the Islamic State group.

"People are saying that he has raised black flags and even has tried to bring down white Taliban flags in some areas," said Saifullah Sanginwal, a tribal leader in Sangin district. "There are reports that 19 or 20 people have been killed" in fighting between the Taliban and the IS, he added.

Pakistan's Interior Minister Chaudhry Nisar Ali Khan said in November there was no Islamic State group presence, only militants using its name. However, a government letter written a month earlier and later obtained by the AP warns local officials that the Islamic State group has begun courting area militants and that the extremists claim the support of up to "12,000 followers" in northwest Pakistan.

A video released in December showed female students and teachers at Islamabad's Red Mosque, which holds great sway with Pakistan's militants, sitting under an Islamic State flag, pledging support to al-Baghdadi as cleric Maulana Abdul Aziz said it would welcome the group. Last week, an online video released by the IS showed a former Pakistani Taliban spokesman pledging his support with more than a dozen others, before beheading a man they identified as a soldier.

The Pak Institute for Peace Studies, an Islamabad-based organization tracking militant groups, also issued a report calling the Islamic State group a "real and emerging threat for Pakistan."

"It has created a major survival challenge for the main militant actors who could now act to prove their operational credentials," the report said.

Taliban militants in Afghanistan and Pakistan owe their allegiance to Mullah Omar, a cleric who has led the Taliban since the 1990s but has not been seen or heard in public for years. Officials fear that an Islamic State push into the region could bring an infusion of guns and money, sparking brutal competition among local militants disenchanted with Mullah Omar's silence and eager to prove themselves with escalating atrocities.

One former Taliban commander pledged allegiance to the Islamic State group "because he felt alienated from the Taliban leadership," said Graeme Smith, an analyst focusing on Afghanistan for the International Crisis Group.

"He seems to be doing better as a result," Smith added. "He seems to have more money and weapons than before and people are noticing that maybe there is some actual link that is more than symbolic."

A senior U.S. State Department official, speaking this week in Pakistan on condition of anonymity to discuss diplomatic talks, said both Afghan and Pakistani officials shared their concerns with America about the Islamic State group seeking a regional foothold.

"There's no daylight between us in terms of wanting to be coordinated in efforts to thwart" the IS, the official said.

At the same time, the IS would struggle to mount a real challenge to the Taliban, which is deeply enmeshed in tribal Pashtun society, said Borhan Osman, a researcher with the Afghanistan Analysts Network.

But with NATO forces and U.S. troops now focused on training Afghans, as opposed to conducting combat operations, some worry the Islamic State group could gain ground and pose a serious threat in the future.

"We need to be very watchful," said Nasir Khan Durrani, police chief of Pakistan's Khyber Pakhtunkhwa province, which borders the lawless tribal region and militant haven. "We need to be careful."

___

Shahzad reported from Peshawar, Pakistan. Associated Press writers contributing to this report include Rahim Faiez in Kabul, Afghanistan; Mirwais Khan in Kandahar, Afghanistan; Riaz Khan in Peshawar, Pakistan; Abdul Sattar in Quetta, Pakistan; Adil Jawad in Karachi, Pakistan; Ishtiaq Mahsud in Dera Ismail Khan, Pakistan; Zaheer Babar in Lahore, Pakistan; and Matthew Lee in Islamabad.

___

Follow Lynne O'Donnell on Twitter at www.twitter.com/lynnekodonnell . Follow Asif Shahzad at www.twitter.com/asiffshahzad .