Ideally, an ion would like to have 8 valence electrons. It can achieve this stable state by either gaining enough electrons to let it have 8 valence electrons or losing all of its valence electrons.
For the first question, X has 7 valence electrons and would therfore want to gain another electron. Z wants to lose 3 electrons. Since Z will give up three electrons, it will take 3 X atoms to take up those electrons, giving the formula X3Z.
Now, try the other three.