Religious People why do i get wrong answer for every testcase?

Hello @khushipriya681,

Your code is failing for the testcases like:
1
3 2 5 1
1 3
3 2
Expected output:
7 i.e two roads and one temple.
Your Output:
11 i.e. 2 temples and one road.

This is happening because you are iterating starting from first city 1 then 2 till n.
Which is causing unnecessary construction of temples.
Solution:
Rather follow BFS approach.

Hope, this would help.
Give a like, if you are satisfied.