JOHANNESBURG (AP) — Tim Paine was in agony. Australian cricket in torment.

Both showed some guts on Day 3 of the fourth and final test in South Africa on Sunday, but it likely wasn't going to be enough to avoid a big defeat and a first series loss in South Africa in nearly 50 years.

New captain Paine made 62 with Australia struggling in the series decider and played through the pain of a fractured thumb sustained while he was wicket-keeping the day before.

But despite Paine's fighting half-century — and another one from bowler Pat Cummins — South Africa bowled Australia out for 221 in its first innings and extended its lead to 401 runs by the end of the day.

South Africa ground its way toward a series win with an unbeaten 40-run partnership between Dean Elgar (39 not out) and skipper Faf du Plessis (34 not out). Those two opted to be resolute and give away nothing.

Elgar made just 5 runs in 61 balls in that unbeaten stand to take South Africa to stumps on 134-3.

South Africa leads 2-1 and was edging toward a first series win over Australia at home since 1970.

That history might have been lost on an Australia team worried, instead, for its future after the ball-tampering scandal from the last match in Cape Town led to long-term bans for captain Steve Smith, vice-captain David Warner and batsman Cameron Bancroft, and thrust Paine into the top job.

Tasked with putting the shattered morale of the team back together, he led from the front, combining with Cummins for a 99-run partnership at the start of Sunday after Australia was in danger of collapsing at 96-6 on Day 2.

After being diagnosed with the fractured thumb on Saturday, Paine stayed on as wicketkeeper while Australia was in the field and also took his place in the batting lineup. He was 5 not out overnight and hit seven fours and two sixes in all to give Australia's first innings a little respectability despite it still being way behind South Africa's.

Paine was last man out to a running, diving catch by Elgar, which gave the innings a gripping finish. Fast bowlers Vernon Philander and Kagiso Rabada and spinner Keshav Maharaj all picked up three wickets each for South Africa.

Cummins made 50 and also removed Aiden Markram and AB de Villiers early in South Africa's second innings for his sixth and seventh wickets of the game. Cummins has maybe the biggest claim to be Australia's best player on one of its worst tours considering the stunning fallout from the cheating scandal.

While Paine battled it out, South Africa also had an injury complication, with retiring fast bowler Morne Morkel picking up a left side strain. He departed the field in the first session to get treatment and some strapping, but returned and would try and "push on" for the rest of his farewell game, the South Africa team said. South Africa wasn't yet sure if he'd be able to bowl during the last two days of his test career.

Du Plessis was also struck a painful blow on the right index finger, a finger he's broken and had problems with already this season.

When du Plessis was hit and reeled away, dropping his bat, Paine came over and appeared to show some concern for his opposite number.

That was one of a few examples of a good spirit between the teams — bowlers and batsmen chatted and smiled a couple of times — as one of the most ill-tempered cricket series in recent memory mellowed for the final few days.

As du Plessis and Paine stood together, the Bob Marley song "Everything's Gonna Be Alright" played on the stadium speakers.