Under construction

Still being built. Things may change

Education Nightmare

Time limit10sMemory limit512 MB

Summary
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.
Level

Medium6 of 10

Topics
Tree, DFS, Greedy
Solved
No attempts yet

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 nn rooms, numbered 1,2,…,n1, 2, \ldots, n. 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 n−1n - 1 passages, and every room can be reached given enough time. In other words, the rooms and passages form a tree.

You start in room ss. 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 mm holds a complete timetable of all classes. If you ever enter room mm, you immediately learn where the class is and may go there at once by the shortest route. Be advised, though: room mm does not provide printing, faxing, scanning, or photocopy services. Do not ask them about it.

Find the minimal time tt needed to find your class in the worst case, that is, the minimal number tt for which some strategy guarantees finding the correct room within tt seconds.

Input

The first line contains the number of test cases zz (1≤z≤1091 \leq z \leq 10^9). Each test case starts with a line containing three integers nn, ss, and mm (1≤n≤200 0001 \leq n \leq 200\,000, 1≤s,m≤n1 \leq s, m \leq n). Then n−1n - 1 lines follow, each containing two integers a,ba, b (1≤a,b≤n1 \leq a, b \leq n, a≠ba \neq b), denoting a passage between rooms aa and bb. It is guaranteed that every room can be reached from every other one.

The class may be in room ss or in room mm. It is also possible to start in room mm.

The total number of rooms in all test cases does not exceed 10710^7.

Output

For each test case, output a single integer: the total number of seconds needed to reach the classroom under the best possible strategy.

Examples1

  1. Example 1

    Input
    1
    4 2 3
    1 2
    1 3
    1 4
    
    Expected output
    4