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


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;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
//---- Debugger ---- //
#ifndef ONLINE_JUDGE
#define debarr(a,n) cout<<#a<<" : ";for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define debmat(mat,row,col) cout<<#mat<<" :\n";for(int i=0;i<row;i++) {for(int j=0;j<col;j++) cout<<mat[i][j]<<" ";cout<<endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cout << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cout << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cout << "["; for (T i = a; i != b; ++i) {if (i != a) cout << ", "; cout << *i;} cout << "]\n";}
//----------------- //
#else
#define pr(...){}
#define debarr(a,n){}
#define debmat(mat,row,col){}
#endif

#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr);
#define nl cout<<'\n'
#define ppb pop_back
#define pb push_back
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define fr for(int i=0  ; i < n ; i++ )
#define all(a) a.begin(), a.end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define sortt(ar) sort(ar.begin(),ar.end())
#define int long long
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define spi set<pair<int,int>>
#define pi vector<pair<int,int>>
#define vvi vector<vector<int>>
#define printans(ans) for(auto it:ans)cout<<it<<" ";
#define endl '\n' 
void solve()
{
  int n;
  cin>>n;
  vi g[n+1];
  rep(i,0,n-1)
  {
      int x,y;cin>>x>>y;
      g[x].pb(y);
      g[y].pb(x);
  }
  multiset<int> se;
  for(int i=1;i<=n;i++)
  {
    se.insert(g[i].size());
  }
  auto op = se.rbegin();
  int sum=0;
  if(*op==1);
  else
    {
      sum = *op ;
      op++;
      if(*op==1)
        sum--;
      else  
          sum+=*op-1;
    }

  cout<<sum<<endl;

    
}
signed main()
{
   fast
       int t; cin>>t ;   for(int i=0;i<t;i++)
      solve();
 

 }