Match students and teachers based on ranking
Your problem differs from stable marriage problem in a minor way. A teacher can have 4 students maximum. You will have to fix the maximum students allotted to teacher (you may not have 3-4, if you also prescribe minimum number of students then you might have to dig deeper).

Now you can do two things, first, have four copies of each teacher and solve the stable marriage problem. Second solution is essentially like first but you keep a list of 4 "engagements"  for each teacher.