Do IM first, if you are taking the shelf exams. 75-80% of the Surgery NBME shelf exam is IM. You will do much better on the surgery shelf. I was warned before hand and studied a lot of IM in addition to surgery, but some of my colleagues were shocked after taking the exam how much IM there was on the test (none of us had taken the IM rotation yet). Granted, you will probably be taking this test with people who took IM before surgery. You should really aim to learn as much IM in the IM rotation to kick butt on the surgery shelf exam.
If you take Ob/Gyn before hand and all of your classmates taking the shelf exam have not had IM, and IF you can get away with studying IM during Ob/Gyn and Surgery, the two most time consuming ward experiences, then the more power to you and choose Ob/Gyn. I think you will find this difficult to do. After surgery and IM, the next highly weighted clerkship for surgery fields is Ob/Gyn. Are you willing to not do as well on Ob/Gyn to kick butt on the general surgery exam? I dunno. Think about these things and the pros and cons. That was my experience, but yours may differ. Don't say I didn't warn ya. 🙂