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

Solution 1 (runtime: 234 ms):
#include <bits/stdc++.h>

using namespace std;
#define maxn 200002
//#define int long long
#define ll long long
//int arr[maxn] = {};

bool flag[maxn] = {};

void solve() {
    int n = 0;
    cin >> n;
    vector<vector<int>> v(n + 1);
    for (int i = 1; i <= n; ++i) flag[i] = false;
    for (int i = 1, v1, v2; i < n; ++i) {
        cin >> v1 >> v2;
        v[v1].push_back(v2);
        v[v2].push_back(v1);
    }

    int maxNum = 0;
    vector<int> maxNumNode;
    for (int i = 1; i <= n; ++i) {
        if (v[i].size() > maxNum) {
            maxNum = v[i].size();
            maxNumNode.clear();
            maxNumNode.push_back(i);
        } else if (v[i].size() == maxNum) {
            maxNumNode.push_back(i);
        }
    }
    if (maxNumNode.size() == 1) {
        int secNum = 0;
        vector<int> secNumNode;
        for (int i = 1; i <= n; ++i) {
            if (v[i].size() > secNum && v[i].size() != maxNum) {
                secNum = v[i].size();
                secNumNode.clear();
                secNumNode.push_back(i);
            } else if (v[i].size() == secNum) {
                secNumNode.push_back(i);
            }
        }
        bool expect = false;
        for (auto i : v[maxNumNode[0]]) {
            flag[i] = true;
        }
        for (int i = 0; i < secNumNode.size(); ++i) {
            if (!flag[secNumNode[i]]) {
                expect = true;
                break;
            }
        }
        if (expect) {
            cout << secNum + maxNum - 1 << endl;
        } else {
            cout << secNum + maxNum - 2 << endl;
        }

    } else {
        bool expect = false;
        for (int i = 0; i < maxNumNode.size(); ++i) {
            if (expect) break;
            for (auto j : v[maxNumNode[i]]) {
                flag[j] = true;
            }
            for (int j = i + 1; j < maxNumNode.size(); ++j) {
                if (!flag[maxNumNode[j]]) {
                    expect = true;
                    break;
                }
            }
            for (auto j : v[maxNumNode[i]]) {
                flag[j] = false;
            }
        }
        if (expect) {
            cout << maxNum + maxNum - 1 << endl;
        } else {
            cout << maxNum + maxNum - 2 << endl;
        }
    }

}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
//    solve();
    int t; cin >> t;
    while (t--) solve();
}

Solution 2 (runtime: 265 ms):
#include <bits/stdc++.h>
#define fastio          ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fileio          freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define ll              long long
#define vi              vector<ll>
#define pb              push_back
#define all(x)          x.begin(), x.end()
#define endl            '\n'

using namespace std;
ll MOD = 998244353;
const ll MAX = 1e9;

void solve() {
    int n;
    cin >> n;

    vector<vector<int>> adj(n);
    vector<int> deg(n, 0);
    
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }

    vector<pair<int, int>> list;
    for (int i = 0; i < n; i++) {
        list.push_back({deg[i], i});
    }

    sort(list.begin(), list.end(), greater<pair<int,int>>());

    int idx1 = list[0].second;
    int idx2 = (n > 1) ? list[1].second : -1;

    int ans = 0;

    for (int f : {idx1, idx2}) {
        if (f == -1) continue;
        sort(all(adj[f]));
        for (int s = 0; s < n; s++) {
            if (s == f) continue;

            int c = deg[f] + deg[s] - 1;
            
            int left = 0, right = adj[f].size() - 1;

            while(left <= right) {
                int mid = (left + right) / 2;
                if(adj[f][mid] == s) {
                    c--;
                    break;
                }
                else if(adj[f][mid] > s) {
                    right = mid - 1;
                }
                else {
                    left = mid + 1;
                }
            }

            ans = max(ans, c);
        }
    }

    cout << ans << endl;
}

int main() {
    fastio
    #ifndef ONLINE_JUDGE
        fileio
    #endif
    int t = 1;
    cin >> t;
    // int i = 1;
    while (t--) {
        // cout << "Case " << i++ << ": ";
        solve();
    }
    return 0;
}