Sen. Ted Cruz of Texas won Alaska's Republican "Super Tuesday" contest, edging out New York billionaire Donald Trump with 40 districts reporting late Tuesday night.

The last votes trickled in just before midnight, bringing the total cast to 21,930, far more than the much less interesting 2012 poll.

Cruz had 36.4 percent of the vote compared to Trump's 33.5 percent, according to the unofficial results from the party. Trump and Cruz were separated by 627 votes, or 2.9 percentage points.

Sen. Marco Rubio of Florida was well behind with 15.1 percent, retired neurosurgeon Ben Carson had 10.9 percent and Ohio Gov. John Kasich had a meager 4.1 percent.

Alaska Republicans will send 28 delegates to the national convention July 18 in Cleveland. Twenty-five of them will be pledged to candidates on a proportional basis according to the Tuesday poll, and one each from the remaining three will go to the top three winners. That means Cruz just squeaked by with a one-delegate margin: 12 for him, 11 for Trump and 5 for Rubio, according to party officials.

The election was conducted by the state Republican Party, which organized around the 40 state legislative districts. Cruz won 24 districts and Trump won 15. Carson took only one, in Bethel, and Rubio got none.

[District-by-district results from the Alaska Republican presidential preference poll]

The Fairbanks area and Kenai were solidly for Cruz, while Trump won big in parts of the Matanuska-Susitna Borough, especially around Big Lake and the northwest part of the borough, and by a smaller margin in Wasilla. Cruz carried Palmer, the Richardson Highway communities and southern parts of the Mat-Su Borough. Anchorage was split, with Rabbit Creek, Bear Valley. the Turnagain Arm communities, the U-Med District, Spenard, Mountain View and Sand Lake going for Trump, while Chugiak-Eagle River, East Anchorage, downtown, the lower and middle Hillside and Klatt-Ocean View supporting Cruz. Southeast Alaska split too, with the southern part voting for Trump and the northern voting for Cruz. Nome and Barrow were solidly for Trump, but like Bethel, they had few Republican voters. The three districts together cast only 155 Republican ballots.

Crowded polling places

Alaskans formed long lines at polling places around the state to cast votes for the Republican nominee for president, with some registering as Republicans on the spot. Late Tuesday night, Peter Goldberg, chairman of the Alaska Republican Party, said party officials were "amazed" at the turnout, which prompted the printing of additional ballots and voter registration materials.

About 14,100 people voted in the last presidential preference poll in 2012, and the Alaska Republican Party printed 25,000 ballots for Tuesday's poll.

After the polls closed at 8 p.m. Tuesday night, volunteers were hand-counting ballots at the voting sites and relaying results to party officials in Anchorage.

At O'Malley's on the Green on the Hillside in South Anchorage, the line wrapped down a hallway and out to a front parking lot. It was so busy at 3:15 p.m., right after polls opened, that 81-year-old Joan Hollenbeck decided had to come back two hours later.

"It's crazy busy, just a ton of people," said volunteer Jamie Donley, standing by the front entrance.

"So much more than we expected," volunteer Lisa Haugen chimed in.

Donley said the polling station ran low on forms used by voters to identify themselves for name-matching in a Republican database. Party officials said the database was designed to keep people from voting twice.

Only registered Republicans could participate in the poll, and some people registered on the spot. Doyle Crane, 51, was waiting in line with a registration form in his hand. He was registering as a Republican for the first time in his life to vote for Trump.

"He's hard on politicians," Crane said of Trump.

A few years ago, Crane said, he registered as a Libertarian to help keep that party afloat.

Ward Hurlburt IV, 55, an Anchorage-based pilot for Alaska Airlines, said he was voting for Cruz and pulled out his phone to display an email to his parents with a bullet list of reasons for doing so.

"He actually showed up in our state," Hurlburt said, referring to Cruz's visit to Alaska in 2014 to campaign for Sen. Dan Sullivan.

Another voter, 59-year-old Jon Andreassen, said he supported Cruz because of Cruz's support for giving federal land to state governments, a longstanding issue in Alaska.

Greg Schmidt, 56, supported Carson. He said he was "a little scared by Trump."

Schmidt voted in the Republican Party's last presidential preference poll too.

"It seems busier, it's packed," Schmidt said.

Waiting in Wasilla - former Gov. Palin

At First Christian Church in Midtown Anchorage, cars were backed up in the parking lot and the line of people went out the door. Among people in line, Trump and Cruz seemed to be the preferences -- though a few voters, asked whom they were voting for, would only say "not Trump."

Paris King, 28, lives in Soldotna but was casting her vote in Anchorage. She said she decided to support Cruz at the last minute.

"I trust his leadership," King said.

Eric Tetpon, 52, an artist in Spenard, said he was supporting Trump, casting what he called "a vote for America."

Another Trump supporter: former Alaska Gov. Sarah Palin, who used Facebook to post photos of herself waiting in line to vote at the Menard Memorial Sports Center in Wasilla.

"Waiting in a very long, long line to cast a vote for Donald J. Trump," Palin wrote.

In Juneau, a steady stream of voters flowed through Juneau's convention center Tuesday evening, where capital city Republicans had set up their poll.

Locals there rubbed shoulders with a string of elected officials and legislative staffers, some of whom arrived at the voting booths after descending a three-story outdoor staircase from the Capitol.

Of three GOP lawmakers interviewed — Senate President Kevin Meyer and Rep. Craig Johnson, both of Anchorage, and Rep. Dave Talerico of the Interior town of Healy — none would say whom they voted for.

Mike Higgs, a 63-year-old retired engineer, said he voted for Trump because his first preference, Ben Carson, was unlikely to win the nomination.

"I'm American. I vote for the Constitution. And I want to have some change," Higgs said. "He's a little bit crude, but I think he's going to be able to get stuff done."

Mandee Collins, 26, said she showed up with her family to vote against Trump, calling him "highly racist" and vindictive. Instead, she picked Ted Cruz.

Collins, who works at a local law firm, said she agrees with some of Trump's politics, like the fact that he's strongly opposed to illegal immigration.

"But I don't think rounding them all up and deporting them and then building a wall to keep them out is the best course of action," she added.

As for Trump's idea to bar Muslims from entering the country, Collins' father, Chuck said: "If we're going to be the land of the free, it has to be free for everybody."

Trump would still get the Collins family votes if he wins the GOP primary, they said.

"Clinton, we've had all of that I can take," Chuck Collins said. "Sanders, I think he'd be hard on my paycheck."

Trump's rhetoric didn't bother everyone at the polls, however — not even a middle-aged man who declined to give his name but who said he immigrated to the United States from a South Pacific island nation 26 years ago.

The man, who said he was a Democrat for his first 10 years in the United States, said he voted for Trump because of his frustration with the political establishment, and because of Trump's leadership capability.

Trump's incendiary comments about immigrants and Muslims, the man said, are theater and meant at getting free media coverage.

"I'm an immigrant," he said. "I don't see it in him."

Voting in a dentist office

In Bethel, though, voting was slow at the Lions Club. Bethel has fewer than 500 registered Republicans. Jon Cochrane, a longtime Republican who works as a bank branch manager, said he was asked to run the polling site about a week and a half ago after no one else stepped up. He said he agreed to ensure rural residents have a voice.

"If we get 50 people, it will be a good night," Cochrane said. As it happened, by 8 p.m. they got 42.

Voter Bryan Malvich, who switched from undeclared to Republican on the spot, said he selected Ben Carson.

"I just didn't want Trump," said Malvich, who works at the post office. He said he likes the fact that Carson is not a politician. But: "the main thing is not Trump."

Robert Tyree, who moved to Bethel in the fall to work as a family physician, also changed his affiliation to GOP. He said he's from Ohio and his wife sent him to the polls to vote for Ohio Gov. John Kasich.

"He's seems to be very even-tempered and I like that feature," Tyree said.

But when it comes to the general election, he added, "I might vote for the Democrat."

He said he feared Trump might unnecessarily push the country into war.

"I think he's a little bit rude," Tyree, a retired Army doctor, said. "I've been in the military and I just wouldn't want him to be my commander-in-chief."

At the state Republican Party's headquarters in Anchorage earlier Tuesday afternoon before the polls opened, Peter Goldberg, the chairman of the Alaska Republican Party, had just hung up the phone after telling a voter that only registered Republicans could participate.

"There's not been 10 seconds in between calls," said Goldberg, who was wearing a red tie with small blue elephants on it.

The preference poll is being conducted by the party. The polling places were mostly community centers and churches. In the small Southeast island town of Craig, the polling place was the office of dentist Scott Brookshire.

Brookshire, who has lived in Craig for 22 years, said a friend who works for the state Legislature contacted him about 10 days ago about holding the poll. He decided to do it and set up the poll in the waiting room of his office on Spruce Street in the middle of Craig.

"A dentist always wants a full waiting room, but it's going to be more full than normal," Brookshire said in a phone interview shortly before the poll opened.

As well as running the poll, Brookshire said he had 12 patients booked for the rest of the afternoon.

"I'll put the drill down, take the gloves off, wash, go answer any questions ... and then go back to see a patient," Brookshire said. "It's Alaska — you make do."

Alaska Dispatch News reporters Nathaniel Herz contributed from Juneau and Lisa Demer from Bethel.