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

Solution 1 (runtime: 421 ms):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
double eps = 1e-12;
#define ln "\n"
#define printVector(a) for(int i=0; i<a.size(); i++){cout<<a[i]<<" ";}cout<<ln;
#define print_array(a,n) for(int i=0; i<n; i++){cout<<a[i]<<" ";}cout<<ln;
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define pb push_back
#define take_vector(a) for(auto &x:a)cin>>x;
#define take_array(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define take_matrix(a,m,n) for(int i=0; i<m; i++){for(int j=0; j<n; j++){cin>>a[i][j];}}
#define print_matrix(a,m,n) for(int i=0; i<m; i++){for(int j=0; j<n; j++){cout<<a[i][j]<<" ";}cout<<ln;}
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a/gcd(a,b))*b
#define count(a,x) count(a.begin(), a.end(),x)
#define sum(a) accumulate(a.begin(), a.end(),0)
#define max_ele(a) *max_element(a.begin(), a.end())
#define min_ele(a) *min_element(a.begin(), a.end())

int32_t main()
{
  fast_cin();

  int t;
  cin >> t;

  while (t--)
  {

    int n;
    cin>>n;
    
    vector<set<int>> graph(n+1);

    for( int i = 1 ; i < n;  i++ ){
        int a,b;
        cin>>a>>b;
        graph[a].insert(b);
        graph[b].insert(a);
    }

    //  1  -  2
    //  2  -  2
    //  3  -  1
    //  4  -  2
    //  5  -  3
    //  6  -  1
    //  7  -  1

    map<int,int> mp;

    for( int i = 1; i <= n ; i++ ){
        mp[ graph[i].size() ]++;
    }

    int ans = 0;

    for( int i = 1; i <= n; i++ ){

        int temp = graph[i].size();
            
        mp[ graph[i].size() ]--;
        if( mp[ graph[i].size() ] == 0 ) mp.erase(graph[i].size() );

        for( int j : graph[i] ){

            mp[ graph[j].size() ]--;
            if( mp[ graph[j].size() ] == 0 ) mp.erase(graph[j].size() );
            mp[ graph[j].size() - 1 ]++;

        }

        ans = max( ans , temp + mp.rbegin()->first - 1 );

        for( int j : graph[i] ){

            mp[ graph[j].size() ]++;
            mp[ graph[j].size() - 1 ]--;
            if( mp[ graph[j].size() - 1 ] == 0 ) mp.erase(graph[j].size() - 1);

        }

        mp[ graph[i].size() ]++;

    }

    cout<<ans<<endl;

  }
  return 0;
}

Solution 2 (runtime: 983 ms):
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<climits> 
#include<cstring>
#include<ctime>
#include<chrono>
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<string>
#include<stack>
#include<set>
#include<utility>
#include<unordered_set>
#include<unordered_map>
#include<vector>





#define mp              make_pair
#define pb              push_back
#define fi              first
#define se              second
#define maxl2(a,b)      ((a>b)?a:b)
#define minl2(a,b)      ((a>b)?b:a)
#define maxl3(a,b,c)    ((a>b)?((a>c)?a:c):((b>c)?b:c))
#define minl3(a,b,c)    ((a<b)?((a<c)?a:c):((b<c)?b:c))
#define mem(a,b)        memset(a,b,sizeof(a))
#define all(x)          x.begin(),x.end()
#define SP(y)           fixed<<setprecision(y)


typedef long long           ll;
typedef long double         ld;

typedef std::vector<int>    VI;
typedef std::vector<ll>     VL;
typedef std::vector<bool>   VB;

typedef std::pair<int,int>  PII;
typedef std::pair<ll,ll>    PLL;
typedef std::pair<int,ll>   PIL;
typedef std::pair<ll,int>   PLI;
typedef std::pair<char,ll>  PCL;
typedef std::pair<ll,char>  PLC;



const                       ll MOD = 1e9+7;
const                       ll N = 2e5+10;
const                       double PI = 3.14159265358979323846;

#define fo(i,a,b) for(ll i = a; i < b; ++i)
#define fd(i,a,b) for(ll i = a ;i >= b ;--i)

using namespace std;

VI fact;
void initializeFactorials(int n,ll mod = MOD)
{
    fact.push_back(1);
    for(int i = 1;i <= n;++i) fact.push_back((1ll*fact[i-1]*i) % mod);
}

ll exp(ll x,ll n,ll mod = MOD)
{
    ll ans = 1ll;
    while(n > 0)
    {
        if(n % 2 != 0) ans = (ans * x) % mod;
        x = (x*x) % mod;
        n >>= 1;
    }
    return ans % mod;
}

ll mod(ll a,ll b){ ll r = ((a%b)+b)%b;  return r;}

ll modInv_1(ll x,ll mod = MOD)  {return exp(x,mod-2);} //! Used only if mod is PRIME.

ll nCr_1(int n,int r,ll mod = MOD) //!Used only if mod is PRIME
{
    return (1ll * fact[n] * modInv_1(fact[n-r],mod) % mod * 1ll * modInv_1(fact[r],mod) % mod);
}

VL prefixCreation(VL v)
{
    VL ans;
    ll sum;
    ans.pb(0);
    for(auto i : v)
    {
        ans.pb(*(ans.end()-1) + i);
    }
    return ans;
}

void solve()
{
    //cout.setf(ios::fixed);
    //cout.precision(3); //!Any number of decimal places required
    int n, x, y;
    cin>>n;
    vector<set<int>> adj(n);
    for(int i = 0; i < n - 1; ++i)
    {
        cin>>x>>y;
        --x;--y;
        adj[x].insert(y);
        adj[y].insert(x);
    }

    int ans = 0, maxi = -1;

    set<pair<int, int>> degs, degs1;
    for(int i = 0; i < n; ++i)
    {
        degs.insert({adj[i].size(), i});
        degs1.insert({adj[i].size(), i});
    }

    auto maxDegItr = degs.rbegin();
    int maxDeg = maxDegItr->first;
    while(maxDegItr != degs.rend() && maxDegItr -> first == maxDeg)
    {
        degs1.erase({maxDegItr->first, maxDegItr->second});
        for(auto i : adj[maxDegItr->second])
        {
            degs1.erase({adj[i].size(),i});
            degs1.insert({adj[i].size()-1,i});
        }
        ans = max(ans, maxDeg + degs1.rbegin()->first - 1);

        for(auto i : adj[maxDegItr->second])
        {
            degs1.insert({adj[i].size(),i});
            degs1.erase({adj[i].size()-1,i});
        }
        degs1.insert({maxDegItr->first, maxDegItr->second});
        ++maxDegItr;
    } 
    

    cout<<ans<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    t = 1;
    cin>>t;

    while(t--)
    {
        solve();
    }
   

    return 0;
}