Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 561 ms):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REV(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;




void solve()
{
    int n; cin >> n;

    vector<vector<int>> adj(n+1, vector<int>());
    FOR(i, 1, n - 1){
        int u,v; cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }

    multiset<int> s;
    FOR(i, 1, n){
        s.insert(sz(adj[i]));
    }

    int answer = 0;
    FOR(x, 1, n){
        s.erase(s.find(sz(adj[x])));
        for(int v : adj[x]){
            answer = max(answer, sz(adj[x]) + sz(adj[v]) - 2);
            s.erase(s.find(sz(adj[v])));
        }

        if(s.size() > 0)
            answer = max(answer, sz(adj[x]) + (*(--(s.end()))) - 1);

        for(int v : adj[x]){
            s.insert(sz(adj[v]));
        }
        s.insert(sz(adj[x]));
    }

    cout << answer << "\n";

    return;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; cin >> t;
    while(t--) solve();
    return 0;
}


Solution 2 (runtime: 311 ms):
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pii pair<int,int>
#define ll long long int
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define pb push_back
#define MOD 1000000007
#define INF 1000000007
using namespace std;

int n , ma1[SIZE] , deg[SIZE] , ans , ma2[SIZE];
vector<int> g[SIZE] , child[SIZE];

void init() {
    ans=0;
    for (int i = 1; i <= n; i++) {
        g[i].clear();
        child[i].clear();
        ma1[i] = ma2[i] = deg[i] = 0;
    }
}

void dfs(int here ,int pa) {
    multiset<int> st1 , st2;
    for (auto t : g[here]) {
        if (t == pa) continue;
        ans = max(ans , deg[here]+deg[t]-2);
        dfs(t , here);
        ma2[here] = max(ma2[here] , ma1[t]);
        st1.insert(-ma1[t]);
        st2.insert(ma2[t]);
    }
    if (st2.size() > 0) ans = max(ans , deg[here]+(*st2.rbegin())-1);
    if (st1.size() >= 2) {
        int ret=0;
        auto it = st1.begin();
        ret += -(*it);
        it++;
        ret += -(*it); ret--;
        ans = max(ans , ret);
    }
    ma1[here] = max(deg[here] , ma2[here]);
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n-1; i++) {
        int u , v; cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
        deg[u]++; deg[v]++;
    }

    if (n == 2) {
        cout << "0\n";
        return;
    }

    dfs(1 , -1);

    cout << ans << "\n";
}

int main() {
    FastIO;

    int t; cin >> t;
    while(t--) {
        init();
        solve();
    }

    return 0;
}