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

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update>



#define ll long long

#define scn(v) for(auto &i:v)cin>>i
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define vl vector<ll>
#define vi vector<int>
#define vvi vector<vi>
#define vvl vector<vl>
#define vb vector<bool>
#define vvb vector<vb>
#define en cout<<'\n'
#define vb vector<bool>
#define vpl vector<pair<ll,ll>>
#define vi vector<int>
#define X first
#define Y second

const int MOD = 998244353;
ll mul(ll a, ll b) {
    return (((a % MOD + MOD) * (b % MOD + MOD)) % MOD);
}
ll fastPow(ll x, ll n, ll m) {
    if (n == 0) {
        return 1;
    }
    ll po = fastPow(x, n / 2, m);
    po =mul(po,po) % m;
    if (n & 1) {
        po =mul(po,x) %m;
    }
    return po % m;
}

ll modInv(ll x, ll m) {
    return fastPow(x, m - 2, m) % m;
}

ll setBit(ll num, ll idx, ll val = 1) {
    return (val) ? (num | (1 << idx)) : (num & ~(1 << idx));
}
int getBit(ll num, int idx) { return ((num >> idx) & 1) == 1; }

void burnx(){
     ll n;
     cin>>n;
     vvl adj(n+1);
     vl deg(n+1);
     for(int i=1;i<n;i++){
         ll u,v;
         cin>>u>>v;
         adj[u].push_back(v);
         adj[v].push_back(u);
         deg[u]++;
         deg[v]++;
     }
//     if(n<=2){
//         cout<<"0";
//         return;
//     }
     ll ans=0;
     ll cnt=1;

     for(int z=0;z<2;z++){
         ll mx=-1;
         ll mxi=-1;
         for(int i=1;i<=n;i++){
             if(deg[i]>mx){
                 mxi=i;
                 mx=deg[i];
             }
         }
         cnt+=deg[mxi]-1;
         deg[mxi]=-1;
         for(auto v:adj[mxi]){
             deg[v]--;
         }
     }

     cout<<cnt;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cin.tie(NULL);
//  freopen ("input.txt","r",stdin);
//   freopen ("output.txt","w",stdout);
    //preFact();
    int t = 1;
    cin >> t;

    for(int txt=1;txt<=t;txt++){
        burnx();
        en;
    }

    return 0;
}

Submission (verdict: Time limit exceeded on test 6):
#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pair<ll,ll>, null_type,less<pair<ll,ll>>, rb_tree_tag,tree_order_statistics_node_update>



#define ll long long

#define scn(v) for(auto &i:v)cin>>i
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define vl vector<ll>
#define vi vector<int>
#define vvi vector<vi>
#define vvl vector<vl>
#define vb vector<bool>
#define vvb vector<vb>
#define en cout<<'\n'
#define vb vector<bool>
#define vpl vector<pair<ll,ll>>
#define vi vector<int>
#define X first
#define Y second

const int MOD = 998244353;
ll mul(ll a, ll b) {
    return (((a % MOD + MOD) * (b % MOD + MOD)) % MOD);
}
ll fastPow(ll x, ll n, ll m) {
    if (n == 0) {
        return 1;
    }
    ll po = fastPow(x, n / 2, m);
    po =mul(po,po) % m;
    if (n & 1) {
        po =mul(po,x) %m;
    }
    return po % m;
}

ll modInv(ll x, ll m) {
    return fastPow(x, m - 2, m) % m;
}

ll setBit(ll num, ll idx, ll val = 1) {
    return (val) ? (num | (1 << idx)) : (num & ~(1 << idx));
}
int getBit(ll num, int idx) { return ((num >> idx) & 1) == 1; }

void burnx(){
     ll n;
     cin>>n;
     vvl adj(n+1);
     vl deg(n+1);
     vpl dgi(n+1);
     vector<set<ll>> st(n+1);
     for(int i=1;i<n;i++){
         ll u,v;
         cin>>u>>v;
         adj[u].push_back(v);
         adj[v].push_back(u);
         st[u].insert(v);
         st[v].insert(u);
         deg[u]++;
         deg[v]++;
     }

     for(int i=1;i<=n;i++){
         dgi[i]={deg[i],i};
     }
     sort(rall(dgi));

     ll ans=0;
     ll cnt=1;
//
//     for(int z=0;z<2;z++){
//         ll mx=-1;
//         ll mxi=-1;
//         for(int i=1;i<=n;i++){
//             if(deg[i]>mx){
//                 mxi=i;
//                 mx=deg[i];
//             }
//         }
//         cnt+=deg[mxi]-1;
//         deg[mxi]=-1;
//         for(auto v:adj[mxi]){
//             deg[v]--;
//         }
//     }


        for (int x = 1; x <= n; ++x) {
            for (int y = x + 1; y <= n; ++y) {
                ll temp = deg[x] + deg[y] - (st[x].find(y) != st[x].end() ? 2 : 1);
                ans = max(ans, temp);
            }
        }

    cout<<ans;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cin.tie(NULL);
//  freopen ("input.txt","r",stdin);
//   freopen ("output.txt","w",stdout);
    //preFact();
    int t = 1;
    cin >> t;

    for(int txt=1;txt<=t;txt++){
        burnx();
        en;
    }

    return 0;
}