Opener Aiden Markram struck 78 and seamer Duanne Olivier bagged 4-48 as South Africa seized control of the second Test against Pakistan on day one at Newlands.
Olivier and Dale Steyn (3-48) proved too hostile on a bouncy wicket for the tourists, who were bundled out for just 177 despite Sarfraz Ahmed's 17th Test fifty.
South Africa reached 123-2 before stumps to trail by 54 runs - Markram driving authoritatively to collect 14 fours and one six before he was bowled by the final delivery of the day, a ball from Shan Masood that kept low.
The wicket broke a second-wicket stand of 67 with Hashim Amla, who struck 24 not out after Dean Elgar (20) was caught behind off Mohammad Amir.
Earlier, Safraz struck a belligerent 56 after coming in at 54-5 and added 60 for the sixth-wicket with Mahsood (42) and a further 42 with Amir (22no).
Steyn, fresh from becoming South Africa's leading Test wicket-taker of all time in the Proteas' first Test win, set the standard for the day by forcing Fakhar Zaman to fence a short ball to gully in the fifth over.
Fit-again Vernon Philander - picked ahead of left-arm spinner Keshav Maharaj - trapped Imam-ul-Haq lbw for eight to claim his 50th wicket at Newlands before Olivier, who produced a match-winning display at Centurion last week, had Azhar Ali caught by Amla at first slip.
The cordon remained busy as Asad Shafiq and Babar Azam fell in quick succession to Kagiso Rabada and Olivier respectively and Pakistan's plight seemed desperate when Sarfraz, dismissed for two ducks in the first Test, walked out.
The captain led the fightback, hitting nine fours in an 81-ball innings, before he rather guided a short Olivier delivery into Quinton de Kock's gloves.
The wicketkeeper picked up his second catch of the innings by diving low in front of first slip to snaffle Mahsood off Rabada, then took his tally to four as Steyn made short work of the tail.
Pakistan had earlier made one change to their team, with seamer Mohammad Abbas back in place of Hasan Al, but he and Amir could not match the early in-roads achieved by South Africa's attack.
Markram moved to within touching distance of a fourth Test fifty - and then beyond it - with successive boundaries off Abbas, reaching the landmark with a neat clip through the leg-side.
The right-hander brought up a fifty-run stand with Amla in style off 89 balls by striking Yasir Shah for six over long on.
Watch day two of the second Test between South Africa and Pakistan, live on Sky Sports Cricket from 8.25am on Friday.