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

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <chrono>
#include <bits/stdc++.h>
#include <vector>
#include <limits.h>
#include <cmath>
#include <algorithm>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define newl cout << "\n";
#define vi vector<int>
#define vpl vector<pair<ll int, ll int>>
#define vpi vector<pair<int, int>>
#define vl vector<ll int>
#define pl pair<ll int, ll int>
#define ml map<ll int, ll int>
#define show(v) cout<<v<<"\n";
#define all(v) v.begin(),v.end()
#define print(v) for(auto it : v){cout << it << ' ';}newl
#define fors(n) for (ll int i = 0; i < n; i++)
#define loop(i,st,en) for (ll int i = st; i < en; i++)
#define loopdec(i,st,en) for(ll int i=st;i>en;i--)
#define inv(v,n) fors(n) cin>>v[i];
#define ina(a,n) fors(n) cin>>a[i];
#define setBits(x) __builtin_popcount(x)
#define setBitsll(x) __builtin_popcountll(x)
#define py show("YES");
#define pn show("NO");
using namespace std;
const int MOD = 998244353;

//policy data structure
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional>
using namespace __gnu_pbds;  
  
typedef tree<ll, null_type, less<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;
 
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_multiset;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

class DSU{
public:
    int parent[10001];
    int size[10001];
    void make(int v){
        parent[v] = v;
        size[v] = 1;
        return;
    }
    int findParent(int v){
        if(parent[v]==v){
            return v;
        }
        //Path compression
        return parent[v] = findParent(parent[v]);
    }
    void Union(int a, int b){
        a = findParent(a);
        b = findParent(b);
        if(a!=b){
            //union by size;
            if(size[a]<size[b]){
                swap(a,b);
            } 
            parent[b] = a;
            size[a] += size[b];
        }
    }
};

ll int binsearch(ll int arr[],ll int l,ll int r,ll int x)
{
    while (l <= r) {
        ll int m = l + (r - l) / 2;

        if (arr[m] == x)
            return m;

        if (arr[m] < x)
            l = m + 1;

        else
            r = m - 1;
    }
    return -1;
}

int getBit(ll int num, ll int pos){
    return num & (1<<pos);
}

int setBit(ll int num, ll int pos){
    num |= (1<<pos);
    return num;
}

ll int add(ll int x, ll int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
} 
 
ll int mul(ll int x, ll int y) {
    x %= MOD;
    y %= MOD;
  return x * 1LL * y % MOD;
}

ll int pow(ll int a, ll int b){
    if(b==0){
        return 1;
    }
    else{
        if(b%2==0){
            return mul(pow(a,b/2),pow(a,b/2));
        }
        else{
            return mul(a,mul(pow(a,b/2),pow(a,b/2)));
        }
    }
}

ll int negmod(ll int a,ll int m){
    return ((a%m + m) % m);
}

long long power(long long a, long long b) {
    long long result = 1;
    while(b) {
        if (b & 1) 
            result = mul(result, a);
        a = mul(a,a);
        b >>= 1;
    }
    return result;
}

vector<bool> sieve(ll int n) {
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = isPrime[1] = false;
 
    ll int sqrtN = sqrt(n);
    for (ll int i = 2; i <= sqrtN; ++i) {
        if (isPrime[i]) {
            for (ll int j = i * i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
 
    return isPrime;
}

ll int gcd (ll int a,ll int b) {
    if (b == 0)
        return a;
    else
        return gcd (b, a % b);
}

//giving minimum prime divisor for a particular index i
vector<ll int> precalc(ll int N) {
    vector<ll int> v(N);
	for(ll int i=0;i<N;i++){
        v[i] = i;
    }
	for (ll int p = 2; p < N; p++) {
		if (v[p] != p)
			continue;
		for (ll int d = 2 * p; d < N; d += p)
			v[d] = min(v[d], p);
	}
    return v;
}

ll int digsum(ll int a) { 
    ll int sum = 0;
    while(a){
        sum += a%10;
        a /= 10;
    } 
    return sum;
}

ll int calculate(ll int p, ll int q) 
{ 
    ll int mod = 1e9 + 7, expo; 
    expo = mod - 2;  
    while (expo) { 
 
        if (expo & 1) { 
            p = (p * q) % mod; 
        } 
        q = (q * q) % mod; 
 
        expo >>= 1; 
    } 
    return p; 
}

int ask(int x, int y) {
	printf("? %d %d\n", x, y);
	fflush(stdout);
	scanf("%d", &x);
	return x;
}

void give(int x, int y) {
	printf("! %d %d\n", x, y);
	fflush(stdout);
}

void dfs(ll int node, ll int parent, vector<vector<ll int>> &edges, vector<ll int> &par, vector<ll int> &numofnodes, vector<ll int> &maxnumofnodes){
    par[node] = parent;
    for(auto &child: edges[node]){
        if(child==parent) continue;
        dfs(child, node, edges, par, numofnodes, maxnumofnodes);
        numofnodes[node]++;
        maxnumofnodes[node] = max(numofnodes[child], maxnumofnodes[node]);
        maxnumofnodes[node] = max(maxnumofnodes[child], maxnumofnodes[node]);
    }
}

void dfs1(ll int node, ll int parent, vector<vector<ll int>> &edges, vector<ll int> &par, vector<ll int> &numofnodes, vector<ll int> &maxnumofnodes, vector<ll int> &mx1num, ll int &ans){
    ll int a1 = numofnodes[node];
    ll int a2 = 0;
    ll int mx1 = 0;
    ll int c1 = 0;
    ll int mx2 = 0;
    for(auto &child: edges[node]){
        if(child==parent) continue;
        mx1 = max(mx1, mx1num[child]);
    }
    for(auto &child: edges[node]){
        if(child==parent) continue;
        if(mx1==mx1num[child]) c1++;
    }
    if(c1>=2){
        ans = max(ans, mx1 + mx1 + 1);
    }else{
        for(auto &child: edges[node]){
            if(child==parent) continue;
            if(mx1==mx1num[child]) continue;
            mx2 = max(mx2, mx1num[child]);
        }
        ans = max(ans, mx1 + mx2 + 1);
    }
    for(auto &child: edges[node]){
        if(child==parent) continue;
        dfs1(child, node, edges, par, numofnodes, maxnumofnodes, mx1num, ans);
        if(node==1) ans = max(ans, numofnodes[node] - 1 + numofnodes[child]);
        else ans = max(ans, numofnodes[node] + numofnodes[child]);
        a2 = max(a2, maxnumofnodes[child]);
    }
    if(a2==0){
        ans = max(ans, a1 - 1);
    }else{
        ans = max(ans, a1 + a2);
    }
}

void solve(){
    ll int n;
    cin>>n;
    vector<vector<ll int>> edges(n+1, vector<ll int>());
    fors(n-1){
        ll int a;
        ll int b;
        cin>>a>>b;
        edges[a].pb(b);
        edges[b].pb(a);
    }
    if(n==2){
        show(0);
        return;
    }
    vector<ll int> par(n+1, 0);
    vector<ll int> numofnodes(n+1, 0);
    vector<ll int> maxnumofnodes(n+1, 0);
    vector<ll int> mx1num(n+1, 0);
    dfs(1, -1, edges, par, numofnodes, maxnumofnodes);
    // print(par);
    // print(numofnodes);
    // print(maxnumofnodes);
    fors(n+1){
        mx1num[i] = max(mx1num[i], numofnodes[i]);
        mx1num[i] = max(mx1num[i], maxnumofnodes[i]);
    }
    ll int ans = 0;
    dfs1(1, -1, edges, par, numofnodes, maxnumofnodes, mx1num, ans);
    cout<<ans<<"\n";
}

int main() {
    fastio();
    auto start1 = chrono::high_resolution_clock::now();
    ll int t;
    cin>>t;
    while(--t>=0){
        solve();
    }
    auto stop1 = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::milliseconds>(stop1 - start1);
    // cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
}

Submission (verdict: Wrong answer on test 2):
#include <iostream>
#include <chrono>
#include <bits/stdc++.h>
#include <vector>
#include <limits.h>
#include <cmath>
#include <algorithm>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define newl cout << "\n";
#define vi vector<int>
#define vpl vector<pair<ll int, ll int>>
#define vpi vector<pair<int, int>>
#define vl vector<ll int>
#define pl pair<ll int, ll int>
#define ml map<ll int, ll int>
#define show(v) cout<<v<<"\n";
#define all(v) v.begin(),v.end()
#define print(v) for(auto it : v){cout << it << ' ';}newl
#define fors(n) for (ll int i = 0; i < n; i++)
#define loop(i,st,en) for (ll int i = st; i < en; i++)
#define loopdec(i,st,en) for(ll int i=st;i>en;i--)
#define inv(v,n) fors(n) cin>>v[i];
#define ina(a,n) fors(n) cin>>a[i];
#define setBits(x) __builtin_popcount(x)
#define setBitsll(x) __builtin_popcountll(x)
#define py show("YES");
#define pn show("NO");
using namespace std;
const int MOD = 998244353;

//policy data structure
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>  
#include <functional>
using namespace __gnu_pbds;  
  
typedef tree<ll, null_type, less<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_set;
 
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,  
            tree_order_statistics_node_update>  
    ordered_multiset;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

class DSU{
public:
    int parent[10001];
    int size[10001];
    void make(int v){
        parent[v] = v;
        size[v] = 1;
        return;
    }
    int findParent(int v){
        if(parent[v]==v){
            return v;
        }
        //Path compression
        return parent[v] = findParent(parent[v]);
    }
    void Union(int a, int b){
        a = findParent(a);
        b = findParent(b);
        if(a!=b){
            //union by size;
            if(size[a]<size[b]){
                swap(a,b);
            } 
            parent[b] = a;
            size[a] += size[b];
        }
    }
};

ll int binsearch(ll int arr[],ll int l,ll int r,ll int x)
{
    while (l <= r) {
        ll int m = l + (r - l) / 2;

        if (arr[m] == x)
            return m;

        if (arr[m] < x)
            l = m + 1;

        else
            r = m - 1;
    }
    return -1;
}

int getBit(ll int num, ll int pos){
    return num & (1<<pos);
}

int setBit(ll int num, ll int pos){
    num |= (1<<pos);
    return num;
}

ll int add(ll int x, ll int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
} 
 
ll int mul(ll int x, ll int y) {
    x %= MOD;
    y %= MOD;
  return x * 1LL * y % MOD;
}

ll int pow(ll int a, ll int b){
    if(b==0){
        return 1;
    }
    else{
        if(b%2==0){
            return mul(pow(a,b/2),pow(a,b/2));
        }
        else{
            return mul(a,mul(pow(a,b/2),pow(a,b/2)));
        }
    }
}

ll int negmod(ll int a,ll int m){
    return ((a%m + m) % m);
}

long long power(long long a, long long b) {
    long long result = 1;
    while(b) {
        if (b & 1) 
            result = mul(result, a);
        a = mul(a,a);
        b >>= 1;
    }
    return result;
}

vector<bool> sieve(ll int n) {
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = isPrime[1] = false;
 
    ll int sqrtN = sqrt(n);
    for (ll int i = 2; i <= sqrtN; ++i) {
        if (isPrime[i]) {
            for (ll int j = i * i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
 
    return isPrime;
}

ll int gcd (ll int a,ll int b) {
    if (b == 0)
        return a;
    else
        return gcd (b, a % b);
}

//giving minimum prime divisor for a particular index i
vector<ll int> precalc(ll int N) {
    vector<ll int> v(N);
	for(ll int i=0;i<N;i++){
        v[i] = i;
    }
	for (ll int p = 2; p < N; p++) {
		if (v[p] != p)
			continue;
		for (ll int d = 2 * p; d < N; d += p)
			v[d] = min(v[d], p);
	}
    return v;
}

ll int digsum(ll int a) { 
    ll int sum = 0;
    while(a){
        sum += a%10;
        a /= 10;
    } 
    return sum;
}

ll int calculate(ll int p, ll int q) 
{ 
    ll int mod = 1e9 + 7, expo; 
    expo = mod - 2;  
    while (expo) { 
 
        if (expo & 1) { 
            p = (p * q) % mod; 
        } 
        q = (q * q) % mod; 
 
        expo >>= 1; 
    } 
    return p; 
}

int ask(int x, int y) {
	printf("? %d %d\n", x, y);
	fflush(stdout);
	scanf("%d", &x);
	return x;
}

void give(int x, int y) {
	printf("! %d %d\n", x, y);
	fflush(stdout);
}

void dfs(ll int node, ll int parent, vector<vector<ll int>> &edges, vector<ll int> &par, vector<ll int> &numofnodes, vector<ll int> &maxnumofnodes){
    par[node] = parent;
    for(auto &child: edges[node]){
        if(child==parent) continue;
        dfs(child, node, edges, par, numofnodes, maxnumofnodes);
        numofnodes[node]++;
        maxnumofnodes[node] = max(numofnodes[child], maxnumofnodes[node]);
        maxnumofnodes[node] = max(maxnumofnodes[child], maxnumofnodes[node]);
    }
}

void dfs1(ll int node, ll int parent, vector<vector<ll int>> &edges, vector<ll int> &par, vector<ll int> &numofnodes, vector<ll int> &maxnumofnodes, vector<ll int> &mx1num, ll int &ans){
    ll int a1 = numofnodes[node];
    ll int a2 = 0;
    if((int)edges[node].size()>=2){
        ll int mx1 = 0;
        ll int c1 = 0;
        ll int mx2 = 0;
        for(auto &child: edges[node]){
            if(child==parent) continue;
            mx1 = max(mx1, mx1num[child]);
        }
        for(auto &child: edges[node]){
            if(child==parent) continue;
            if(mx1==mx1num[child]) c1++;
        }
        if(c1>=2){
            ans = max(ans, mx1 + mx1 + 1);
        }else{
            for(auto &child: edges[node]){
                if(child==parent) continue;
                if(mx1==mx1num[child]) continue;
                mx2 = max(mx2, mx1num[child]);
            }
            ans = max(ans, mx1 + mx2 + 1);
        }
    }
    for(auto &child: edges[node]){
        if(child==parent) continue;
        dfs1(child, node, edges, par, numofnodes, maxnumofnodes, mx1num, ans);
        if(node==1) ans = max(ans, numofnodes[node] - 1 + numofnodes[child]);
        else ans = max(ans, numofnodes[node] + numofnodes[child]);
        a2 = max(a2, maxnumofnodes[child]);
    }
    if(a2==0){
        ans = max(ans, a1 - 1);
    }else{
        ans = max(ans, a1 + a2);
    }
}

void solve(){
    ll int n;
    cin>>n;
    vector<vector<ll int>> edges(n+1, vector<ll int>());
    fors(n-1){
        ll int a;
        ll int b;
        cin>>a>>b;
        edges[a].pb(b);
        edges[b].pb(a);
    }
    if(n==2){
        show(0);
        return;
    }
    vector<ll int> par(n+1, 0);
    vector<ll int> numofnodes(n+1, 0);
    vector<ll int> maxnumofnodes(n+1, 0);
    vector<ll int> mx1num(n+1, 0);
    dfs(1, -1, edges, par, numofnodes, maxnumofnodes);
    // print(par);
    // print(numofnodes);
    // print(maxnumofnodes);
    fors(n+1){
        mx1num[i] = max(mx1num[i], numofnodes[i]);
        mx1num[i] = max(mx1num[i], maxnumofnodes[i]);
    }
    ll int ans = 0;
    dfs1(1, -1, edges, par, numofnodes, maxnumofnodes, mx1num, ans);
    cout<<ans<<"\n";
}

int main() {
    fastio();
    auto start1 = chrono::high_resolution_clock::now();
    ll int t;
    cin>>t;
    while(--t>=0){
        solve();
    }
    auto stop1 = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::milliseconds>(stop1 - start1);
    // cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
}