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

Solution 1 (runtime: 249 ms):
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;
typedef long long ll;

void solve()
{
    int n;
    cin>>n;
    vector<vector<int>> g(n+1);
    vector<int> ind(n+1,0);

    for(int i=0,u,v;i<n-1;++i)
    {
        cin>>u>>v;
        g[u].push_back(v),g[v].push_back(u);
        ind[u]++,ind[v]++;
    }

    int tag=0,cnt=0;
    for(int i=1;i<=n;++i)
        if(ind[i]>ind[tag]) tag=i,cnt=1;
        else if(ind[i]==ind[tag]) cnt++;

    if(cnt>=3) cout<<ind[tag]*2-1<<'\n';
    else if(cnt==2)
    {
        bool is = false;
        for(int i=0;i<(int)g[tag].size();++i)
            if(ind[g[tag][i]]==ind[tag]) is = true;
        if(is) cout<< ind[tag]*2-2<< '\n';
        else cout<< ind[tag]*2-1<< '\n';
    }
    else
    {
        for(int i=0;i<(int)g[tag].size();++i) ind[g[tag][i]]--;
        int mx=0;
        for(int i=1;i<=n;++i)
            if(i!=tag) mx = max(mx,ind[i]);
        cout<< ind[tag]+mx-1<< '\n';
    }

}

int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //fflush(stdout);

    int t;
    cin>>t;
    while(t--) solve();

    return 0;
}


Solution 2 (runtime: 249 ms):
#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
 
typedef long long int ll;
typedef unsigned long long int ull;
 
//Templates
 //defining template when all elements are distinct 
// template <class T> 
// using indexed_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 //defining template when duplicate elements are also used
// template <class T> 
// using indexed_set = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define per(i,a,b) for(int i = (a); i > (b); i--)
#define repl(i,a,b) for(ll i = (a); i < (b); i++)
#define perl(i,a,b) for(ll i = (a); i > (b); i--)
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
 
const ll Mod=998244353;
const ll mod=1000000007;
const ll MAX_n=1010;
const ll p_size=4*1e6+100;
 
ll power(ll x, ll y, ll m){
    ll res = 1;
    while (y > 0){
        if (y & (ll)1){
            res = (res * x)%m;
        }    
        y = (y >> (ll)1)%m;
        x = (x * x)%m;
    }
    return res%m;
}
 
ll gcd(ll a, ll b){
    if (b == 0ll){
        return a;
    }
    return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    ll g=gcd(a,b);
    return (a/g)*b;
}
 
void SieveOfEratosthenes(int n,bool prime[]){
    memset(prime, true, n);
 
    for (int p = 2; p * p <= n-1; p++){
        if (prime[p] == true){
            for (int i = p * p; i <= n-1; i += p){
                prime[i] = false;
            }
        }
    }
 
}
 
// ll modInverse(ll a, ll b){           // (a/b) % p = ((a%p) * (pow(b,p-2) % p))%p
//     ll x=power(a,b)%Mod;
//     ll y=power(x,Mod-2)%Mod;
//     return y;
// }
 
 
// bool sortby(const pair<int,int> &a,const pair<int,int> &b){
//     if(a.first==b.first){
//         return a.second < b.second;
//     } 
//     return a.first < b.first;
    
// }
 
 
// class CompareDist
// {
// public:
//     bool operator()(pair<int,int> n1,pair<int,int> n2) {
//         if(n1.first==n2.first){
//             return n1.second>n2.second;
//         }
//         return n1.first<n2.first;
//     }
// };
 
// bool sortby(pair<int,int> a, pair<int,int> b){
//     if(a.first==b.first){
//         return a.second < b.second;
//     }
//     return a.first > b.first;
// }
 
// class compare{
//     bool operator()(pair<int,int> &a, pair<int,int> &b){
//         return a.second<b.second;
//     }
// }
 
// ll fact[MAX_n];
// ll invfact[MAX_n];
 
// ll nCr(ll n,ll r, ll m){
//     ll ans=fact[n];
//     ans*=invfact[r];
//     ans%=m;
//     ans*=invfact[n-r];
//     ans%=m;
//     return ans;
// }
 

void reduce_deg(vector<int> &dp, set<int> &s, int val, int n){
    if(val!=0 && val!=n && dp[val-1]>0){
        dp[val-1]--;
        if(dp[val-1]==0) s.erase(val-1);
        if(dp[val-1]==1) s.insert(val-1);
    }
    if(val!=n-1 && val!=(2*n)-1 && dp[val+1]>0){
        dp[val+1]--;
        if(dp[val+1]==0) s.erase(val+1);
        if(dp[val+1]==1) s.insert(val+1);
    }
    if(val<n && dp[val+n]>0){
        dp[val+n]--;
        if(dp[val+n]==0) s.erase(val+n);
        if(dp[val+n]==1) s.insert(val+n);
    }
    if(val>=n && dp[val-n]>0){
        dp[val-n]--;
        if(dp[val-n]==0) s.erase(val-n);
        if(dp[val-n]==1) s.insert(val-n);
    }
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
 
    
    // manipulated_seive(xn+10);
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
 
    // bool prime[MAX_n];
    // SieveOfEratosthenes(MAX_n,prime);
    // prime[1]=false;
    // indexed_set s;
    // s.insert(2);
    // auto x = s.find_by_order(index);
    // int y=s.order_of_key(value);
    int t; cin>>t;
    while(t-->0){
        int n;
        cin>>n;
        vector<int> arr[n];
        rep(i,0,n-1){
            int a,b; cin>>a>>b;
            arr[--a].pb(--b);
            arr[b].pb(a);
        }
        if(n==2){cout<<"0\n"; continue;}

        vector<pair<int,int>> v;
        rep(i,0,n){
            v.pb({arr[i].size(), i});
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());
        // rep(i,0,n){
        //     cout<<v[i].first<<" "<<v[i].second<<"\n";
        // }
        int ans = 0;
        if(v[0].first==v[1].first){
            if(v[1].first == v[2].first){
                ans = 2*v[0].first - 1;
            }else{
                bool ok=true;
                rep(i,0,v[0].first){
                    if(arr[v[0].second][i] == v[1].second){
                        ok=false; break;
                    }
                }
                if(ok){
                    ans = 2*v[0].first - 1;
                }else{
                    ans = 2*v[0].first - 2;
                }
            }
        }else{
            bool ok=true;
            rep(i,0,v[0].first){
                if(arr[v[0].second][i] == v[1].second){
                    ok=false; break;
                }
            }
            if(ok){
                ans = v[0].first + v[1].first - 1;
            }else{
                bool ok2 = true;
                rep(i,0,v[0].first){
                    if(arr[v[0].second][i] == v[2].second){
                        ok2=false; break;
                    }
                }
                if(ok2){
                    ans = max(v[0].first + v[1].first - 2, v[0].first + v[2].first - 1);
                }else{
                    ans = max(v[0].first + v[1].first - 2, v[1].first + v[2].first - 1);
                    // cout<<ans<<"\n";
                    if(n>3){
                        int temp[n]={0};
                        rep(i,0,v[0].first){temp[arr[v[0].second][i]] = 1;}
                        rep(i,3,n){
                            if(temp[v[i].second] == 0){
                                // cout<<i<<"\n";
                                ans = max(v[0].first + v[i].first - 1, ans);
                                break;
                            }
                        }
                    }
                }
            }
        }
        cout<<ans<<"\n";

        
        

    }
 
    
    return 0;
}