ISLAMABAD, Pakistan - At the foot of green Margalla hills, on the northern edge of the Pakistani capital, stands the Shah Faisal Mosque, the fourth-largest in the world. As thousands of faithful knelt inside the cavernous interior at prayer time, their words echoed while they prayed for those who died in America in the deadliest terrorist attack in U.S. history.

"I'm really very sorry," said 18-year-old student Abdul Abd-ur-Rehman, placing his hand on his heart. "It's very bad news for people all over the world."

When talk turns to the United States' expected retaliation, however, many Pakistanis' sympathies lie not across the Atlantic, but across the border with their Muslim neighbor, Afghanistan. What many Americans see as an issue of swift justice and revenge is a complex, potentially explosive matter in Pakistan, a pivotal nation in the Bush administration's drive to punish those who leveled the World Trade Center and severely damaged the Pentagon.

In coming weeks, Pakistan's leaders must try to accommodate the demands of the United States while controlling the anger of their own people. The results could ripple throughout the Muslim world.

Pakistan shares a 1,500-mile border with Afghanistan, home of Saudi exile Osama bin Laden, whom Washington calls its prime suspect in Tuesday's attacks. To launch air strikes into Afghanistan, the United States would presumably need to fly in from the Arabian Sea and across Pakistan.

But some Pakistanis remain unconvinced that bin Laden is responsible, and say they will oppose their government if its leaders help the United States punish Afghanistan. As prayers finished at the Faisal Mosque, students spilling outside spoke of solidarity with the Afghan people.

"How can we just sit in our houses and watch our brothers in Afghanistan die?" said M. Akif Khan, 24, a law student at International Islamic University. "We will go against the government. We will go fight with them."

Pakistani President Pervez Musharraf faces a terrible dilemma. As the United States moves toward military action, he must choose between making an enemy of the world's superpower or perhaps many of his people.

Musharraf and his National Security Council said yesterday it would comply with all U.N. resolutions that seek to combat "international terrorism," but would stop short of using Pakistani troops beyond their border.

In Washington, Secretary of State Colin L. Powell said Pakistan has agreed to help the United States "in whatever might be required" in dealing with neighboring Afghanistan.

"The Pakistani government was very forthcoming," Powell said, "and we're appreciative."

Pakistan's foreign minister, Abdul Sattar, declined to give details of what he called a list of proposals received from the United States. "We are in the process of discussions of the specific proposals. I can only indicate to you a general policy of support," he said.

The government's willingness to help Washington could bring protesters into the streets, and make Pakistan a target for Muslim militant groups that might launch attacks inside the country.

Some talk of rebellion.

"The people will react strongly, and there will be a civil war," said Wakar Ahmed Abbassi, 40, a fruit-stand owner in the hill country outside Islamabad. "People in other Muslim countries will definitely support civil war here."

Most analysts think Musharraf has little choice but to agree to the list of U.S. demands, which are said to include overflight privileges, stationing a multinational force inside Pakistan, cutting off fuel to Afghanistan and closing the border.

"If [Pakistan] refuses, the U.S. may have few qualms about embracing India and turning the screws on Pakistan, plunging it into economic ruin and political anarchy," said a front-page editorial in The Friday Times, a weekly magazine.

At the heart of tensions building here is the profoundly different way Pakistanis and Americans view last week's attack and the person the U.S. government believes is responsible.

While the West regards bin Laden as a ruthless terrorist, many Pakistanis revere him as a religious hero, in part because he stands up to America. Most say they can only support military action if the United States convincingly shows he was involved.

And while the West views Afghanistan as a desperately poor, extremist Muslim state, many Pakistanis feel a kinship with the Afghans. The two Islamic nations have had a long and close relationship. In the 1980s, Pakistan stood on the front lines of the Cold War, helping Afghanistan drive out the Soviet invaders. Among the main supporters of the Afghan resistance was the United States. That the United States is trying to destroy bin Laden, an operative it once funded, strikes Pakistanis as hypocritical.

"Why are you after Osama?" said Muhammed Aslam, 66, a retired soldier. "He was a man who worked for America."

Many Pakistanis also argue that the U.S. government brought the terrorist attack on itself by supporting what they see as unfettered support for Israeli against the Palestinians. That is what Pakistani tourists said in the village of Murree, in the cool hills of Punjab, within sight of the mountains of Kashmir.

Yesterday, Tahir Raja, 31, a businessman, strolled along Murree's main street past the clothing stores with his wife, Fakher, and their 1-year-old son, Ibrahim. He said that Washington bears some of the responsibility for last week's attacks.

"America is responsible for this attack because of its foreign policy," Raja said as Ibrahim sat in his mother's arms twisting a balloon animal. "Daily, there are many Muslims in Palestine being killed by Israelis. The Camp David agreement is a total fake."

There is also the issue of Kashmir, the mountainous border region where Muslim guerillas are fighting Hindu India in a "holy war" of liberation. "For 50 years, Muslims have been struggling for independence, but no one is helping them," Raja said.

Some analysts predict that in the short run the Pakistani government, a powerful military dictatorship, will be able to control its people. They doubt that the country will challenge the world's most powerful nation when it is on a crusade to stamp out terrorism.

"A lot of people are going to run for cover," says Rifaat Hussain, chairman of the Department of Defense and Strategic Studies at Islamabad's Quaid-I-Azam University.

A greater threat, Hussain says, are the thousands of Afghan refugees who might pour over the border into Pakistan and use terrorism to destabilize the country as punishment for Islamabad'ssupport of Washington.

"You have angry frustrated, gun-toting people running towards the border, so what is going to happen?" said Hussain. "You have to think of the aftermath. That is going to be a huge, huge challenge."