Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>

using namespace std;
//#define endl "\n"
#define speeed ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define ld long double

#define ll long long
#define sz(s) (int)size(s)
//#define int ll

void File() {
#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
}

ll tc = 1;
#define OO LLONG_MAX
#define ONBit(n, k) (n|(1ll<<k))
#define OFFBit(n, k) (n&(~(1ll<<k)))
#define IsON(n, k) ((n>>k)&1ll)
#define twopower(n) (1ll<<n)
ll MOD1 = 1e9 + 7;

bool valid(long long i, long long j, long long n, long long m) { return i >= 0 && i < n && j >= 0 && j < m; }

long long mul(long long x, long long y, const long long MOD = MOD1) { return ((x % MOD) * (y % MOD)) % MOD; }

long long add_MOD(long long x, long long y, long long MOD = MOD1) {
    return (((x % MOD) + (y % MOD)) % MOD + MOD) % MOD;
}

long long sub(long long a, long long b, long long m = MOD1) {
    return ((a % m) - (b % m) + m) % m;
}

ll SafeMul(ll a, ll b, ll m) {
    if (b == 0)return 0;
    ll res = SafeMul(a, b / 2, m);
    res = add_MOD(res, res, m);
    if (b & 1)return add_MOD(res, a, m);
    else return res;
}

ll fast_power(ll a, ll b, ll m) {
    if (b == 0)return 1;
    ll res = fast_power(a, b / 2, m);
    res = (res * res) % m;
    if (b & 1)return (res * a) % m;
    else return res;
}

/*ll fast_power(ll x, ll n, ll m) {
    ll result = 1;
    while (n > 0) {
        if (n % 2 == 1) {
            result = SafeMul(result, x, m);
        }
        x = SafeMul(x, x, m);
        n = n / 2;
    }
    return result;
}*/

int log2_floor(long long i) { return i ? __builtin_clzll(1) - __builtin_clzll(i) : 0; }

int power_of_2(int n) { return __builtin_popcountll(n) == 1; }

bool line_checking(int a1, int b1, int a2, int b2, int a3, int b3) {
    return (b2 - b1) * (a2 - a3) == (b2 - b3) * (a2 - a1);
}

pair<int, int> rotate(int i, int j, int n) { return make_pair(j, n - 1 - i); }

int comp_double(double a, double b, double error = 1e-7) {
    if (fabs(a - b) <= error)return 0;
    if (a < b)return 1;
    return -1;
}

int round(int x, int y) {
    if (y < 0)x *= -1, y *= -1;
    if (x < 0) {
        return (2 * x - y + 1) / (2 * y);
    }
    return (2 * x + y - 1) / (2 * y);
}

int floor(int x, int y) {
    return x / y;
}

int ceil(int x, int y) {
    return (x + y - 1) / y;
}

/*int round(int m,int x){return round(m/x)*x;}*/


int dx[]{0, 0, 1, -1, -1, -1, 1, 1};
int dy[]{1, -1, 0, 0, 1, -1, -1, 1};


ll egcd(ll a, ll b, ll &x, ll &y) { /// ax + by = gcd(a,b)
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    ll g = egcd(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
}

ll modInverse(ll a, ll m) { /// (a/b)%m = ((a%m)*(modInverse(b)%m))%m
    ll x, y, g;
    g = egcd(a, m, x, y);
    if (g > 1)
        return -1;
    return (x + m) % m;
}

void print(vector<int> &v) {
    for (auto item: v) {
        cout << item << " ";
    }
    cout << endl;
}

struct pair_hash {
    template<class T1, class T2>
    std::size_t operator()(const std::pair<T1, T2> &p) const {
        auto h1 = std::hash<T1>{}(p.first);
        auto h2 = std::hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};


int F(vector<set<int>> &adj, vector<bool> &vis) {
    int mx = 0, node = 1;
    for (int i = 1; i < adj.size(); ++i) {
        if (mx <= adj[i].size() and !vis[i]) {
            mx = adj[i].size();
            node = i;
        }
    }
    return node;
}

void dfs(int node, vector<bool> &vis, vector<set<int>> &adj) {
    vis[node] = true;
    for (auto child: adj[node]) {
        if (!vis[child]) {
            dfs(child, vis, adj);
        }
    }
}


void main_() {
    int n;
    cin >> n;
    vector<set<int>> adj(n + 1);
    for (int u, v, i = 0; i < n - 1; ++i) {
        cin >> u >> v;
        adj[u].insert(v);
        adj[v].insert(u);
    }
    int ans = 0;
    for (int node = 1; node <= n; ++node) {
        int curr = 0;
        for (auto child: adj[node]) {
            curr = max<int>(adj[child].size(), curr);
        }
        ans = max<int>(ans, curr + adj[node].size() - 2);
    }
    vector<bool> vis(n + 1, false);
    int node = F(adj, vis);
    for (auto child: adj[node]) {
        adj[child].erase(node);
    }
    adj[node].clear();
    vis[node] = 1;
    node = F(adj, vis);
    for (auto child: adj[node]) {
        adj[child].erase(node);
    }
    adj[node].clear();
    vis[node] = 1;
    int curr = 0;
    for (int i = 1; i <= n; ++i) {
        if (!vis[i]) {
            ++curr;
            dfs(i, vis, adj);
        }
    }
    ans= max(ans,curr);
    cout << ans;
}

void preprocessing() {

}


signed main() {
    speeed
    File();
//    freopen("business.in", "r", stdin);
    int t = 1;
    preprocessing();
    cin >> t;
    for (int i = 1; i <= t; ++i) {
//        cout << "Case #"<<i<<": " ;
        main_();
        ++tc;
        if (i != t)
            cout << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

using namespace std;
//#define endl "\n"
#define speeed ios_base::sync_with_stdio(false); cin.tie(0),cout.tie(0);
#define ld long double

#define ll long long
#define sz(s) (int)size(s)
//#define int ll

void File() {
#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
}

ll tc = 1;
#define OO LLONG_MAX
#define ONBit(n, k) (n|(1ll<<k))
#define OFFBit(n, k) (n&(~(1ll<<k)))
#define IsON(n, k) ((n>>k)&1ll)
#define twopower(n) (1ll<<n)
ll MOD1 = 1e9 + 7;

bool valid(long long i, long long j, long long n, long long m) { return i >= 0 && i < n && j >= 0 && j < m; }

long long mul(long long x, long long y, const long long MOD = MOD1) { return ((x % MOD) * (y % MOD)) % MOD; }

long long add_MOD(long long x, long long y, long long MOD = MOD1) {
    return (((x % MOD) + (y % MOD)) % MOD + MOD) % MOD;
}

long long sub(long long a, long long b, long long m = MOD1) {
    return ((a % m) - (b % m) + m) % m;
}

ll SafeMul(ll a, ll b, ll m) {
    if (b == 0)return 0;
    ll res = SafeMul(a, b / 2, m);
    res = add_MOD(res, res, m);
    if (b & 1)return add_MOD(res, a, m);
    else return res;
}

ll fast_power(ll a, ll b, ll m) {
    if (b == 0)return 1;
    ll res = fast_power(a, b / 2, m);
    res = (res * res) % m;
    if (b & 1)return (res * a) % m;
    else return res;
}

/*ll fast_power(ll x, ll n, ll m) {
    ll result = 1;
    while (n > 0) {
        if (n % 2 == 1) {
            result = SafeMul(result, x, m);
        }
        x = SafeMul(x, x, m);
        n = n / 2;
    }
    return result;
}*/

int log2_floor(long long i) { return i ? __builtin_clzll(1) - __builtin_clzll(i) : 0; }

int power_of_2(int n) { return __builtin_popcountll(n) == 1; }

bool line_checking(int a1, int b1, int a2, int b2, int a3, int b3) {
    return (b2 - b1) * (a2 - a3) == (b2 - b3) * (a2 - a1);
}

pair<int, int> rotate(int i, int j, int n) { return make_pair(j, n - 1 - i); }

int comp_double(double a, double b, double error = 1e-7) {
    if (fabs(a - b) <= error)return 0;
    if (a < b)return 1;
    return -1;
}

int round(int x, int y) {
    if (y < 0)x *= -1, y *= -1;
    if (x < 0) {
        return (2 * x - y + 1) / (2 * y);
    }
    return (2 * x + y - 1) / (2 * y);
}

int floor(int x, int y) {
    return x / y;
}

int ceil(int x, int y) {
    return (x + y - 1) / y;
}

/*int round(int m,int x){return round(m/x)*x;}*/


int dx[]{0, 0, 1, -1, -1, -1, 1, 1};
int dy[]{1, -1, 0, 0, 1, -1, -1, 1};


ll egcd(ll a, ll b, ll &x, ll &y) { /// ax + by = gcd(a,b)
    if (!b) {
        x = 1;
        y = 0;
        return a;
    }
    ll g = egcd(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
}

ll modInverse(ll a, ll m) { /// (a/b)%m = ((a%m)*(modInverse(b)%m))%m
    ll x, y, g;
    g = egcd(a, m, x, y);
    if (g > 1)
        return -1;
    return (x + m) % m;
}

void print(vector<int> &v) {
    for (auto item: v) {
        cout << item << " ";
    }
    cout << endl;
}

struct pair_hash {
    template<class T1, class T2>
    std::size_t operator()(const std::pair<T1, T2> &p) const {
        auto h1 = std::hash<T1>{}(p.first);
        auto h2 = std::hash<T2>{}(p.second);

        // Mainly for demonstration purposes, i.e. works but is overly simple
        // In the real world, use sth. like boost.hash_combine
        return h1 ^ h2;
    }
};


void main_() {
    int n;
    cin >> n;
    vector<set<int>> adj(n + 1);
    vector<int>deg(n+1);
    multiset<pair<int,int>,greater<>>multiset1;
    for (int u, v, i = 0; i < n - 1; ++i) {
        cin >> u >> v;
        adj[u].insert(v);
        adj[v].insert(u);
        deg[u]++;
        deg[v]++;
    }

    for (int i = 1; i <= n ; ++i) {
        multiset1.insert({deg[i],i});
    }

    int ans = 0;
    for (int node = 1; node <= n ; ++node) {
        multiset1.erase({deg[node], node});
        for (auto child:adj[node]) {
            ans = max(ans, deg[node] + deg[child] - 2);
            multiset1.erase({deg[child],child});
        }

        int mx = multiset1.begin()->first;
        ans = max(ans, mx + deg[node] - 1);


        for (auto child:adj[node]) {
            ans = max(ans, deg[node] + deg[child] - 2);
            multiset1.insert({deg[child],child});
        }

        multiset1.insert({deg[node], node});
    }

    cout<<ans;


}

void preprocessing() {

}


signed main() {
    speeed
    File();
//    freopen("business.in", "r", stdin);
    int t = 1;
    preprocessing();
    cin >> t;
    for (int i = 1; i <= t; ++i) {
//        cout << "Case #"<<i<<": " ;
        main_();
        ++tc;
        if (i != t)
            cout << endl;
    }
    return 0;
}