President Donald Trump's ability to swing a statewide race will get one of its biggest tests in Montana.

The president has made it a personal quest to unseat Democratic Sen. Jon Tester, a native Montanan and rancher who’s been working his way up the state’s political ladder for more than two decades.

Trump has been to the state three times on behalf of state auditor and GOP Senate candidate Matt Rosendale, who moved to Montana in 2002 and has fashioned himself as a Trump acolyte. Rosendale has been crisscrossing the state, from its snow-capped mountains to its vast cattle ranches, to tout the president’s agenda — even dressing to match Trump’s red tie and dark suit when they appeared together at a recent rally.

Tester has clung to a narrow lead in polls but Rosendale's support has gradually risen, largely pulled along by Trump, who won the state by 20 points in 2016. It was the president who prompted James Warneke, 46, a delivery truck driver in the eastern Montana town of Miles City, to do something different this year when he cast an absentee ballot for the Nov. 6 election.

"I voted straight Republican; I didn't used to but I do now," Warneke said. "I support Trump and what he's doing."

Trump is trying to encourage voters to turn out in races around the country, holding more than two dozen rallies nationally in advance of the election that will determine which party controls the House and Senate. The president's power to win over independents and encourage GOP turnout could make a difference in close contests like the one in Montana, but he could also drive Democrats to the polls.

Montana has a long history of ticket-splitting, re-electing Democratic Governor Steve Bullock in 2016 on the same day it voted overwhelmingly for Trump, a tradition that should help Tester. But both sides say the rhetoric is nastier now than ever, as Trump stokes animosity among conservatives.

The president says he has intervened in the race because Tester opposed Ronny Jackson, Trump’s one-time nominee to be veterans affairs secretary. The senator made public anonymous complaints about Jackson’s workplace behavior — including drinking on the job and abusive behavior toward subordinates — that Jackson has denied. Jackson ultimately withdrew.

"I said, 'I've got to come.' What he did was unfair, what he did was vicious," Trump told a Oct. 18 rally in the liberal bastion of Missoula. "How the hell did you elect this guy?"

Trump's presence in the state has polarized voters.

"The level of animosity that I'm encountering on the phones, it's off the charts," said Mary Catherine Dunphy, 67, a Tester volunteer in Miles City.

An average of four polls since September, compiled by Real Clear Politics, shows Tester with an average lead of 4.5 percentage points.

Tester, a plaid shirt-wearing, big-bellied farmer from Big Sandy, is countering Trump's attacks by traveling around the state talking about health care while exhibiting the folksy personality he developed as a music teacher and butcher.

Voters from both parties say Tester knows how to relate to them. He shoots gophers and raises cattle. Tester pronounces the state's name "mont-ANA," the way longtime residents do, while Rosendale, a Maryland native, says "mont-AH-na."

Tester's campaign plan involves picking off traditional Republicans from the farming, small business and veterans communities, activating union workers and rallying the university strongholds by enlisting the likes of actor Jeff Bridges, who stumped for him in a "Montana Abides" T-shirt last week.

At Little Big Horn College on the Crow reservation — Native Americans are another key part of Tester’s base — locals feted the Democrat with a victory drum circle and prayers to protect him from Trump’s attacks.

"We're going to need a record turnout in Indian country," Tester told the crowd noshing on fry bread, beans and brisket. Tribe members told stories of worsening meth addiction and joblessness.

Above him, a large mural depicted the Crow warriors who fought alongside doomed General George Custer in 1876 against Sitting Bull's Lakota tribe who were encroaching on Crow hunting grounds. Today, the tribe has a large number of veterans in need of better health care.

Tester doesn't criticize Trump on the campaign trail, focusing his fire on Rosendale.

"He wants to sell our public lands, he wants to privatize our public education system, he wants to kick people off of assistance. He claims to be a rancher but has no cows," he said at a veterans rally in the mining city of Butte.

Tester said he had a duty to veterans to bring up the Jackson allegations and that he ultimately voted for Trump's replacement nominee, Robert Wilkie.

He touts the 21 bills he sponsored that have been signed by Trump and says he has "no love" for Obamacare but wants to get cheaper premiums and cover pre-existing conditions.

The Democrat said Trump's visits haven't changed how he campaigns.

Tester quoted former Montana Gov. Ted Schwinden as saying, "By the time you run a statewide campaign, you've shook hands with and looked everybody in the eye that's going to vote for you and against you."

"That's pretty cool," Tester said.

Marian Jensen, a former Silver Bow County Democratic party chairwoman, said the key to Tester's success is compromise, including being pro-gun and fiscally restrained.

"He's very good on social issues but he doesn't rub it in people's faces," she said.

Former Montana Sen. Max Baucus, D, said the fact Tester is a native is his strongest asset.

"Montana is really one big small town," he said. "Tester is one of us, a farmer."

Tester said his support earlier this year for a partial repeal of the 2010 Dodd-Frank financial law illustrates how he differs from more liberal party leaders. He said its rules were hurting community banks.

The financial services industry has invested heavily in Tester, who serves on the Senate Banking Committee, with contributions exceeding $1.5 million. He has raised $17 million.

Tester said he's independent. "Who contributes to my campaign has nothing to do with what I do," he said.

On the campaign trail, Rosendale echoes core Trump themes, vowing to build a border wall and defund cities that don't cooperate with federal immigration enforcement, while backing tax cuts and deregulation. He has made his "A" rating from the National Rifle Association a major theme, and rails against Obamacare.

"I will never give up on repealing and replacing Obamacare," he said at the Missoula rally. "I will protect pre-existing conditions."

Rosendale says Tester isn't bipartisan enough, having voted against Trump's tax cuts and Supreme Court nominees.

"President Trump and all of his policies are on the line," Rosendale told the Missoula rally. "Jon Tester's been too wrong for too long."

In the final week of the campaign, Rosendale is planning an eight-city bus tour with the president's son, Donald Trump Jr. The strategy is a deliberate attempt at building a brand for a politician whose profile is much lower than Tester's previous opponents.

"Rosendale has attached himself very closely to Trump, more so than any previous campaign we've seen," said Jeremy Johnson, a political science professor at Carroll College in Helena.

Rosendale's campaign didn't respond to requests to interview the candidate.

Rosendale, whose campaign has raised $4 million, has been able to match Tester's massive television advertising campaign with outside support from conservative groups. Among them is the anti-tax Club for Growth, which says it's spending more than $4.5 million to back Rosendale and is seeking to cast Tester as a liberal tied to Minority Leader Chuck Schumer, D-N.Y.

“He goes home and plays up the good ol' boy in his ads but then comes back to D.C. and takes lobbyist money and votes for the Schumer agenda,” said Club president David McIntosh, dismissing Tester’s vote on the Dodd-Frank bill as unimportant.