Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#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;
}

bool isEdgesPresent(vector<vector<bool>> &hash, ll u, ll v) {
  if (u == -1 || v == -1) {
    return false;
  }
  return (hash[u][v] || hash[v][u]);
}

void printVector(const vector<ll> &vec) {
  for (const auto &v : vec) {
    cout << v + 1 << " ";
  }
  cout << endl;
}
void dfs(ll node, vector<vector<ll>> &adj, vector<bool> &visited) {
  visited[node] = true;

  for (auto neighbor : adj[node]) {
    if (!visited[neighbor]) {
      dfs(neighbor, adj, visited);
    }
  }
}

ll findConnectedComponents(ll n, vector<pair<ll, ll>> &edges) {
  vector<vector<ll>> adj(n + 1);
  for (auto edge : edges) {
    adj[edge.first].push_back(edge.second);
    adj[edge.second].push_back(edge.first);
  }

  vector<bool> visited(n + 1, false);
  ll componentCount = 0;

  for (ll i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i, adj, visited);
      componentCount++;
    }
  }

  return componentCount;
}

void solve() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> edges(n - 1);
  vector<vector<bool>> hash(n + 1, vector<bool>(n + 1, false));
  map<ll, ll> indegree;

  for (int i = 0; i < n - 1; i++) {
    cin >> edges[i].first >> edges[i].second;
    edges[i].first--;
    edges[i].second--;
    indegree[edges[i].first]++;
    indegree[edges[i].second]++;
    hash[edges[i].first][edges[i].second] = true;
    hash[edges[i].second][edges[i].first] = true;
  }

  vector<pair<ll, ll>> vp;
  for (auto it : indegree) {
    vp.push_back({it.first, it.second});
  }

  sort(vp.begin(), vp.end(), compare);

  vector<ll> removedVertex(2, -1);

  removedVertex[0] = vp.back().first;

  ll secondRemoved = -1;
  for (int i = vp.size() - 2; i >= 0; i--) {
    if (!isEdgesPresent(hash, removedVertex[0], vp[i].first)) {
      removedVertex[1] = vp[i].first;
      break;
    } else {
      secondRemoved = vp[i].first;
    }
  }
  if (removedVertex[1] == -1) {
    removedVertex[1] = secondRemoved;
  }

  vector<pair<ll, ll>> newEdges;
  for (int i = 0; i < edges.size(); i++) {
    if (edges[i].first == removedVertex[0] ||
        edges[i].first == removedVertex[1] ||
        edges[i].second == removedVertex[0] ||
        edges[i].second == removedVertex[1]) {
      continue;
    } else {
      newEdges.push_back(edges[i]);
    }
  }
  if (edges.size() == 1) {
    cout << 0 << endl;
    return;
  }

  ll cnt = findConnectedComponents(n, newEdges) - 2;
  cout << cnt << endl;
}
signed main() {
  fast;
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


Submission (verdict: Accepted):
#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();
  }
}