West Virginia teachers on Tuesday ended a four-day strike that had kept more than 277,000 students out of class, after the state's governor agreed to a 5 percent pay raise.

Republican Governor Jim Justice announced the deal after thousands of teachers and their backers descended on the state capitol in Charleston demanding better benefits and a hike in wages now near the bottom among U.S. teachers.

The proposal calls for teachers to get a 5 percent raise in the fiscal year starting in July and other state employees to get a 3 percent hike, Justice told a news conference. The deal requires approval by the Republican-controlled legislature.

"All of it works," said Justice, who had spent much of the day in talks with union leaders. Students will be back in class on Thursday after a "cooling-off" on Wednesday, he said.

Justice said he would appoint a task force to look into the Public Employees Insurance Agency, which runs health programs for public workers. He suggested that a $20 million investment to stabilize the agency could come through an increase in severance taxes on coal and natural gas.

Justice said the raises could be paid for by boosting state revenue estimates by $58 million, based on the impact of federal tax cuts and an improved U.S. economy.

“This is the beginning of better things to happen,” Christine Campbell, president of the West Virginia Federation of Teachers-West Virginia, was quoted as saying by the MetroNews website.

Teachers walked out on Thursday after Justice signed legislation to give teachers and state police a 2 percent raise. Teachers were also expected to get 1 percent increases in each of the next two fiscal years.

West Virginia ranked 48th among the 50 states in average teacher's salary in 2016, at $45,622, according to National Education Association data.

(Reporting by Ian Simpson in Washington; Editing by Peter Cooney)