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

Previous submission (verdict: Wrong answer on test 4):
/*
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       "In the name of Allah, the Most Gracious, the Most Merciful"

       Author: 2077DevWave
       Date:   ANOTHER DAAAAAAAAAAAAAAAAAAY IN CP

       Problem: Better To Be Z

       Description:
       This code solves the problem efficiently by something cool
       that we know about that!

       Complexity:
       - Time Complexity: O(-INF)
       - Space Complexity: O(-INF)

       May this code run without errors and lead to be Accepted.
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

// Includes
#include <bits/stdc++.h>

// Name spaces
using namespace std;

// Type definitions for convenience
typedef long long ll; // Long long integer type
typedef pair<int, int> pii; // Pair of integers
typedef pair<ll, ll> pll; // Pair of long long integers
typedef vector<int> vi; // Vector of integers
typedef vector<char> vc; // Vector of charecters
typedef vector<ll> vl; // Vector of long long integers
typedef set<ll> sl; // set of of long long integers
typedef vector<bool> vb; // Vector of long long integers
typedef vector<pii> vpii; // Vector of pairs of integers
typedef vector<pll> vpll; // Vector of pairs of long long integers

// Constant definitions
#define INF 1e9 // Infinity value for integer type
#define LINF 1e18 // Infinity value for long long integer type
#define MOD 1000000007 // Modulus value for modular arithmetic
#define endl "\n" // End of line character

// Commonly used macros
#define fi first // First element of a pair
#define se second // Second element of a pair
#define all(x) (x).begin(), (x).end() // All elements of a container
#define rall(x) (x).rbegin(), (x).rend() // All elements of a container in reverse order
#define sz(x) ((int)(x).size()) // Size of a container

// Loop macros
#define rep(i, a) for (int i = 0; i < (a); i++) // Repaet a time with value i
#define FOR(i, a, b) for (int i = (a); i <= (b); i++) // Loop from a to b (excluding b)
#define ROF(i, a, b) for (int i = (b); i >= (a); i--) // Loop from b to a (excluding a)

// Mathematical constants
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 /* a */

// Data structure definitions
#define matrix(type) vector<vector<type>> // Matrix data structure
typedef map<ll,vl> graph; // Graph data structure
typedef map<ll,vpii> wgraphi; // Weighted graph with integer weights data structure
typedef map<ll,vpll> wgraphl; // Weighted graph with long long integer weights data structure

// Algorithmic functions
#define lb lower_bound // Lower bound function
#define ub upper_bound // Upper bound function
#define sq(x) (x) * (x) // Square function
#define sortup(arr) sort(all(arr))  // Sorts the array in ascending order (min to max)
#define sortdown(arr,type) sort(all(arr),greater<type>) // Sorts the array in descending order (max to min)

// Vector operations
#define vsum(arr) accumulate(all(arr), 0LL)
#define vmax(arr) *max_element(all(arr))
#define vmin(arr) *min_element(all(arr))
#define vunique(arr) arr.erase(unique(all(arr)), arr.end())

// Iterator macros
#define forp(i, a) for (auto i = a.begin(); i != a.end(); i++) // Forward iterator
#define rforp(i, a) for (auto i = a.rbegin(); i != a.rend(); i++) // Reverse iterator

// Read Input
#define rl(x) ll x; cin >> x  // create and Read a single long long
#define rc(x) char x; cin >> x  // create and Read a single character
#define rs(x) string x; cin >> x  // create and Read a single String
#define rv(arr) forp(i,arr) cin >> *i // Read array of size n
#define rvp(arr) forp(i, arr) cin >> i->fi >> i->se // Read vector of pairs
#define rm(arr, n, m) rep(i,n) for (int j = 0; j < (m); j++) cin >> arr[i][j] // Read 2D array of size n x m
#define rg(g, n) rep(i,n) {int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u);}  // g = graph , m = number of edges

// Print Output
#define YES cout << "YES" << endl // Print "YES" to the console
#define NO cout << "NO" << endl // Print "NO" to the console
#define Yes cout << "Yes" << endl // Print "Yes" to the console
#define No cout << "No" << endl // Print "No" to the console
#define yes cout << "yes" << endl // Print "yes" to the console
#define no cout << "no" << endl // Print "no" to the console
#define Case(x) cout << "Case #" << x << ": " // Print case number to the console
#define print(x) cout << x << endl // Print a value to the console
#define print2(x, y) cout << x << " " << y << endl // Print two values to the console
#define print3(x, y, z) cout << x << " " << y << " " << z << endl // Print three values to the console
#define printv(arr) for (auto x : arr) cout << x << " "; cout << endl // Print a vector to the console
#define printvp(arr) for (auto x : arr) cout << x.fi << " " << x.se << endl // Print a vector of pairs to the console
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void solve(){
    rl(n); map<ll,sl> graph;
    ll cnt = 0, ans = 1;

    rep(i,n-1){rl(u);rl(v); graph[u].insert(v);graph[v].insert(u);}

    rep (k,2)
    {
        ll max_deg = graph.rbegin()->fi;
        rforp(i,graph) if(graph[max_deg].size() < graph[i->fi].size()) max_deg = i->fi;

        // if(k == 0){
        //     bool flag = true;
        //     for (ll i : graph[max_deg]) if(graph[i].size() == graph[i].)
            
        // }

        ans += graph[max_deg].size() - 1;

        forp(j,graph[max_deg]) graph[*j].erase(max_deg);

        graph.erase(max_deg);
    }

    print(ans);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main()
{
    cin.tie(0);cin.sync_with_stdio(0);
    cout.tie(0);cout.sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
/*
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       "In the name of Allah, the Most Gracious, the Most Merciful"

       Author: 2077DevWave
       Date:   ANOTHER DAAAAAAAAAAAAAAAAAAY IN CP

       Problem: Better To Be Z

       Description:
       This code solves the problem efficiently by something cool
       that we know about that!

       Complexity:
       - Time Complexity: O(-INF)
       - Space Complexity: O(-INF)

       May this code run without errors and lead to be Accepted.
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

// Includes
#include <bits/stdc++.h>

// Name spaces
using namespace std;

// Type definitions for convenience
typedef long long ll; // Long long integer type
typedef pair<int, int> pii; // Pair of integers
typedef pair<ll, ll> pll; // Pair of long long integers
typedef vector<int> vi; // Vector of integers
typedef vector<char> vc; // Vector of charecters
typedef vector<ll> vl; // Vector of long long integers
typedef set<ll> sl; // set of of long long integers
typedef vector<bool> vb; // Vector of long long integers
typedef vector<pii> vpii; // Vector of pairs of integers
typedef vector<pll> vpll; // Vector of pairs of long long integers

// Constant definitions
#define INF 1e9 // Infinity value for integer type
#define LINF 1e18 // Infinity value for long long integer type
#define MOD 1000000007 // Modulus value for modular arithmetic
#define endl "\n" // End of line character

// Commonly used macros
#define fi first // First element of a pair
#define se second // Second element of a pair
#define all(x) (x).begin(), (x).end() // All elements of a container
#define rall(x) (x).rbegin(), (x).rend() // All elements of a container in reverse order
#define sz(x) ((int)(x).size()) // Size of a container

// Loop macros
#define rep(i, a) for (int i = 0; i < (a); i++) // Repaet a time with value i
#define FOR(i, a, b) for (int i = (a); i <= (b); i++) // Loop from a to b (excluding b)
#define ROF(i, a, b) for (int i = (b); i >= (a); i--) // Loop from b to a (excluding a)

// Mathematical constants
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 /* a */

// Data structure definitions
#define matrix(type) vector<vector<type>> // Matrix data structure
typedef map<ll,vl> graph; // Graph data structure
typedef map<ll,vpii> wgraphi; // Weighted graph with integer weights data structure
typedef map<ll,vpll> wgraphl; // Weighted graph with long long integer weights data structure

// Algorithmic functions
#define lb lower_bound // Lower bound function
#define ub upper_bound // Upper bound function
#define sq(x) (x) * (x) // Square function
#define sortup(arr) sort(all(arr))  // Sorts the array in ascending order (min to max)
#define sortdown(arr,type) sort(all(arr),greater<type>) // Sorts the array in descending order (max to min)

// Vector operations
#define vsum(arr) accumulate(all(arr), 0LL)
#define vmax(arr) *max_element(all(arr))
#define vmin(arr) *min_element(all(arr))
#define vunique(arr) arr.erase(unique(all(arr)), arr.end())

// Iterator macros
#define forp(i, a) for (auto i = a.begin(); i != a.end(); i++) // Forward iterator
#define rforp(i, a) for (auto i = a.rbegin(); i != a.rend(); i++) // Reverse iterator

// Read Input
#define rl(x) ll x; cin >> x  // create and Read a single long long
#define rc(x) char x; cin >> x  // create and Read a single character
#define rs(x) string x; cin >> x  // create and Read a single String
#define rv(arr) forp(i,arr) cin >> *i // Read array of size n
#define rvp(arr) forp(i, arr) cin >> i->fi >> i->se // Read vector of pairs
#define rm(arr, n, m) rep(i,n) for (int j = 0; j < (m); j++) cin >> arr[i][j] // Read 2D array of size n x m
#define rg(g, n) rep(i,n) {int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u);}  // g = graph , m = number of edges

// Print Output
#define YES cout << "YES" << endl // Print "YES" to the console
#define NO cout << "NO" << endl // Print "NO" to the console
#define Yes cout << "Yes" << endl // Print "Yes" to the console
#define No cout << "No" << endl // Print "No" to the console
#define yes cout << "yes" << endl // Print "yes" to the console
#define no cout << "no" << endl // Print "no" to the console
#define Case(x) cout << "Case #" << x << ": " // Print case number to the console
#define print(x) cout << x << endl // Print a value to the console
#define print2(x, y) cout << x << " " << y << endl // Print two values to the console
#define print3(x, y, z) cout << x << " " << y << " " << z << endl // Print three values to the console
#define printv(arr) for (auto x : arr) cout << x << " "; cout << endl // Print a vector to the console
#define printvp(arr) for (auto x : arr) cout << x.fi << " " << x.se << endl // Print a vector of pairs to the console
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void solve(){
    rl(n); map<ll,pair<ll,sl>> graph;
    ll cnt = 0, ans = 1;

    rep(i,n-1){rl(u);rl(v); graph[u].se.insert(v);graph[v].se.insert(u);}
    
    forp(i,graph){
        ll sum = 0;
        for (ll j : i->se.se)
        {
            sum += (graph[j].se.size() == i->se.se.size());
        }
        i->se.first = sum;
    }

    rep (k,2)
    {
        ll max_deg = graph.begin()->fi;
        forp(i,graph) if(graph[max_deg].se.size() <= graph[i->fi].se.size() and i->se.fi < graph[max_deg].fi) max_deg = i->fi;

        ans += graph[max_deg].se.size() - 1;

        forp(j,graph[max_deg].se) graph[*j].se.erase(max_deg);

        graph.erase(max_deg);
    }

    print(ans);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main()
{
    cin.tie(0);cin.sync_with_stdio(0);
    cout.tie(0);cout.sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}