We can rearrange the mirror equation before plugging our values in.
1/p = 1/f - 1/q.
1/p = 1/10cm - 1/40cm
1/p = 4/40cm - 1/40cm = 3/40cm
40cm=3p <-- cross multiplication
13.33cm = p
Now that we have the value of p, we can plug it into the magnification equation.
M=-16/13.33=1.2
1.2=h'/8cm
9.6=h'
So the height of the image produced by the mirror is 9.6cm.