Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 2):
/*
    AUTHOR : SHIVANSH SINGH
*/

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

#define MOD 1000000007
#define mod 998244353
#define INF 1e18
#define pb push_back
#define pp pop_back
#define ff first
#define ss second
#define ll long long
#define ull unsigned long long
#define lld long long double
#define bit_count __builtin_popcountll
#define sz(x) ((int)(x.size()))
#define all(x) (x).begin(),(x).end()
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << " "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

template <typename T> void _print(T x) { cerr << x; }
template <typename T, typename V> void _print(pair<T, V> p) { cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}"; }
template <typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T, typename V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T> void _print(unordered_set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T, typename V> void _print(unordered_map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T> void _print(deque<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <typename T> void _print(stack<T> s) { vector<T> v; while (!s.empty()) { v.pb(s.top()); s.pop(); } reverse(all(v)); _print(v); }
template <typename T> void _print(queue<T> q) { vector<T> v; while (!q.empty()) { v.pb(q.front()); q.pop(); } _print(v); }
template <typename T> void _print(priority_queue<T> pq) { vector<T> v; while (!pq.empty()) { v.pb(pq.top()); pq.pop(); } _print(v); }

template <typename T> void iv(vector<T>& v, int n) { v.resize(n); for (int i = 0; i < n; ++i) cin >> v[i]; }
template <typename T> void im(vector<vector<T>>& matrix, int m, int n) { matrix.resize(m); for (int i = 0; i < m; ++i) { matrix[i].resize(n); for (int j = 0; j < n; ++j) cin >> matrix[i][j]; } }

int main() {
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    IOS;

    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin>>n;
        vector<vector<ll>>adj(n);
        for(int i=0;i<n-1;i++){
            int u,v;
            cin>>u>>v;
            u--;
            v--;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        vector<ll>deg(n,0);
        for(ll i=0;i<n;i++){
            deg[i]=adj[i].size();
        }
        ll mx_deg=-1;
        ll mx_deg_node=-1;
        for(ll i=0;i<n;i++){
            if(deg[i]>mx_deg){
                mx_deg=deg[i];
                mx_deg_node=i;
            }
        }
        ll ans=mx_deg;
        for(int i=0;i<adj[mx_deg_node].size();i++){
            deg[adj[mx_deg_node][i]]--;
        }
        deg[mx_deg_node]=-1; //does not exist;
        mx_deg=-1;
        mx_deg_node=-1;
        for(ll i=0;i<n;i++){
            if(deg[i]>mx_deg && deg[i]!=-1){
                mx_deg=deg[i];
                mx_deg_node=i;
            }
        }
        ans=ans+mx_deg-1;
        cout<<ans<<endl;

    }

    return 0;
}