(a) P is soap and Q is a detergent.
The give-away is the last column: only P forms scum, and forming scum with hard water is the characteristic failing of soap.
(b) Sample Y is the hard water — it is the only one in which P's lather collapses, from 6.0 cm down to 0.8 cm.
The scum is the insoluble calcium and magnesium salts of the fatty acid in the soap. Hard water contains dissolved Ca2+ and Mg2+ ions, and these displace the sodium:
2C17H35COONa+Ca2+⟶(C17H35COO)2Ca↓+2Na+
(sodium stearate, soluble)(calcium stearate — the scum)
(c) Both P and Q have a long hydrocarbon chain and a charged end, and both clean by forming micelles — the hydrocarbon tails burying themselves in the grease while the charged heads stay in the water. The difference is what happens when a Ca2+ ion arrives.
Soap (P) is the sodium salt of a long-chain carboxylic acid. Its calcium and magnesium salts are insoluble. So in sample Y the soap is precipitated out of solution as scum almost as fast as it dissolves — it is removed from the water before it ever gets the chance to form micelles. Almost none is left in solution to lather, which is why the lather falls to 0.8 cm. The soap is not just working badly; it is being destroyed.
Detergent (Q) is the sodium salt of a long-chain sulphonic acid (or an ammonium salt with chloride or bromide ions). Its calcium and magnesium salts are soluble. The charged ends of detergents do not form insoluble precipitates with calcium and magnesium ions, so the detergent stays in solution and carries on forming micelles exactly as before. Nothing is precipitated, nothing is lost, and the lather is essentially unchanged at 5.9 cm.
In one line: soap is precipitated as scum by the calcium and magnesium ions of hard water, and a detergent is not.