South Africa win eventful Test to level series