Only 1 test-case is passing

My code is not passing all test cases, please tell me what I have missed, my code:- https://ide.codingblocks.com/s/262343

Consider test case:
10 7
0 2
1 8
1 4
2 8
2 6
3 5
6 9

3 sets {0,1,2,4,6,8,9} {3,5} {7}
So answer should be 23. But your code is giving 6.

In one of the graphs webinar of Prateek Bhaiya, he has discussed about a problem named “Journey to the Moon” which is similar to this problem. Refer that video and then try to solve this.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.