Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 1937 ms):
import sys, math, random; from heapq import heapify, heappop, heappush;
from collections import defaultdict, Counter, deque; from bisect import bisect_left, bisect_right
def IL(): return list(map(int, sys.stdin.readline().strip().split()))
def S(): return sys.stdin.readline().strip()

def solve():
    n = IL()[0]
    adj = [[] for _ in range(n + 1)]
    degree = [0] * (n + 1)
    ind = [0] * (n + 1)
    for _ in range(n - 1):
        x, y = IL()
        adj[x].append(y)
        adj[y].append(x)
        degree[x] += 1
        degree[y] += 1
        ind[x] += 1
        ind[y] += 1

    que = deque()
    for node in range(1, n + 1):
        if degree[node] == 1:
            que.append(node)
    val = [-1] * (n + 1)
    level = 0
    while que:
        for _ in range(len(que)):
            node = que.popleft()
            val[node] = level
            for nei in adj[node]:
                ind[nei] -= 1
                if ind[nei] == 1:
                    que.append(nei)
            level += 1
    
    arr = []
    for node in range(1, n + 1):
        arr.append((degree[node], -val[node], node))
    arr.sort()

    ans = 1
    deg, _, first = arr.pop()
    ans += deg - 1
    for nei in adj[first]:
        degree[nei] -= 1
    arr = []
    for node in range(1, n + 1):
        if node != first:
            arr.append((degree[node], -val[node], node))
    arr.sort()
    deg, _, _ = arr.pop()
    ans += deg - 1
    print(ans)

    


    

tt = 1
tt = IL()[0]
for _ in range(tt):
    solve()

Solution 2 (runtime: 406 ms):
"""
For God so loved the world that he gave his one and only Son, 
that whoever believes in him shall not perish but have eternal life.
JOHN 3:16 
"""
import random
from math import *
from functools import lru_cache
from itertools import accumulate
from fractions import Fraction
from bisect import bisect_left as bl
from bisect import bisect_right as br
from operator import itemgetter
from collections import Counter,deque,defaultdict
from heapq import heappop as hpop, heappush as hpush
# constants
p = 10**9 + 7 
ANS = ['NO','YES']
import sys
input = sys.stdin.readline
output = sys.stdout.write
#sys.setrecursionlimit(110000)
# #For large input
# input = sys.stdin.read
# data = input().split()
# Shortcut for input()
def I(): return int(input())
def S(): return input().strip()
def F(): return float(input())
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LS(): return list(map(str, input().split()))
def LF(): return list(map(float , input().split()))
# Shortcut for output()
res = []
def OI(res): output("\n".join(res))
def OL(res): output("\n".join(" ".join(i) for i in res))






t = I()
for _ in range(t):
    n = I()
    deg = [0]*(n+1)
    g = [[] for _ in range(n+1)]
    for _ in range(n-1):
        u,v = MI()
        deg[u] += 1
        deg[v]+=1
        g[u].append(v)
        g[v].append(u)
    m = max(deg)
    if deg.count(m) >= 3:
        print(2*m-1)
        continue
    ans = m
    node = deg.index(m)
    deg[node] = 0
    for child in g[node]:deg[child]-=1
    ans += (max(deg) - 1)
    print(ans)