Valency of X in the aluminium salt Al2X3 can be worked out as follows (we know that valency of Al is 3):
Thus, it is clear that the valency of X is two.
Formula for the magnesium salt of X can be worked out as follows:
Crossing the valencies gives us the formula of the magnesium salt of X i.e. MgX.