WASHINGTON (Reuters) - U.S. Representative Mark Sanford, a vocal critic of President Donald Trump, lost a Republican congressional primary in South Carolina after Trump urged voters to punish Sanford’s disloyalty by tossing him from office.

A few hours before polls closed on Tuesday, Trump tweeted that Sanford was “nothing but trouble” and “very unhelpful to me.” He backed Sanford’s pro-Trump challenger, state Representative Katie Arrington, for November’s congressional elections.

Arrington, who made a campaign issue of Sanford’s criticism of Trump, won 50.6 percent of the vote to Sanford’s 46.5 percent. That just crossed the 50 percent threshold to avoid a runoff later this month between the top two contenders.

The South Carolina race was a highlight of Tuesday’s primary voting in five states. Nevada, North Dakota, Maine and Virginia also chose candidates for the midterm election on Nov. 6, when Democrats hope to capture a majority of the U.S. Congress.

The upset of Sanford, a member of the conservative House Freedom Caucus, was the latest sign of Trump’s firm grip on the Republican Party. The onetime insurgent has made allegiance to his leadership a litmus test in many Republican races.

Sanford had been critical of Trump at times, saying he “fanned the flames” of intolerance and decrying what he characterized as his disregard for facts. But during the campaign, the congressman ran ads saying how often he voted with the president.

That was not enough for Trump, who also mocked Sanford with a reference to a 2009 scandal when the then-governor disappeared for days before surfacing to say he was “hiking the Appalachian Trail.” Later, Sanford admitted he was involved in an extramarital affair in Argentina.

Slideshow ( 4 images )

Trump, who issued his tweet as he returned from his summit with North Korean leader Kim Jong Un, wrote that Sanford “is better off in Argentina.” On Wednesday, he congratulated Arrington in another tweet, saying his advisers had earlier told him to stay out of the race but that he “had to give it a shot.”

Republican Representative Justin Amash of Michigan, Sanford’s fellow conservative and House Freedom Caucus member, fired back at Trump after the president’s attack on Sanford.

“Unlike you, Mark has shown humility in his role and a desire to be a better man than he was the day before,” Amash wrote on Twitter.

Sanford is the second congressional Republican to lose in a nominating contest this year, following Robert Pittenger’s loss in North Carolina last month. But Sanford’s South Carolina district is considered a safe Republican seat, meaning the outcome is unlikely to play a role in November’s battle for control of the House of Representatives.

Backing Trump did not guarantee success in South Carolina. Republican Governor Henry McMaster, an early supporter of Trump’s 2016 presidential campaign, did not win a majority and was forced into a June 26 runoff primary with a political novice, businessman John Warren.

McMaster was the first statewide elected official to back Trump’s 2016 presidential campaign but has faced questions over corruption in state government.

Slideshow ( 4 images )

DEMOCRATS TARGET COMSTOCK

In Virginia, a state senator favored by the Virginia Democratic Party establishment won a crowded battle for the right to challenge one of the most endangered congressional Republicans in November’s elections.

Democrat Jennifer Wexton won a six-way primary race and will take on Republican Barbara Comstock, one of the Democrats’ top targets in their push to pick up the 23 seats they need to claim a majority in the House.

Comstock’s district in the Washington suburbs, home to many federal government workers, has drifted left in recent years. Democrat Hillary Clinton carried it by 10 percentage points over Trump in the 2016 presidential election.

Corey Stewart, a combative conservative Republican and immigration hard-liner who nearly upset Ed Gillespie for the party’s gubernatorial nomination last year, won the right to face Democratic Senator Tim Kaine, the 2016 vice presidential candidate, in November’s U.S. Senate election.

Trump weighed in on Stewart’s victory on Wednesday, resurrecting his 2016 attacks against Kaine and saying in a tweet that Stewart had “a major chance of winning.”

In North Dakota and Nevada, voters set the stage for two of the most competitive U.S. Senate races, when Democrats must pick up two seats to capture a Senate majority.

In North Dakota, Republican Representative Kevin Cramer easily won the nomination to challenge Democratic Senator Heidi Heitkamp. In the swing state of Nevada, Democratic Representative Jacky Rosen sailed to the nomination to face vulnerable Republican Senator Dean Heller.