Help with Newtons laws and tension?

The block of mass M in the following figure slides on a frictionless surface.

Find an expression for the tension in the string.

Express your answer in terms of the variables m, M, and appropriate constants.

I tried starting with F=ma to get:

T=Ma <—— for block M

T=ma-mg <– for Block m

But I can’t figure out how to get a single expression out of them, and I can’t see any other way to solve this. Can someone please help me?

Answer: