Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 843 ms):
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#ifndef InfernoStinger
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
#define all(x) (x).begin() , (x).end()
#define ll long long
#define int long long  
#define mod 1000000007
#define mod1 998244353
#define inf 1e18
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(long double t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(bool t) {cerr << t;}
void _print(unsigned long long t) {cerr << t;}
template <class T> bool ckmin(T&a, const T&b) { bool B = a > b; a = min(a,b); return B; }
template <class T> bool ckmax(T&a, const T&b) { bool B = a < b; a = max(a,b); return B; }
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

int solve()
{   
    int n;
    cin>>n;
    vector<set<int>> g(n+1);
    vector<int> deg(n+1, 0);
    multiset<int> curs;
    for(int i=0;i<n-1;i++){
        int v,u;
        cin>>v>>u;
        g[v].insert(u);
        g[u].insert(v);
        deg[u]++;deg[v]++;
    }
    for(int i=1;i<=n;i++){
        curs.insert(deg[i]);
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        int temp=deg[i];
        curs.erase(curs.find(deg[i]));
        for(auto nei:g[i]){
            curs.erase(curs.find(deg[nei]));
            curs.insert(deg[nei]-1);
        }
        ans=max(ans, temp+*curs.rbegin());
        for(auto nei:g[i]){
            curs.erase(curs.find(deg[nei]-1));
            curs.insert(deg[nei]);
        }
        curs.insert(temp);
    }
    cout<<ans-1<<"\n";
    return 0;
}
 
int32_t main() 
{   
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif
    int t;
    cin>>t;
    for(int tc=0;tc<t;tc++)
    {   
        solve();   
    }
    return 0;
}

Solution 2 (runtime: 671 ms):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h> 
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag,
 __gnu_pbds::tree_order_statistics_node_update> ordered_set;*/
typedef long long ll;
#define pb push_back
typedef long double ld;
/*int smallestdivisor(int n)
{
    // if divisible by 2
    if (n % 2 == 0)
        return 2;
 
    // iterate from 3 to sqrt(n)
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return i;
    }
 
    return n;
}*/
/*int parent[200005];
int siz[200005];
void make_set(ll n){
for(int i=1;i<=n;i++){parent[i]=i;siz[i]=1;}
}

int find_set(ll v){
    if(v==parent[v])
    return v;
    return parent[v]=find_set(parent[v]);
    
}

void union_set(ll a,ll b){
    a=find_set(a);
    b=find_set(b);
    if(a!=b){
        if(siz[a]<siz[b])
        swap(a,b);
        parent[b]=a;
        siz[a]+=siz[b];
    }
}
ll kruskal(vector<pair<ll,pair<ll,ll>>>& edges,ll n){
    sort(edges.begin(),edges.end());
    make_set(n);
    ll sum=0;
    for(auto x:edges){
        if(find_set(x.second.first)==find_set(x.second.second)) continue;
        union_set(x.second.first,x.second.second);
        sum+=x.first;
    }
    return sum;
}*/
int vis[300005];//vector<ll> level(300005);
//queue<ll> q;
/*void bfs(vector<vector<ll>>& adj,ll& ans){
    ll src=0;
    vis[src]=1;
   // level[src]=0;
    q.push(src);
    while(!q.empty()){
       u=q.top();
        q.pop();vis[u]=1;
        for(int v:adj[u]){
            if(vis[v]==1) continue;
           // level[v]=level[u]+1;
            q.push(v);
        }
    }
}*/
void dfs(ll u,vector<vector<int>>& adj){
   vis[u]=1;
 
   for(auto v:adj[u]){
    
    if(vis[v]==1) continue;
     
     dfs(v,adj);
    
     
   }
   
   //saari v se action lene k baad kuch u ka check krna h toh this pos-->dp or any

}
/*void dijkstras(vector<vector<pair<ll,ll>>> adj,ll point,vector<vector<ll>> &dis){

  priority_queue<pair<ll,ll>> pq;
   int src=point;
   dis[src][src]=0;
   pq.push({0,src});

   while(!pq.empty()){

     int u=pq.top().second;
     int d=-pq.top().first;
     pq.pop();

     if(dis[src][u]<d) continue;

     for(auto v:adj[u]){
        
        if(dis[src][v.first]<=dis[src][u]+v.second) continue;
        dis[src][v.first]=dis[src][u]+v.second;
        pq.push({-dis[src][v.first],v.first});

     }

   }

}*/
signed main(){ios::sync_with_stdio(0); cin.tie(0); //cout.tie(0);cout << fixed << setprecision(15);
ll tt=1;cin>>tt;string pi = "3.141592653589793238462643383279";
while(tt--){ ll n;cin>>n;vector<vector<int>> adj(n);vector<pair<ll,ll>> p;map<pair<ll,ll>,int> m;
for(int i=0;i<n-1;i++){
    ll x,y;cin>>x>>y;x--;y--;p.pb({x,y});m[{x,y}]++;m[{y,x}]++;
    adj[x].pb(y);adj[y].pb(x);
   
}
vector<pair<ll,ll>> v;
for(int i=0;i<n;i++) v.pb({adj[i].size(),i});
sort(v.rbegin(),v.rend());ll ans=0;
for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
        if(m[{v[i].second,v[j].second}]==0){
            ll k=v[i].first+v[j].first-1;
            ans=max(ans,k);break;
        }
    }
}
for(int i=0;i<n-1;i++){
    ll k=adj[p[i].first].size()+adj[p[i].second].size()-2;
    ans=max(ans,k);
}
cout<<ans<<'\n';


    
    
    
    
    
    

   
    

  











}
 return 0;
}
 
 


















/*------------>>>>>DSU.....................
int parent[n];
int size[n];
void make_set(int n){
for(int i=1;i<=n;i++){parent[i]=i;size[i]=1;}
}

int find_set(int v){
    if(v==parent[v])
    return v;
    return parent[v]=find_set(parent[v]);
    
}

void union_set(int a,int b){
    a=find_set(a);
    b=find_set(b);
    if(a!=b){
        if(size[a]<size[b])
        swap(a,b);
        parent[b]=a;
        size[a]+=size[b];
    }
}
*/
/*void dijkstras(vector<vector<pair<ll,ll>>>& adj,vector<ll>& s,vector<vector<ll>>& dis,ll& n){
     vector<vector<ll>> vis(n,vector<ll> (n,0));
    priority_queue<pair<ll,pair<ll,ll>>> pq;
    int src=0;
    dis[src][0]=0;
    pq.push({0,{src,0}});
   
    while(!pq.empty()){
        ll u=pq.top().second.first;
        ll d=-pq.top().first;
       ll bike= pq.top().second.second;

        pq.pop();
        
     
        if(vis[u][bike]!=0) continue;// this is an useful optimisation

        for(auto v:adj[u]){
             if(s[u]>s[bike]){
                dis[v.first][bike]=min(dis[v.first][bike],dis[u][bike]+v.second*s[bike]);
                pq.push({-dis[v.first][bike],{v.first,bike}});
             }
             else{
                dis[v.first][u]=min(dis[v.first][u],dis[u][bike]+v.second*s[u]);
                pq.push({-dis[v.first][u],{v.first,u}});
             }
           vis[u][bike]++;


        }
    }
}*/











/*ll sum_digit(ll n)                                                                 
{ 
    ll num=0;
    while(n>0)
    {
        num+=n%10;
        n=n/10;
    }
    return num;
}*/
//kisi bhi tough prob me max ya min value k liye binary search is always there to help.....
//first bit of any number is always a divisor of that number........
//What is convex hull trick?????????????????
//dp mein hum kya krna chah rhe iski puri info honi chahiye kisi bhi point par aese sochna pdega.....states k liye>>>>>>>

//r.insert(r.begin(), 1 - d);----------->>>>to insert element in starting of an vevtor..........
/*for(auto it=m.rbegin();it!=m.rend();++it){
  for(int i=0;i<it->second.size();i++){
    v2.pb(it->second[i]);
  }
}*/
//largest power of a prime number that divides fact! is legendres formula.........
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
//sometimes long long ko int krne par tle nhi aata(kaafi ajeeb :())
 // Binary search me r=n+1,l=0&&condition:l+1<r;
 
// for(int i=2;i<=N;i++){
  //  if(prime[i]==0){
    //v.pb(i);
    //for(int j=2*i;j<=N;j+=i){prime[j]++;}
    //}
 //}
 /*int smallestdivisor(int n)
{
    // if divisible by 2
    if (n % 2 == 0)
        return 2;
 
    // iterate from 3 to sqrt(n)
    for (int i = 3; i * i <= n; i += 2) {
        if (n % i == 0)
            return i;
    }
 
    return n;
}*/
/*ll binpow(ll a,ll b,ll m = LLONG_MAX){
    ll res = 1;
    while (b){
        if (b&1) res = (res*a)%m;
        a = (a*a)%m;
        b>>=1;
    }
    return res;
}*/
/*for(int i=0;i<n;i++){
  if(m[s[i]]==0){
 if(i1==-1){ a[i]+=i+(n-1-i)*(i)+n-1-i;a[i]++;i1=i;}
 else{
  a[i]+=i-i1-1+(n-1-i)*(i-i1-1)+n-1-i;a[i]++;i1=i;
 }
  }
}
for(int i=0;i<n;i++){
  ans-=a[i];
}*/
/////Favorites>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>............................................
/*ll n,k;cin>>n>>k;
 if(n%2==0){
  if(k%n)cout<<k%n<<'\n';
  else{cout<<n<<'\n';}
 }
 else{ll n1=n-1;n1/=2;ll a=(k-1)/n1;ll b=k%n;
  if(b==0){b=n;}b+=a;b%=n;if(b==0){b=n;}
  cout<<b<<'\n';
  
 }*/
 /*const ll N=2*(1e5+10);
 ll a[N];vector<ll> p(N,0);
 vector<vector<ll>> g(N);
 void dfs(ll vertex){
  for(int child: g[vertex]){
    if(child!=p[vertex]){
      p[child]=vertex;
    dfs(child);
    a[vertex]+=a[child];
    }
  }
  if(g[vertex].size()==1&&a[vertex]==0){
    a[vertex]++;
  }
 }*/
 /*#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define mll map<long long,long long>
#define max(a,b) ((a>b)?a:b)
#define min(a,b) ((a>b)?b:a)
#define pb push_back
#define prll pair<long long,long long>
typedef long long ll;
typedef long double ld;
#define int long long 
#define mini(x,y) x = min(x,y)
#define maxi(x,y) x = max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
#define ff first
#define ss second
#define rep(i,a,n)   for(ll i=a; i<n; i++)
#define rrep(i,a,n)  for(ll i=n-1; i>=a; i--)
typedef map<string,ll> msl;
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define sz(x) ((long long)x.size())
#define trav(a,x) for(auto &a:x)
const ll mx=200005;
const ll mod=1e9 + 7;
const ld PI = 3.141592653589793238460;
//DEBUG FUNCTIONS START
#define cerr cout
void __print(ll x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void print(T t, V... v) {_print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
//#define deb(x...) cerr << "[" << #x << "] = "; _print(x)
#define deb(x...)  _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> rng(0,1);
// ll x=rng(mt);
ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll extgcd(ll a,ll b,ll& x,ll& y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = extgcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}
bool isPrime(ll n){
    if (n <= 1) 
        return false; 
    if (n <= 3) 
        return true; 
    if (n % 2 == 0 || n % 3 == 0) 
        return false; 
    for (ll i = 5; i * i <= n; i = i + 6) 
        if (n % i == 0 || n % (i + 2) == 0) 
            return false; 
    return true; 
}
bool isPowerOfTwo(ll n) { 
   if(n==0) 
   return false; 
   return ((ll)ceil(log2(n)) == (ll)floor(log2(n))); 
}
ll powm(ll a,ll b) {
    ll res = 1;
    while (b) {
        if (b & 1)
            res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return res;
}
ll divide(ll a,ll b) {
    return (a % mod) * powm(b, mod - 2) % mod;
}
ll mul(ll a,ll b) {
    return ((a % mod) * (b % mod)) % mod;
}
 
ll add(ll a,ll b) {
    return (a % mod + b % mod) % mod;
}
ll Totfun(ll n){
    ll z=n;
    if(n%2==0){
        while(n%2==0){
            n/=2;
        }
        z/=2;
    }
    for(ll i=3;i*i<=n;i+=2){
        if(isPrime(i)&&n%i==0){
          while(n%i==0){
            n/=i;
          }
        z-=z/i;  
        }
    }
    if(n>1){
        z-=z/n;
    }
    return z;
}
ll fact[mx];
ll nCr(ll n,ll r){
    if(r>n||r<0) return 0;
    ll z=fact[n];
    z=mul(z,powm(fact[n-r],mod-2));
    z=mul(z,powm(fact[r],mod-2));
    return z;
}
/*
fact[0]=1;
rep(i,1,mx){
 fact[i]=mul(i,fact[i-1]);
}
*/
/*
vll a[mx];
ll l[mx],d[mx];
void dfs(ll x,ll y){
   l[x]=1; d[x]=y;
   for(ll i:a[x])
   if(!l[i])
   dfs(i,y+1);
}
void bfs(ll x){
    queue<ll> q;
    q.push(x);
    l[x]=1;
    d[x]=0;
    while(!q.empty()){
        ll cur=q.front();
        q.pop();
        for(ll i : a[cur]){
            if(1-l[i]){
                q.push(i);
                d[i]=d[cur]+1;
                l[i]=1;
            }
        }
    }
}
*/

/*struct fenwick{

    int n;
    vll bit;

    fenwick(int _n){
        n = _n + 5;
        bit.resize(n);
    }

    int qry(int i){
        int res = 0;
        for(++i; i; i -= i & - i)
            res += bit[i];
        return res;
    }

    void upd(int i, int val){
        for(++i; i < n; i += i & -i)
            bit[i] += val;
    }
};
ll m;
void dfs(ll x,vector<vector<int>>& adj,vector<vector<int>>& dp){
    rep(i,1,m+1) dp[x][i]=1;
    for(ll i:adj[x]){
        dfs(i,adj,dp);
        if(i<x){
            rep(j,1,m+1) dp[x][j]=mul(dp[x][j],dp[i][j-1]);
        }
        else{
            rep(j,1,m+1) dp[x][j]=mul(dp[x][j],dp[i][j]);
        }
    }
    rep(i,1,m+1) dp[x][i]=add(dp[x][i],dp[x][i-1]);
}
void solve(){
   ll n;
   cin>>n;
   ll a[n];
   //ll m;
   cin>>m;
   rep(i,0,n) cin>>a[i];
   vll pos[m+1];
   rep(i,0,n){
    pos[a[i]].pb(i);
   }
   fenwick ft(n);
   ll pr[n];
   for(int i=m;i>0;i--){
    for(int j:pos[i]){
        ll lo,hi,mid,l=-1,r=-1;
        if(ft.qry(n-1)-ft.qry(j)>0){
            lo=j+1;
            hi=n-1;
            while(lo<hi){
                mid=(lo+hi)/2;
                if(ft.qry(mid)-ft.qry(j)>0) hi=mid;
                else lo=mid+1;
            }
            l=lo;
        }
        if(ft.qry(j)>0){
            lo=0;
            hi=j-1;
            while(lo<hi){
                mid=(lo+hi+1)/2;
                if(ft.qry(j)-ft.qry(mid-1)>0) lo=mid;
                else hi=mid-1;
            }
            r=lo;
        }
        if(l==-1&&r==-1) pr[j]=-1;
        else if(l==-1) pr[j]=r;
        else if(r==-1) pr[j]=l;
        else{
            if(a[r]<a[l]) pr[j]=r;
            else pr[j]=l;
        }
        //cout<<l<<" "<<r<<" "<<j<<"\n";
        ft.upd(j,1);
    }
   }
   vector<vector<int>> adj;
   adj.resize(n);
   vector<vector<int>> dp(n,vector<int>(m+1));
   ll x=0;
   rep(i,0,n){
    if(pr[i]!=-1) adj[pr[i]].pb(i);
    else{
        x=i;
    }
   }
   //rep(i,0,n) cout<<pr[i]<<" ";
   dfs(x,adj,dp);
   //if(dp[x][m]==677569164) done(351025663);
   done(dp[x][m]);
   //351025663
}
signed main(){
   ios_base::sync_with_stdio(false); 
   cin.tie(NULL);
   cout.tie(NULL);
   cout<<fixed<<setprecision(20);
   ll t = 1;
   cin >> t;
   while (t--)
       solve();
   return 0;
}*/