How to read n lines of input in JAVA if n is not give?

Example Problem
How do I take the input of this type in Java where n is not given?

Check some JAVA solutions of this problem, it should help. https://www.codechef.com/DEC17/problems/CPLAY