Problem: 2063C - Remove Exactly Two
Language: python

Solution 1 (runtime: 687 ms):
import io
import os
import sys


inf = 10 ** 18
mod = 10 ** 9 + 7

ONLINE_JUDGE = __debug__

if ONLINE_JUDGE:
    R = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline


    def _input() -> str:
        return R().decode()
else:
    R = sys.stdin.readline


    def _input() -> str:
        return R()


def get_int() -> int:
    return int(_input())


def get_str() -> str:
    return _input().strip()


def get_int_list() -> list[int]:
    return list(map(int, _input().strip().split()))


def init_matrix(n_, m_, value_=0) -> list[list]:
    return [[value_] * m_ for _ in range(n_)]


def solve() -> None:
    n = get_int()

    g = init_matrix(n, 0)
    cnt = [0] * n
    pairs = set()
    for i in range(n - 1):
        u, v = get_int_list()
        u -= 1
        v -= 1
        g[v].append(u)
        g[u].append(v)
        cnt[u] += 1
        cnt[v] += 1
        pairs.add((u, v))
        pairs.add((v, u))

    order = sorted(
        range(len(cnt)),
        key=lambda x: cnt[x],
        reverse=True,
    )
    u, v = order[:2]
    h = []
    for i in range(n):
        if cnt[i] == cnt[v]:
            h.append(i)

    if cnt[u] != cnt[v]:
        for w in h:
            if (u, w) not in pairs:
                print(cnt[u] + cnt[v] - 1)
                return

        print(cnt[u] + cnt[v] - 2)
    else:
        for i in range(len(h)):
            for j in range(i + 1, len(h)):
                if (h[i], h[j]) not in pairs:
                    print(cnt[u] + cnt[v] - 1)
                    return

        print(cnt[u] + cnt[v] - 2)


for _ in range(get_int()):
    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)