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

Solution 1 (runtime: 1281 ms):
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <cstring>
#include <iterator>
#define ll long long
#define MOD 1000000007 
using namespace std;
vector<int>sieve(int n){
    vector<int>p;
    vector<int>isprime(n+1, 1);
    isprime[0] = isprime[1] = 0;
    for (int i = 2; i<=n; i++){
        if (isprime[i]){
            for (int j = 2*i; j<=n; j+=i){
                isprime[j] = 0;
            }
        }
    }
    for (int i = 1; i<=n; i++){
        if (isprime[i]){
            p.push_back(i);
        }
    }
    return p;
}
//---------------------------------------------------------------------------
// bool check(int k, vector<int>d, vector<int>a){

// }

void solve(){
    int n;
    cin>>n;
    vector<vector<int>>adj(n);
    map<int,int>ed;
    map<pair<int,int>,int>ed1;
    for (int i = 0; i<n-1; i++){
        int u, v;
        cin>>u>>v;
        u--;
        v--;
        ed1[make_pair(u,v)] = 1;
        ed1[make_pair(v,u)] = 1;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    if (n <= 4){
        cout<<n-2<<endl;
        return;
    }
    for (int i = 0; i<n; i++){
        ed[i] = adj[i].size();
    }
    int ans = 0;
    for (int i = 0; i<n; i++){
        for (int j:adj[i]){
            ans = max(ans, ed[i] + ed[j] - 2);
        }
    }
    vector<pair<int,int>>ed2;
    for (auto i:ed){
        ed2.push_back(make_pair(i.second, i.first));
    }
    sort(ed2.begin(), ed2.end());
    for (int i = n-1; i>=0; i--){
        for (int j = n-1; j>=0; j--){
            if (j != i && ed1.find(make_pair(ed2[i].second, ed2[j].second)) == ed1.end()){
                ans = max(ans, ed[ed2[i].second] + ed[ed2[j].second]-1);
                break;
            }
        }
    }
    cout<<ans<<endl;


    
}

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

Solution 2 (runtime: 1139 ms):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;

#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pb push_back
#define pii pair<ll, ll>
#define vpi vector<pii>
#define psi pair<string, ll>
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) x.begin(), x.end()
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define loopr(i, a, b) for (ll i = a; i >= b; i--)
#define read(a, n) loop(i, 0, n) cin >> a[i]
#define print(a, n)                   \
    rep(i, 0, n) cout << a[i] << " "; \
    cout << "\n"
#define ff first
#define ss second
#define yes cout << "YES" \
                 << "\n"
#define no cout << "NO" \
                << "\n"
#define lld long long double
#define ull unsigned long long
#define si set<ll>
#define sc set<char>
#define sti stack<ll>
#define stc stack<char>
#define msi map<string, ll>
#define mis map<ll, string>
#define mii map<ll, ll>
#define in insert
#define M 1000000007
#define bpc(x) __builtin_popcountll(x)
int ceil(int a, int b)
{
    if (a % b)
        return (a / b) + 1;
    else
        return a / b;
}

/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/

void solve()
{
    ll n;
    cin >> n;
    vvi g(n);
    vi deg(n, 0);
    loop(i, 0, n - 1)
    {
        ll u, v;
        cin >> u >> v;
        g[--u].pb(--v), g[v].pb(u);
        deg[u]++, deg[v]++;
    }
    set<pii> st;
    loop(i, 0, n)
    {
        st.insert({deg[i], i});
    }
    ll ans = LONG_LONG_MIN;
    loop(i, 0, n)
    {
        st.erase({deg[i], i});
        for (ll &it : g[i])
        {
            st.erase({deg[it], it});
            st.insert({deg[it] - 1, it});
        }
        ans = max(ans, deg[i] + (*st.rbegin()).first - 1);
        for (ll &it : g[i])
        {
            st.erase({deg[it] - 1, it});
            st.insert({deg[it], it});
        }
    }
    cout << ans << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}