Education Nightmare
Time limit10sMemory limit512 MB
On a tree of n rooms, find the minimum worst-case time to reach the class from room s, where entering room m reveals the class location.
Problem
You are having a nightmare. In the dream, you have finished your education and are now starting to teach others. Today is your first day! You enter the school building (a little late) and, of course, you have forgotten which room your class is in.
The building has rooms, numbered . Some pairs of rooms are connected by a passage. You may move between connected rooms, and each transfer takes exactly one second. There are exactly passages, and every room can be reached given enough time. In other words, the rooms and passages form a tree.
You start in room . If you enter the correct room, you recognize it immediately and end your quest. Still, searching every room could take a very long time. Fortunately, there is one more trick: room holds a complete timetable of all classes. If you ever enter room , you immediately learn where the class is and may go there at once by the shortest route. Be advised, though: room does not provide printing, faxing, scanning, or photocopy services. Do not ask them about it.
Find the minimal time needed to find your class in the worst case, that is, the minimal number for which some strategy guarantees finding the correct room within seconds.
Input
The first line contains the number of test cases (). Each test case starts with a line containing three integers , , and (, ). Then lines follow, each containing two integers (, ), denoting a passage between rooms and . It is guaranteed that every room can be reached from every other one.
The class may be in room or in room . It is also possible to start in room .
The total number of rooms in all test cases does not exceed .
Output
For each test case, output a single integer: the total number of seconds needed to reach the classroom under the best possible strategy.