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

Solution 1 (runtime: 561 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ll long long
#define uin unsigned int
#define ull unsigned long long
#define ld long double
#define fl(i, w, n) for (ll i = w; i < n; i++)
#define rfl(i, w, n) for (ll i = w; i >= n; i--)
#define F first
#define S second
#define B begin()
#define E end()
#define EB emplace_back
#define PB push_back
#define PRES(c, x) ((c).find(x) != (c).end())
#define dbg(...) ;
#define debug(...) ;
typedef vector<string> vs;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define um(x, y) unordered_map<x, y>
#define us(x) unordered_set<x>
#define mpl map lii
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define nl << "\n";
#define nll cout << "\n";
#define Yes cout << "Yes" << "\n";
#define No cout << "No" << "\n";
#define PI 3.1415926535897932384626
#define SET(n) cout << fixed << setprecision(n)
#define get(s) getline(cin, s)
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define ctz __builtin_ctz
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr);

#define INF 1e18
#define vi vector<ll>
#define vb vector<bool>
#define vc vector<char>
#define vvi vector<vector<ll>>
#define vvc vector<vector<char>>
#define vvb vector<vector<bool>>
#define vpi vector<pair<ll, ll>>
#define mi map<ll, ll>
#define mpi map<pair<ll, ll>, ll>
#define pr pair<ll, ll>
#define mp make_pair
#define triplet pair<ll, pair<ll, ll>>
#define ff first
#define ss second
#define pb push_back
#define MAX ll_MAX
#define MIN ll_MIN
#define fr(i, n) for (ll i = 0; i < n; i++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
#define read(x)                                                                \
  ll x;                                                                        \
  cin >> x
#define pD(x, y) cout << fixed << setprecision(y) << x
#define google(tno) cout << "Case #" << tno << ": "
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 998244353
#define LMAX 9223372036854775807
#define upperToLower(x) char(x | ' ')
#define lowerToUpper(x) char(x & '_')
const ll MOD = 1e9 + 7;
const ll GN = 1e5 + 10;

void _prll(ll t) { cerr << t; }
void _prll(string t) { cerr << t; }
void _prll(char t) { cerr << t; }
void _prll(ld t) { cerr << t; }
void _prll(double t) { cerr << t; }

template <class T, class V> void _prll(pair<T, V> p);
template <class T> void _prll(vector<T> v);
template <class T> void _prll(set<T> v);
template <class T, class V> void _prll(map<T, V> v);
template <class T> void _prll(multiset<T> v);
template <class T, class V> void _prll(pair<T, V> p) {
  cerr << "{";
  _prll(p.ff);
  cerr << ",";
  _prll(p.ss);
  cerr << "}";
}
template <class T> void _prll(vector<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _prll(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T> void _prll(set<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _prll(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T> void _prll(multiset<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _prll(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T, class V> void _prll(map<T, V> v) {
  cerr << "[ ";
  for (auto i : v) {
    _prll(i);
    cerr << " ";
  }
  cerr << "]";
}

vi fact(GN, 1);
vb sieve(GN, 1);
vi dsieve(GN, 0);

bool isPrime(ll n) {
  if (n == 1)
    return 0;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
void primeSieve() {
  sieve[0] = sieve[1] = false;
  for (ll i = 2; i * i < GN; i++) {
    if (sieve[i]) {
      for (ll j = i * i; j < GN; j += i)
        sieve[j] = false;
    }
  }
}
void divSieve() {
  for (ll i = 1; i < GN; i++) {
    for (ll j = i; j < GN; j = j + i) {
      ++dsieve[j];
    }
  }
}
void factSieve() {
  for (ll i = 1; i < GN; i++)
    fact[i] = (fact[i - 1] % MOD * 1LL * i % MOD) % MOD;
}
mi primeFactorise(ll n) {
  mi mpp;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ll cnt = 0;
      while (n % i == 0) {
        ++cnt;
        n = n / i;
      }
      mpp[i] = cnt;
    }
  }
  if (n > 1)
    mpp[n] = 1;
  return mpp;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll binexp(ll x, ll n) {
  ll a = x;
  ll prod = 1;
  while (n) {
    if (n % 2 == 1)
      prod = prod * a;
    a = a * a;
    n = n / 2;
  }
  return prod;
}
ll modexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1LL;
  while (b > 0) {
    if (b & 1)
      res = (res % m * 1LL * a % m) % m;
    a = (a % m * 1LL * a % m) % m;
    b >>= 1;
  }
  return res % m;
}
ll modadd(ll a, ll b, ll m) { return (a % m + b % m) % m; }
ll modsub(ll a, ll b, ll m) { return (a % m - b % m + m) % m; }
ll modmul(ll a, ll b, ll m) { return (a % m * 1LL * b % m) % m; }
ll modinv(ll a, ll m) { return modexp(a, m - 2, m); }
ll moddiv(ll a, ll b, ll m) { return (a % m * 1LL * modinv(b, m) % m) % m; }
ll C(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  if (r == 1)
    return n;
  ll nmr = fact[n] % MOD;
  ll dmr = (fact[r] % MOD * 1LL * fact[n - r] % MOD) % MOD;
  return (nmr % MOD * 1LL * modinv(dmr, MOD) % MOD) % MOD;
}

bool compare(pair<ll, ll> &p1, pair<ll, ll> &p2) {
  return p1.second > p2.second;
}

void solve() {
  ll n;
  cin >> n;
  vector<vector<ll>> a(n);
  for (ll i = 0; i < n - 1; ++i) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    a[u].push_back(v);
    a[v].push_back(u);
  }
  ll ans = 0;
  for (ll i = 0; i < n; ++i) {
    for (ll j : a[i]) {
      ans = max(ans, (ll)(a[i].size() + a[j].size()) - 2);
    }
  }
  multiset<ll> ms;
  for (ll i = 0; i < n; ++i) {
    ms.insert(a[i].size());
  }
  for (ll i = 0; i < n; ++i) {
    ms.erase(ms.find(a[i].size()));
    for (ll j : a[i]) {
      ms.erase(ms.find(a[j].size()));
    }
    if (!ms.empty()) {
      ans = max(ans, (ll)a[i].size() + *ms.rbegin() - 1);
    }
    ms.insert(a[i].size());
    for (ll j : a[i]) {
      ms.insert(a[j].size());
    }
  }
  cout << ans << "\n";
}
signed main() {
  fast;
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


Solution 2 (runtime: 265 ms):
#include<bits/stdc++.h>
using namespace std;

#define int long long
//
#define pb push_back
#define ppb pop_back
#define pii pair<int,int>
#define vi  vector<int>
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define vvi(v,n,m,k) vector<vi>v(n,vi(m,k));
#define endl "\n"
#define MOD 998244353      
#define print(v) for(int i=0;i<v.size();i++){cout<<v[i]<<" ";} cout<<endl;
#define printl(v) for(auto it :v){cout<<it<<endl;}
#define printp(v) for(auto it : v){cout<<it.ff<<" "<<it.ss<<endl;}
#define printvv(v) {for(int i=0;i<v.size();i++){print(v[i]);}}
//
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)

void solve(){
    int n;
    cin>>n;
    vector<vi>adj(n);
    for(int i=0;i<n-1;i++){
        int x,y;
        cin>>x>>y;
        x--;
        y--;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    vector<pii>v;
    vi incoming(n,0);
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
        v.pb({incoming[i],i});
    }
    sort(all(v));
    int cnt=v[n-1].ff;
    int node=v[n-1].ss;
    incoming[node]=-1;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    int maxi=cnt;
    for(int i=0;i<n;i++){
        incoming[i]=adj[i].size();
    }
    cnt=v[n-2].ff;
    node=v[n-2].ss;
    incoming[node]=-1;
    for(int i=0;i<adj[node].size();i++){
        int ver=adj[node][i];
        incoming[ver]--;
    }
    sort(all(incoming));
    cnt+=incoming[n-1]-1;
    cout<<max(cnt,maxi)<<endl;
}


signed main(){
    fastio();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}