BERLIN (AP) — A far-right party made strong gains in a pair of state elections in eastern Germany on Sunday, but opponents from the political mainstream won both votes — salvaging their position as the top vote-getters and providing some relief for the national government.

Voters in Saxony, a region of around 4.1 million people bordering Poland and the Czech Republic, and neighboring Brandenburg, which has 2.5 million inhabitants and surrounds Berlin, elected new state legislatures.

All eyes were on the performance of the far-right Alternative for Germany party, or AfD, which is strongest in the ex-communist east, and on how badly Germany's governing parties would do after a rough 18 months for Chancellor Angela Merkel's coalition in Berlin. A symbolically important AfD win in either state could have further destabilized the national government.

AfD co-leader Joerg Meuthen declared himself "highly satisfied" with the outcome, saying that "things can't go much better than this." But opponents voiced some relief that his party was held to second-place finishes.

"The good signal in both states is that a few weeks ago the far-right was ahead, and today there was a clear signal against AfD," said Lars Klingbeil, the general secretary of the center-left Social Democrats, Merkel's junior partners in Berlin.

"People don't want AfD to be the strongest party there, but nevertheless it is a task for all democratic parties to see how we push them back," Klingbeil added. "These election results are much too high."

The governing parties performed better than pre-election polling predicted. However, both lost ground compared with the last state elections in 2014 — before the migrant influx that boosted AfD's support and helped it into Germany's national parliament in 2017.

Merkel's center-right Christian Democratic Union won 32.1% of the vote in Saxony, which it has governed since German reunification in 1990, down from 39% five years ago. AfD took 27.5%, which was its best performance yet in any state election and compares with 9.7% five years ago.

In Brandenburg, the Social Democrats won 26.2% of the vote, down from 31.9% five years ago. Like the CDU's showing in Saxony, it was their worst performance there in 29 years of democracy. The Social Democrats have led Brandenburg since reunification.

AfD won 23.5%, up from 12.2% in the 2014 state election.

Some voters may have returned to the big traditional parties to prevent AfD from winning.

The Greens, who have traditionally struggled in the east but surged in national polls over recent months, made only fairly modest gains Sunday, taking 8.6% in Saxony and 10.8% in Brandenburg. The environmentalist party may, however, be needed to govern both regions.

Merkel's CDU and the Social Democrats govern Germany together in a fractious national coalition, and both are weak in national polls.

In Saxony and Brandenburg, they face the task of putting unwieldy new governing alliances together, as the coalitions both lead appeared to have lost their majorities. The CDU currently runs Saxony with the Social Democrats as its junior partner. The Social Democrats lead Brandenburg in a coalition with the Left Party, which is further to their left.

Mainstream parties have pledged not to form coalitions with the six-year-old AfD, which in any case has thrived on opposition.

Bernd Baumann, its chief whip in the national parliament, said it "pushes the issues forward that really interest people out there."

"I am glad that more and more people in the country say, 'We will take our country back from the left-wing, green mainstream, from the old parties to certain media,'" he said.

Saxony has long been a hotbed of far-right groups. It is not only an AfD stronghold, but also the state where the anti-migration group PEGIDA —Patriotic Europeans against the Islamization of the West — rose to prominence with weekly protests in Dresden at the height of the 2015 migration crisis.

More broadly, AfD has tapped into disillusionment in the east, particularly in rural areas, among people who feel left behind after nearly three decades of German unity. Promises of equal living standards did not always become reality, salaries in the east still lag behind those in the west and many young people have left to seek opportunities elsewhere.

In comparison, AfD took 11% of the vote nationwide in the European Parliament election in May.