Thrilling finish set up in Cape Town
Pakistan ended day three of an evenly poised second Test in the ascendancy against South Africa at Newlands.
The Proteas recovered from 139 for five overnight to reach 326, reducing their deficit to 12, after Robin Peterson compiled 84 to add to AB de Villiers' 61.
Dale Steyn and Vernon Philander then struck in their respective first overs to leave Pakistan seven for two, yet Azhar Ali and Misbah-ul-Haq led the fightback, putting on 55 to reach 100 for three.
Saeed Ajmal had ripped through South Africa's top order yesterday and when Dean Elgar edged a loose drive straight to Younus Khan at slip, the spinner had taken all six wickets to fall.
That came just after de Villiers was dropped on 37 by Azhar, a sharp chance at gully, and he duly pulled Ajmal for his sixth boundary to bring up a 91-ball half-century.
However, the wicketkeeper-batsman's innings ended when a leading edge off 7ft 1in paceman Mohammad Irfan spooned straight to Umar Gul at mid-on.
De Villiers had put on 55 with Elgar and 46 alongside Peterson, who then added a further 67 with Philander.
Peterson's second Test fifty came up with back-to-back boundaries off Tanvir and, in the meantime, Philander dug in for 22 before being ousted by an Irfan short delivery, the ball looping to Nasir Jamshed at gully.
A late cut for four took Peterson to his highest Test score, passing the 61 he made against Bangladesh in May 2003.
After losing Dale Steyn, he cut loose by switch-hitting Ajmal for the first of two boundaries in an over as he surged past 80.
Last man Morne Morkel punched Irfan to the backward-point boundary but the innings ended when Peterson lifted Mohammad Hafeez straight to Gul at long-on.
Irfan ended with 3-86 on debut and Ajmal 6-96, but the Proteas bowlers had a response up their sleeve.
Hafeez was trapped lbw by Steyn with the second ball of the innings and opening partner Nasir Jamshed also failed to trouble the scorers before being pinned in front by Philander.
Azhar and Younus took the score on to 45 before the latter slashed at a wider delivery from Steyn and chopped on to his stumps.
Azhar and Misbah set about building a patient partnership, which had reached just 10 in 9.2 overs when an injury to Morkel brought forward the drinks break; the seamer limped off struggling with his hamstring and Jacques Kallis had to finish the over.
Misbah's boundary off Kallis brought the first runs in 35 balls and he then broke the shackles to twice loft Peterson down the ground for six.
A third maximum followed with an over remaining, bringing up the fifty partnership and Pakistan's hundred. Misbah ended the day on 36 not out with Azhar five runs away from a half-century.