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

Solution 1 (runtime: 717 ms):
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#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 se second 
#define fi first
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define pii pair<int,int>
#define pli pair<LL,int>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))
inline int Ls(int p){return p<<1;}
inline int Rs(int p){return p<<1|1;}
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const int N=2e5+10,M=2*N;
int h[N],ne[M],e[M];
int deg[N];
class Graph{
public:
    int n; int idx=0;
    void add(int a,int b){
        e[++idx]=b,ne[idx]=h[a],h[a]=idx;
    }
    void clear(){
        rep(i,0,n+1) h[i]=deg[i]=0; 
    }
};
int ddo(int n)
{
    multiset<int>st;
    int res=-1e9;
    rep(i,1,n+1) st.insert(deg[i]);
    rep(u,1,n+1){
        int tot=deg[u]-1;
        st.erase(st.find(deg[u]));
        for(int i=h[u];i;i=ne[i]){
            int v=e[i];
            st.erase(st.find(deg[v]));
        }
        for(int i=h[u];i;i=ne[i]){
            int v=e[i];
            st.insert(deg[v]-1);
        }
        tot+=*(st.rbegin())-1; res=max(res,tot);
        for(int i=h[u];i;i=ne[i]){
            int v=e[i];
            st.erase(st.find(deg[v]-1));
        }
        for(int i=h[u];i;i=ne[i]){
            int v=e[i];
            st.insert(deg[v]);
        }
        st.insert(deg[u]);
    }
    return res;
}
inline void Solve()
{
    Graph g;
    cin>>g.n;
    int n=g.n;
    rep(i,0,n-1){
        int a,b;
        cin>>a>>b;
        g.add(a,b); g.add(b,a);
        deg[a]+=1; deg[b]+=1;
    }
    int ans=1;
    ans+=ddo(g.n);
    cout<<ans<<endl;
    g.clear();
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("test_0.in","r",stdin);
        freopen("test_0.out","w",stdout);
    #endif
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int _=1;
    cin>>_;
    while(_--){
        Solve();
    }
    return 0;
}


Solution 2 (runtime: 359 ms):
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#include "debugging.h"
#endif

using namespace std;

#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lp(i,n) for(int i = 0; i < n; i++)
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define invrep(i,a,b) for(ll i = a; i >= b; i--)
#define pb(a) push_back(a)
#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define line cout<<"\n"
#define EPS 0.0000000001

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ii> vii;

ll sum_n(ll n){return (ll)n*(n+1)/2;}
ll c_kn(ll k, ll n){if(n <= 0)return 0;ll res = 1;if(k>n-k)k=n-k;for(ll i=0;i<k;++i){res*=(n-i);res/=(i+1);}return res;}
int cpd(long double a,long double b){return fabs(a-b)<=EPS?0:a-b>0?1:2;}
bool ison(ll n, ll k){return (n & (1LL << k));}

const int INF = INT_MAX;
const int N = 3e5+2;
ll inf_long = (ll)1e18+1LL;
ll mod = 998244353;
ll modi = 1e9+80;

void solve(int _){
    ll n;
    cin >> n;
    ll a, b;
    vector<ii> d(n);
    lp(i,n)d[i].ff = 0;
    set<ii> edges;
    lp(i,n-1){
        cin >> a >> b;
        a--; b--;
        d[a].ff++;
        d[b].ff++;
        if(a > b)swap(a, b);
        edges.insert(mp(a, b));
    }
    lp(i,n)d[i].ss = i;
    ll ans = 0;

    for(auto e:edges)ans = max(ans, d[e.ff].ff + d[e.ss].ff - 2LL);
    sort(all(d));
    reverse(all(d));
    for(ll i = 0; i < n;i++){
        lp(j, n){
            if(i==j)continue;
            auto x = d[j];
            if(edges.find(mp(min(d[i].ss, x.ss), max(d[i].ss, x.ss))) != edges.end())continue;
            ans = max(ans, x.ff+d[i].ff - 1LL);
            break;
        }
    }
    cout << ans << endl;
}


int main(){
    fast;
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int t = 1;
    cin>>t;
    lp(i,t)solve(i+1);
    return 0;
}