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


Submission (verdict: Wrong answer on test 4):
//JAI SHREE RAM
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
typedef long long ll;
#define inf 1e18

/*
-----------------------------ordered set--------------------
#include <ext/pb_ds/assoc_container.hpp> // Common file  
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional> // for less  
#include <iostream>  
using namespace __gnu_pbds;  
using namespace std;  
  
typedef tree<int, null_type, less<int>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;  
*/

/*-----------------PRIME FACTOR-------------
for (int i = 2; i*i <= n; i = i+2)
{
    // While i divides n, print i and divide n
    while (n%i == 0)
    {
        printf("%d ", i);
        n = n/i;
    }
}
*/

/*-----------------------sieve----------------
vector<bool> prime(MAXN, false);
vector<bool> used(MAXN, false);
void precalc() {
    for (int i = 2; i < MAXN; ++i) {
        if (!used[i]) {
            prime[i] = true;
            for (int j = i; j < MAXN; j += i) {
                used[j] = true;
            }
        }
    }
}
*/

/*--------------------------------sqrt function------------------------
ll my_sqrt(ll a)
{
    ll l=0,r=5000000001;
    while(r-l>1)
    {
        ll mid=(l+r)/2;
        if(1ll*mid*mid<=a)l=mid;
        else r=mid;
    }
    return l;
}

ll getclosest(ll x)
{
    ll n = sqrt(x);
    if(n*n==x)
    {
        return 0;
    }
    else
    {
        return (n+1)*(n+1) - x;
    }
}
ll cur = 0;
    for(ll i=1;i<n;i++)
    {
        ll a,x;
        cin>>a>>x;
        cur+=x;
        cout<<cur+w*(n-i)-cur*(n-i-1)<<" ";
    }
    cout<<endl;

const int MAXN = 1e5+5;
vector<vector<int>>divisors(MAXN);
void precomp()
{
    for(int i=2;i<MAXN;i++)
    {
        for(int j = i;j<MAXN;j+=i)
        {
            divisors[j].push_back(i);
        }
    }
}
*/
void solve() 
{
    int n;cin>>n;
    vector<vector<int>>val(n+1);
    for(int i=0;i<n-1;i++)
    {
        int u,v;cin>>u>>v;
        val[u].push_back(v);
        val[v].push_back(u);
    }
    /*
    for(int i=1;i<=n;i++)
    {
        cout<<i<<" "<<val[i].size()<<" == ";
        for(int j = 0;j<val[i].size();j++)
        {
            cout<<val[i][j]<<" ";
        }
        cout<<endl;
    }
    */
    vector<pair<int,int>>degree,degree2;
    for(int i=1;i<=n;i++)
    {
        degree.push_back({val[i].size(),i});
    }
    sort(degree.begin(),degree.end());
    int comp = 0;
    if(degree.back().first==0)
    {
        if(degree.size()==1)
        {
            cout<<0<<endl;
            return;
        }
        else
        {
            comp--;
        }
    }
    else
    {
        if(degree.back().first!=1)
        {
            comp+=degree.back().first;
        }
        int del = degree.back().second;
        //cout<<"del: "<<del<<endl;
        set<int>cur;
        for(auto p:val[del])
        {
            cur.insert(p);
        }
        for(int i=0;i<n;i++)
        {
            if(degree[i].second==del)continue;
            else if(cur.count(degree[i].second))
            {
                degree2.push_back({degree[i].first-1,degree[i].second});
            }
            else
            {
                degree2.push_back({degree[i].first,degree[i].second});
            }
        }
    }
    sort(degree2.begin(),degree2.end());
    /*
    for(auto p:degree2)
    {
        cout<<p.first<<" "<<p.second<<endl;
    }
    */
    if(degree2.back().first==0)
    {
        if(degree2.size()==1)
        {
            cout<<0<<endl;
            return;
        }
        else
        {
            comp--;
        }
    }
    else
    {
        if(degree2.back().first!=1)
            comp+=degree2.back().first-1;
    }
    cout<<comp<<endl;

}

int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;  // Fix type of t
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}