Scanner should work; see e.g. this solution.
When you had the “No such element Exception”, were you trying to “Run” without Providing “Custom Input”?
Edit:
Oh, that’s just because your logic/ output is wrong; for the example test input:
2
3
10 4
8 12
7 6
2
5 10
3 4
that solution outputs:
0 22 0
That solution seems to be reading the input in correctly, as far as I can tell.