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

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define epb emplace_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define int long long
typedef vector<int>vi;
typedef vector<long long>vl;
// Define the ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fl(i,a,b) for(int i=a; i<b; i++)
void debug(vector<int>arr){for(auto &i:arr){cout<<i<<" ";}cout<<nline;}
// ~~~~~~~~

ll gcd(ll a, ll b) {
	if (b > a) {
		return gcd(b, a);
	}
	if (b == 0) {
	 	return a;
	} 
	return gcd(b, a % b);
}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {
	if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}

void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;}
 extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr);
 return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
ll lcm(ll a, ll b){return (a*b)/gcd(a,b);}
// ~~    mod ~~~~
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; 
return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; 
while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0)
 {while (n % i == 0)n /= i; number = (number / i * (i - 1));}}
  if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))


vector<pair<int,int>>direction = {{1,0},{-1,0},{0,1},{0,-1}};
vector<int>dix = {1,-1,0,0};
vector<int>diy = {0,0,-1,1};
// ~~~~~~~~~~~~
// use binary search in pair vector 
int lower_bound(vector<pair<int,int>>&v, int &target){int start = 0;int end = v.size()-1;int ans = v.size();while(start <= end){int mid = (start + end )/2;if(v[mid].first < target){start = mid + 1;}else{ans = mid;end = mid - 1;}}return ans;} // O(log n)

bool check(string s){
    for(int i = 0 ; i < s.size()/2 ; ++i ){
        if(s[i] != s[s.size() - 1 - i]) return false;
    }
    return true;
}

void solve(){
	int n;
	cin>>n;
	vector<vector<int>>edge(n);
	map<pair<int,int>,int>mp;
	vector<int>degree(n);
	for(int i = 0; i < n-1; i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		mp[{u,v}]++;
		mp[{v,u}]++;
		edge[u].push_back(v);
		degree[u]++;
		degree[v]++;
	}
	vector<pair<int,int>>pi;
	for(int i = 0; i < n; i++){
		pi.emplace_back(degree[i],i);
	}
	sort(pi.begin(),pi.end(),greater<pair<int,int>>());
	int ans = 0;
	for(int i = 0; i < n; i++){
		for(int j = i+1; j < n; j++){
			if(mp.find({pi[i].second,pi[j].second}) == mp.end()){
				ans = max(ans,pi[i].first + pi[j].first - 1);
			}
		}
	}
	for(int i = 0; i < n; i++){
		ans = max(ans,pi[i].ff + pi[i+1].ff - 2);
	}
	cout<<ans<<endl;
}
signed main(){
	int t = 1;
	cin>>t;
	while(t--){
		solve();
	}

}

// precuation 
// check code properly min  two time 
// build good testcase then submit

Submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using namespace chrono;
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define epb emplace_back
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define int long long
typedef vector<int>vi;
typedef vector<long long>vl;
// Define the ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fl(i,a,b) for(int i=a; i<b; i++)
void debug(vector<int>arr){for(auto &i:arr){cout<<i<<" ";}cout<<nline;}
// ~~~~~~~~

ll gcd(ll a, ll b) {
	if (b > a) {
		return gcd(b, a);
	}
	if (b == 0) {
	 	return a;
	} 
	return gcd(b, a % b);
}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {
	if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}

void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;}
 extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr);
 return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
ll lcm(ll a, ll b){return (a*b)/gcd(a,b);}
// ~~    mod ~~~~
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}

ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; 
return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; 
while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0)
 {while (n % i == 0)n /= i; number = (number / i * (i - 1));}}
  if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))


vector<pair<int,int>>direction = {{1,0},{-1,0},{0,1},{0,-1}};
vector<int>dix = {1,-1,0,0};
vector<int>diy = {0,0,-1,1};
// ~~~~~~~~~~~~
// use binary search in pair vector 
int lower_bound(vector<pair<int,int>>&v, int &target){int start = 0;int end = v.size()-1;int ans = v.size();while(start <= end){int mid = (start + end )/2;if(v[mid].first < target){start = mid + 1;}else{ans = mid;end = mid - 1;}}return ans;} // O(log n)

bool check(string s){
    for(int i = 0 ; i < s.size()/2 ; ++i ){
        if(s[i] != s[s.size() - 1 - i]) return false;
    }
    return true;
}

void solve(){
	int n;
	cin>>n;
	vector<vector<int>>edge(n);
	map<pair<int,int>,int>mp;
	vector<int>degree(n);
	for(int i = 0; i < n-1; i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		mp[{u,v}]++;
		mp[{v,u}]++;
		edge[u].push_back(v);
		degree[u]++;
		degree[v]++;
	}
	vector<pair<int,int>>pi;
	for(int i = 0; i < n; i++){
		pi.emplace_back(degree[i],i);
	}
	sort(pi.begin(),pi.end(),greater<pair<int,int>>());
	int ans = 0;
	for(int i = 0; i < n; i++){
		for(int j = i+1; j < n; j++){
			if(mp.find({pi[i].second,pi[j].second}) == mp.end()){
				ans = max(ans,pi[i].first + pi[j].first - 1);
			}
		}
	}
	for(int i = 0; i < n-1; i++){
		ans = max(ans,pi[i].ff + pi[i+1].ff - 2);
	}
	cout<<ans<<endl;
}
signed main(){
	int t = 1;
	cin>>t;
	while(t--){
		solve();
	}

}

// precuation 
// check code properly min  two time 
// build good testcase then submit