Under construction

Still being built. Things may change

Dog Snacks

Time limit3sMemory limit512 MB

Summary
Given a tree, find the smallest k so Badugi can eat every snack by always going to the nearest one within k meters, then return to intersection 1.
Level

Medium7 of 10

Topics
Tree, Binary search, DFS
Solved
No attempts yet

Problem

Gildong is playing with his dog, Badugi. They are at a park that has nn intersections and n−1n-1 bidirectional roads. Each road is 11 meter long and connects two intersections. The intersections are numbered from 11 to nn, and for every aa and bb (1≤a,b≤n1\le a,b\le n), Badugi can get from intersection aa to intersection bb using some set of roads.

Gildong has put one snack at every intersection of the park. He gives Badugi a mission to eat all of the snacks. Badugi starts at intersection 11 and moves by the following rules:

  • Badugi looks for the snacks closest to him. Distance is the length of the shortest path from his current location to the intersection with the snack. His sense of smell reaches only kk meters, so he can find only snacks at distance kk or less. If he finds no such snack, he fails the mission.
  • Among the snacks he can smell from his current location, he picks the one that minimizes the distance he must travel. If several snacks tie, he picks any one of them.
  • He repeats this until he has eaten all nn snacks. Then he must find intersection 11 again, and that intersection must be at distance kk or less from the last snack he ate. If he finds it, he completes the mission. Otherwise, he fails.

Gildong does not know kk. Find the minimum value of kk that lets Badugi complete the mission when he moves optimally.

Input

The first line contains the number of test cases tt (1≤t≤1041\le t\le 10^4).

The first line of each test case contains one integer nn (2≤n≤2⋅1052\le n\le 2\cdot 10^5), the number of intersections.

Each of the next n−1n-1 lines contains two integers uu and vv (1≤u,v≤n1\le u,v\le n, u≠vu\ne v), meaning there is a road between intersections uu and vv. All roads are bidirectional and distinct.

It is guaranteed that for each test case, for every aa and bb (1≤a,b≤n1\le a,b\le n), it is possible to get from intersection aa to intersection bb using roads. The sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

Output

For each test case, print one integer: the minimum value of kk such that Badugi can complete the mission.

Hint

In the first case, Badugi can complete the mission with k=2k=2 by moving as follows:

  1. Badugi starts at intersection 11. The closest snack is at intersection 11, so he eats it.
  2. The next closest snacks are at intersection 22 and intersection 33. Suppose he picks intersection 22. He moves 11 meter to intersection 22 and eats the snack.
  3. The only remaining snack is at intersection 33. He moves along 22 roads to get there.
  4. After eating it, he returns to intersection 11, which is 11 meter away. He completes the mission.

In the second case, the only possible sequence of moves is 1→2→3→4→11 \to 2 \to 3 \to 4 \to 1. The distance from intersection 44 to intersection 11 is 33, so kk must be at least 33.

In the third case, Badugi can move as 1→5→6→7→8→2→3→4→11 \to 5 \to 6 \to 7 \to 8 \to 2 \to 3 \to 4 \to 1. It can be shown that this is the only sequence that completes the mission with k=3k=3.

Examples1

  1. Example 1

    Input
    3
    3
    1 2
    1 3
    4
    1 2
    2 3
    3 4
    8
    1 2
    2 3
    3 4
    1 5
    5 6
    6 7
    5 8
    
    Expected output
    2
    3
    3