Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 4):
// Problem: C. Remove Exactly Two
// Contest: Codeforces - Codeforces Round 1000 (Div. 2)
// URL: https://codeforces.com/contest/2063/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define endl '\n'
#define pb push_back
#define mod 1000000007
#define fin for(int i=0;i<n;i++) 

ll power(ll x, ll y) 
{ 
    if (y == 0) 
        return 1; 
    ll p = power(x, y/2) % mod; 
    p = (p * p) % mod; 
  
    return (y%2 == 0)? p : (x * p) % mod; 
} 
ll factorial[1000007];
void fact(ll n)
{
    memset(factorial,0,sizeof(factorial));
    factorial[0]=1;
	factorial[1]=1;

    for(int i=1;i<n;i++)
    {
        factorial[i]=(factorial[i-1]*i)%mod;
    } 
}
ll ncr(ll n,ll r)
{
    ll ans=1;
    ans=factorial[n];
    ll ans1=power(factorial[n-r],mod-2);
    ll ans2=power(factorial[r],mod-2);
    ans=((ans*ans1)%mod*ans2)%mod;
    return ans;
}
ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 

bool prime[100000006];
void sieve(ll n){
    memset(prime,true,sizeof(prime));
	prime[1]=false; 
    for (ll p=2;p*p<=n;p++){
        if (prime[p]){  
            for (ll i=p*p;i<=n;i+=p)
                prime[i]=false;
        }
    }
}

// string s=bitset<64>(n).to_string();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		map<ll,set<ll> > mp;
		vector<ll> degree(n+1,0);
		for(int i=0;i<n-1;i++)
		{
			ll u,v;
			cin>>u>>v;
			mp[u].insert(v);
			mp[v].insert(u);
			degree[u]++;
			degree[v]++;
		}
		// for(int i=0;i<=n;i++)
		// {
			// cout<<degree[i]<<" ";
		// }
		// cout<<endl;
		ll node1 = 1,node2 =-1;
		ll ans = 0;
		for(int i=2;i<=n;i++)
		{
			// cout<<node1<<" "<<node2<<endl;
			ll res = degree[i];
			// cout<<res<<endl;
			//node1
			if(mp[i].find(node1)==mp[i].end())
			{
				// cout<<"THIS"<<endl;
				ans = max(ans,res+degree[node1]-1);
			}
			else
			{
				// cout<<degree[node1]<<endl;
				ans = max(ans,res+degree[node1]-2);
			}
			//node2
			if(node2!=-1)
			{
				// cout<<"THIS"<<endl;
				if(mp[i].find(node2)==mp[i].end())
				{
					ans = max(ans,res+degree[node2]-1);
				}
				else
				{
					ans = max(ans,res+degree[node2]-2);
				}
			}
			if(degree[i]>degree[node1])
			{
				node2 = node1;
				node1 = i;
			}
			else
			{
				if(node2==-1||degree[node2]<degree[i])
				{
					node2 = i;
				}
			}
			//
		}
		// cout<<ans<<endl;
		// cout<<node1<<" "<<node2<<endl;
		if(mp[node1].find(node2)==mp[node1].end())
		{
			ans = max(ans,degree[node1]+degree[node2]-1);
		}
		else
		{
			ans = max(ans,degree[node1]+degree[node2]-2);
		}
		cout<<ans<<endl;
	}
	return 0;
}


Submission (verdict: Time limit exceeded on test 5):
// Problem: C. Remove Exactly Two
// Contest: Codeforces - Codeforces Round 1000 (Div. 2)
// URL: https://codeforces.com/contest/2063/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define endl '\n'
#define pb push_back
#define mod 1000000007

ll power(ll x, ll y) 
{ 
    if (y == 0) 
        return 1; 
    ll p = power(x, y/2) % mod; 
    p = (p * p) % mod; 
  
    return (y%2 == 0)? p : (x * p) % mod; 
} 
ll factorial[1000007];
void fact(ll n)
{
    memset(factorial,0,sizeof(factorial));
    factorial[0]=1;
	factorial[1]=1;

    for(int i=1;i<n;i++)
    {
        factorial[i]=(factorial[i-1]*i)%mod;
    } 
}
ll ncr(ll n,ll r)
{
    ll ans=1;
    ans=factorial[n];
    ll ans1=power(factorial[n-r],mod-2);
    ll ans2=power(factorial[r],mod-2);
    ans=((ans*ans1)%mod*ans2)%mod;
    return ans;
}
ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
      
} 

bool prime[100000006];
void sieve(ll n){
    memset(prime,true,sizeof(prime));
	prime[1]=false; 
    for (ll p=2;p*p<=n;p++){
        if (prime[p]){  
            for (ll i=p*p;i<=n;i+=p)
                prime[i]=false;
        }
    }
}

// string s=bitset<64>(n).to_string();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
	ll t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		map<ll,set<ll> > mp;
		vector<ll> degree(n+1,0);
		for(int i=0;i<n-1;i++)
		{
			ll u,v;
			cin>>u>>v;
			mp[u].insert(v);
			mp[v].insert(u);
			degree[u]++;
			degree[v]++;
		}
		ll ans = 0;
		// traverse over all the edges for degree[u]+degree[v] - 2
		for(auto it1:mp)
		{
			for(const auto &it2 : it1.second)
			{
				// cout<<it2<<endl;
				ans = max(ans,degree[it1.first]+degree[it2]-2);
			}
		}
		vector<pair<ll,ll> > deg;
		for(int i=1;i<=n;i++)
		{
			deg.pb(make_pair(degree[i],i));
		}
		sort(deg.begin(),deg.end());
		// check all for non adj
		for(int i = 0;i<deg.size();i++)
		{
			for(int j = i+1;j<deg.size();j++)
			{
				if(mp[deg[i].second].find(deg[j].second)==mp[deg[i].second].end())
				{
					ans = max(ans,deg[i].first+deg[j].first-1);
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}