WARSAW — Sunday’s parliamentary election wasn’t the first crucial political test faced by Beata Szydło, who is slated to become Poland’s next prime minister.

Her real trial came in 2011 when Jarosław Kaczyński, the founder of their Law and Justice party (PiS), was besieged by a group of young Turks he felt threatened his leadership. Szydło refused to join the rebel group, staying loyal to Kaczyński, as did Andrzej Duda, who was elected Polish president in May.

“Szydło is only getting the chance to be prime minister because Kaczyński was certain that she’d never break from him. It’s just the same for Duda,” said Marek Migalski, a political scientist and former MEP backed by Law and Justice who himself joined a group of party rebels and was ejected from Kaczyński’s inner circle.

Kaczyński, 66, isn’t Poland’s president and says he won't be prime minister, a job he held in 2006-2007. But after this year's twin successes for his right-wing party, he looks like Poland's most powerful politician. It was Kaczyński who plucked Duda from the back benches of the European Parliament to run for president. He chose Szydło to be his party’s candidate for prime minister.

“As of Monday, Kaczyński is the king of Poland,” Migalski said.

The question in the Polish capital in the hours after an unprecedented political housecleaning is how Kaczyński will wield his power, and to what ends. His long life in politics provides a few possible answers.

Retreat to win

Say what you will, he is a political original who has experienced brief highs and frequent stumbles. He seemingly learned from both.

The former childhood film actor and Solidarity activist retains the fervent loyalty of older, rightwing stalwarts in Law and Justice, known as PiS in Poland. “Jarosław save us,” is a common cry at party rallies.

“With Jarosław Kaczyński as candidate for prime minister [we] would not be able to win the elections” — Jarosław Kaczyński

At the same time, Kaczyński is Poland’s most divisive politician. The CBOS polling organization found he was trusted by less than a third of Poles. In the previous eight years, in successive presidential and parliamentary elections, the more centrist Civic Platform party fell back on one campaign slogan: You don't want to let Kaczyński back into power.

The scaremongering worked as long as memories stayed fresh of his short stint as prime minister in 2006-2007. His governing coalition fought bitterly and relations with Germany were terrible. The PiS government relied on secret police agencies and powerful prosecutors to track down crooks, spies, and bent businessmen that Kaczyński said ran Poland from behind the scenes.

This year, Kaczyński didn't contest any of the leadership spots. “I understood that I have no chances for such a position,” Kaczyński said in a recent interview with the Dziennik Polski newspaper. “PiS with Jarosław Kaczyński as candidate for prime minister would not be able to win the elections. In such a situation you have to be able to retreat.”

With Kaczyński’s support and trust, Duda and Szydło became the new faces of Law and Justice.

Szydło, 52, will soon be setting up office in the pastel-yellow buildings that house Poland’s prime minister. Duda’s residence is an old aristocratic palace in the heart of the Polish capital. But the country’s real center of power may be a decrepit grey concrete office building on the edge of downtown with a pool hall in the basement. That’s where Kaczyński has his office, and where PiS has its headquarters. In an example of the power he wields, it was from that office that he decided in 2006 to fire another stand-in prime minister, Kazimierz Marcinkiewicz, and to take his place.

According to people who know him, loyalty is paramount for Kaczyński. His closest relationships were with his mother, Jadwiga, who died in 2013, and his twin brother Lech, Poland’s president who died in the 2010 crash of a government airliner in Smolensk, Russia.

“His first characteristic when it comes to people is to put loyalists in key positions,” said Paweł Zalewski, a former senior member of PiS who defected to join Civic Platform.

Conflict on the path to power

The loyalty he demands from others hasn't often been repaid. Kaczyński's political history is littered with double-crosses and shifts of allegiance.

He helped negotiate a 1989 accord with the ruling communists that allowed for partly free elections that year and for the Solidarity labor union to form the country’s first non-communist post-war government.

Within months Kaczyński helped set off an internal war inside Solidarity between its leader Lech Wałęsa and Poland's first post-communist prime minister, Tadeusz Mazowiecki. He then turned on Wałęsa, leading fierce street opposition after Wałęsa was elected president.

Exiled into the political wilderness in the 1990s, Kaczyński alienated his old allies. He came back into the limelight when his more affable brother Lech became justice minister in 2000.

His first long march back to power culminated in 2005, when Lech was elected president and Law and Justice gained a parliamentary majority with the help of two smaller populist parties. Initially, Kaczyński promised that he would not serve as prime minister, instead appointing a little-known MP, Marcinkiewicz, to lead the government.

That lasted only nine months, before Kaczyński ousted the increasingly popular Marcinkiewicz and took the job himself.

“This will be the same situation now,” said Zalewski. “Neither Duda nor Szydło have the chance to become independent politicians.”

Kaczyński’s government collapsed in 2007 in a tangle of corruption allegations levied against one of his coalition partners.

How will Kaczyński wield power, and to what end?

Since then he has bid his time, planning his return. He built an alliance with the nationalist right wing of the Roman Catholic Church. He strengthened PiS’s support among those who felt left out by Poland’s dramatic economic transformation. He also pruned Law and Justice, crushing at least three rebellions in the last eight years and ruthlessly culling anyone who was disloyal. Every MP was put on the party lists with Kaczyński’s approval.

It's not easy to divine his ambitions for what to do with power. Kaczyński has never shown much interest in economics. His foreign policy has largely been limited to ensuring that Poland is respected as a serious European power. He personally hasn't spelled out much of a program at all. His motivations often seem more personal than political, tied to his brother's death and the need to seek Lech's rehabilitation. Some Polish analysts say the party wants to carry through an ambitious transformation, by constitutional means if possible, of Poland from a European liberal country to a solidly nationalist, Christian one.

Seeking revenge for a tragic death

One clear immediate aim was achieved: To oust the hated Civic Platform party, until recently led by his onetime ally turned bitter enemy Donald Tusk, now president of the European Council.

“Removing the current government from power is my primary patriotic duty,” he said in the newspaper interview.

His disdain for Tusk was heightened by the Smolensk air disaster, which investigators have blamed on undertrained pilots trying to land the plane in dense fog. Kaczyński and his supporters accuse Tusk and his government of complicity in a cover up.

“In a political sense you bear 100 percent of the responsibility for the catastrophe,” Kaczyński told Tusk in parliament in 2012.

Kaczyński and his lieutenant, Antoni Macierewicz, are likely to unleash the full power of the state to find those they deem guilty of Lech’s death.

“Of course there will be revenge,” said Migalski.

“In a political sense you bear 100 percent of the responsibility for the [Smolensk] catastrophe” — Kaczyński to Tusk, 2012

Speaking before a delirious crowd of supporters after voting ended Sunday night, Kaczyński didn't talk about policies and projects. He talked about his brother. "Mr. President, mission accomplished," said Kaczyński, close to tears.

Lech’s death, and his mother’s passing shortly after that, left Kaczyński alone. He has few friends and has never married. His closest companion is his cat, Fiona. Politics "limited the circle of people I meet, although I've never been very sociable," he told the Dziennik. "That group of friends slowly shrank and crumbled away because I was too busy ... It’s my job. For 20 or 30 years I’ve done nothing else." He still remembers his brother’s mobile phone number and said he feels the need to talk to Lech “several times a day.”

Michał Krzymowski, the author of a new book, "The Secrets of Jarosław Kaczyński," said in an interview with the Onet portal, "Kaczyński is a secretive figure, a closed person, a loner who doesn't open up in front of others."

And his trust in Szydło? It’s high, but not absolute.

“If Beata Szydło is a good prime minister, then she’ll rule for four years,” was the qualified comment from Kaczyński to Polish television before the election. If that’s not the case, he added, “the interest of Poland is what's important.”

When voting ended Sunday and the exit polls showed a PiS rout, it was Kaczyński who made the first speech celebrating the victory, only turning to Szydło after a few minutes. When he did so, he addressed her as "Madame Chairwoman" (a reference to her party rank) and not "Prime Minister."