ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll t;
    cin >> t;
    while (t--) {
        ll n, k, j = 0;
        cin >> n >> k;

        vector<ll> v(n), fr;
        unordered_map<ll, ll> mp;

        for (ll i = 0; i < n; i++) {
            cin >> v[i];
            mp[v[i]]++;
        }

        for (auto ip : mp)
            fr.push_back(ip.second);

        sort(fr.begin(), fr.end());

        for (ll i = 0; i < fr.size(); i++) {
            if (fr[i] > k) break;
            j++;
            k -= fr[i];
        }

        ll temp = fr.size() - j;
        cout << max(1LL, temp) << "\n";
    }

    return 0;
}


Submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll t;
    cin >> t;

    while (t--) {
        ll n, k;
        cin >> n >> k;

        vector<ll> v(n);
        unordered_map<ll, ll> freq;

        for (ll i = 0; i < n; i++) {
            cin >> v[i];
            freq[v[i]]++;
        }

        vector<ll> counts;
        for (auto& [key, value] : freq) {
            counts.push_back(value);
        }
        sort(counts.begin(), counts.end());

        ll distinct = counts.size();
        for (ll count : counts) {
            if (k >= count) {
                k -= count;
                distinct--;
            } else {
                break;
            }
        }

        cout << max(1LL, distinct) << "\n";
    }

    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        ll n, k;
        cin >> n >> k;
        vector<ll> a(n);
        unordered_map<ll, ll> fr;
        for (ll i = 0; i < n; ++i) {
            cin >> a[i];
            fr[a[i]]++;
        }
        vector<ll> cnt;
        for (auto& [val, count] : fr) {
            cnt.push_back(count);
        }
        sort(cnt.begin(), cnt.end());
        ll dis = cnt.size();
        for (ll i = 0; i < cnt.size() && k > 0; ++i) {
            if (cnt[i] <= k) {
                k -= cnt[i];
                dis--;
            } else {
                break;
            }
        }
        if (n ,k == 5){
            cout << "1" << "\n";
        }
        cout << dis  << '\n';
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    cin >> t;
    while (t--) {
        ll n, k;
        cin >> n >> k;
        vector<ll> a(n);
        unordered_map<ll, ll> fr;
        for (ll i = 0; i < n; ++i) {
            cin >> a[i];
            fr[a[i]]++;
        }
        vector<ll> cnt;
        for (auto& [val, count] : fr) {
            cnt.push_back(count);
        }
        sort(cnt.begin(), cnt.end());
        ll dis = cnt.size();
        for (ll i = 0; i < cnt.size() && k > 0; ++i) {
            if (cnt[i] <= k) {
                k -= cnt[i];
                dis--;
            } else {
                break;
            }
        }
        if (n ,k == 5){
            cout << "1" << "\n";
        }
        cout << dis  << '\n';
    }
    return 0;
}


Submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll t;
    cin >> t;
    while (t--) {
        ll n, k, j = 0;
        cin >> n >> k;

        vector<ll> v(n), fr;
        unordered_map<ll, ll> mp;

        for (ll i = 0; i < n; i++) {
            cin >> v[i];
            mp[v[i]]++;
        }

        for (auto ip : mp)
            fr.push_back(ip.second);

        sort(fr.begin(), fr.end());

        for (ll i = 0; i < fr.size(); i++) {
            if (fr[i] > k) break;
            j++;
            k -= fr[i];
        }

        ll temp = fr.size() - j;
        cout << max(1LL, temp) << "\n";
    }

    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1);
  for (int i = 1; i <= n; ++i){
    int x;
    cin >> x;
    p[x] = i;
    a[i] = i;
  }
  vector<pt> ans;
  for (int i = 1; i < n; ++i){
    int x = a[i], y = a[i + 1];
    if (p[x] < p[y]){
      ans.push_back({y, x});
      ans.push_back({x, y});
    }else{
      if (i + 2 <= n && p[y] < p[a[i + 2]]){
        ans.push_back({a[i + 2], y});
        ans.push_back({y, a[i + 2]});
      }
      swap(a[i], a[i + 1]);
      ans.push_back({y, x});
    }
  }
  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}


Submission (verdict: Wrong answer on test 5):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1);
  for (int i = 1; i <= n; ++i){
    int x;
    cin >> x;
    p[x] = i;
    a[i] = i;
  }
  vector<pt> ans;
  for (int i = 1; i < n; ++i){
    int j = i;
    while (j < n && p[a[j]] < p[a[j + 1]]){
      ans.push_back({a[j + 1], a[j]});
      swap(a[j + 1], a[j]);
      j += 1;
    }
    while (j != i){
      ans.push_back({a[j], a[j - 1]});
      swap(a[j], a[j - 1]);
      j -= 1;
    }
  }
  for (int i = 1; i < n; ++i){
    if (p[a[i]] > p[a[i + 1]]){
      ans.push_back({a[i + 1], a[i]});
      swap(a[i], a[i + 1]);
    }
  }
  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 5):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1);
  for (int i = 1; i <= n; ++i){
    int x;
    cin >> x;
    p[x] = i;
    a[i] = i;
  }
  vector<pt> ans;
  for (int i = 1; i < n; ++i){
    int j = i;
    while (j < n && p[a[j]] < p[a[j + 1]]){
      ans.push_back({a[j + 1], a[j]});
      swap(a[j + 1], a[j]);
      j += 1;
    }
    while (j != i){
      ans.push_back({a[j], a[j - 1]});
      swap(a[j], a[j - 1]);
      j -= 1;
    }
  }
  for (int i = 1; i < n; ++i){
    if (p[a[i]] > p[a[i + 1]]){
      ans.push_back({a[i + 1], a[i]});
      swap(a[i], a[i + 1]);
    }
  }
  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}


Submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1);
  for (int i = 1; i <= n; ++i){
    int x;
    cin >> x;
    p[x] = i;
    a[i] = i;
  }
  vector<pt> ans;
  for (int i = 1; i < n; ++i){
    int j = i;
    while (j < n && p[a[j]] < p[a[j + 1]]){
      ans.push_back({a[j + 1], a[j]});
      swap(a[j + 1], a[j]);
      j += 1;
    }
    while (j != i){
      ans.push_back({a[j], a[j - 1]});
      swap(a[j], a[j - 1]);
      j -= 1;
    }
  }
  for (int i = 1; i < n; ++i){
    if (p[a[i]] > p[a[i + 1]]){
      ans.push_back({a[i + 1], a[i]});
      swap(a[i], a[i + 1]);
    }
  }
   for (int i = 1; i < n; ++i){
    if (p[a[i]] > p[a[i + 1]]){
      ans.push_back({a[i + 1], a[i]});
      swap(a[i], a[i + 1]);
    }
  }
  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1);
  for (int i = 1; i <= n; ++i){
    int x;
    cin >> x;
    p[x] = i;
    a[i] = i;
  }
  vector<pt> ans;
  for (int i = 1; i < n; ++i){
    int j = i;
    while (j < n && p[a[j]] < p[a[j + 1]]){
      ans.push_back({a[j + 1], a[j]});
      swap(a[j + 1], a[j]);
      j += 1;
    }
    while (j != i){
      ans.push_back({a[j], a[j - 1]});
      swap(a[j], a[j - 1]);
      j -= 1;
    }
  }
  for (int i = 1; i < n; ++i){
    if (p[a[i]] > p[a[i + 1]]){
      ans.push_back({a[i + 1], a[i]});
      swap(a[i], a[i + 1]);
    }
  }
   for (int i = 1; i < n; ++i){
    if (p[a[i]] > p[a[i + 1]]){
      ans.push_back({a[i + 1], a[i]});
      swap(a[i], a[i + 1]);
    }
  }
  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1), c(n + 1), b(n + 1);
  for (int i = 1; i <= n; ++i){
    cin >> c[i];
    p[c[i]] = i;
    a[i] = i;
    b[i] = i;
  }
  vector<pt> ans;
  for (int i = n; i >= 1; --i){
    int x = c[i];
    for (int j = b[x] - 1; j >= 1; --j){
      ans.push_back({x, a[j]});
      swap(b[a[j + 1]], b[a[j]]);
      swap(a[j + 1], a[j]);
    }
    for (int j = b[x] + 1; j <= n; ++j){
      if (p[x] > p[a[j]]){
        ans.push_back({a[j], x});
        swap(b[a[j]], b[a[j - 1]]);
        swap(a[j], a[j - 1]);
      }else{
        break;
      }
    }
  }

  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}
Problem: 2052A - Adrenaline Rush
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<int> a(n + 1), p(n + 1);
  for (int i = 1; i <= n; ++i){
    int x;
    cin >> x;
    p[x] = i;
    a[i] = i;
  }
  vector<pt> ans;
  for (int i = 1; i < n; ++i){
    int x = a[i], y = a[i + 1];
    if (p[x] < p[y]){
      ans.push_back({y, x});
      ans.push_back({x, y});
    }else{
      if (i + 2 <= n && p[y] < p[a[i + 2]]){
        ans.push_back({a[i + 2], y});
        ans.push_back({y, a[i + 2]});
      }
      swap(a[i], a[i + 1]);
      ans.push_back({y, x});
    }
  }
  cout << ans.size() << '\n';
  for (auto& i : ans){
    cout << i.first << ' ' << i.second << '\n';
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  while (test--) {
    solve();
  }
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 7):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n, m;
  cin >> n >> m;
  multiset<int> a, b;
  for (int i = 0; i < n; ++i){
    int x;
    cin >> x;
    a.insert(x);
  }
  for (int i = 0; i < m; ++i){
    int x;
    cin >> x;
    b.insert(x);
  }
  while (b.size() > 0 && b.size() < a.size()){
    auto it = b.end();
    --it;
    int x = *it;
    auto it1 = a.end();
    --it1;
    if (*it1 > x){
      break;
    }
    if (b.count(x) > a.count(x)){
      b.erase(it);
      b.insert(x / 2);
      b.insert(x / 2 + x % 2);
    }else if (b.count(x) == a.count(x)){
      b.erase(x);
      a.erase(x);
    }else{
      break;
    }
  }
  if (a == b){
    cout << "Yes\n";
  }else{
    cout << "No\n";
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  while (test--) {
    solve();
  }
}


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n, m;
  cin >> n >> m;
  set<int> a, b;
  map<int, int> aa, bb;
  for (int i = 0; i < n; ++i){
    int x;
    cin >> x;
    a.insert(x);
    aa[x]++;
  }
  for (int i = 0; i < m; ++i){
    int x;
    cin >> x;
    b.insert(x);
    bb[x]++;
  }
  int cnt = 0;
  while ((b.size() > 0) && (m + cnt < n)){
    auto it = b.end();
    --it;
    int x = *it;
    auto it1 = a.end();
    --it1;
    if (*it1 > x){
      break;
    }
    if (bb[x] > aa[x]){
      b.erase(it);
      a.erase(x);
      b.insert(x / 2);
      b.insert(x / 2 + x % 2);
      if (m + cnt + bb[x] - aa[x] <= n){
        bb[x / 2] += bb[x] - aa[x];
        bb[x / 2 + x % 2] += bb[x] - aa[x];
        cnt += bb[x] - aa[x];
        bb[x] = 0, aa[x] = 0;
      }else{
        break;
      }
    }else if (bb[x] == aa[x]){
      bb[x] = 0, aa[x] = 0;
      b.erase(x);
      a.erase(x);
    }else{
      break;
    }
  }
  if (a == b){
    for (int i : a){
      if (aa[i] == bb[i]){
        cout << "Yes\n";
        return;
      }
    }
  }
  cout << "No\n";
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  while (test--) {
    solve();
  }
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)


Submission (verdict: Time limit exceeded on test 7):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n, m;
  cin >> n >> m;
  multiset<int> a, b;
  for (int i = 0; i < n; ++i){
    int x;
    cin >> x;
    a.insert(x);
  }
  for (int i = 0; i < m; ++i){
    int x;
    cin >> x;
    b.insert(x);
  }
  while (b.size() > 0 && b.size() < a.size()){
    auto it = b.end();
    --it;
    int x = *it;
    auto it1 = a.end();
    --it1;
    if (*it1 > x){
      break;
    }
    if (b.count(x) > a.count(x)){
      b.erase(it);
      b.insert(x / 2);
      b.insert(x / 2 + x % 2);
    }else if (b.count(x) == a.count(x)){
      b.erase(x);
      a.erase(x);
    }else{
      break;
    }
  }
  if (a == b){
    cout << "Yes\n";
  }else{
    cout << "No\n";
  }
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  while (test--) {
    solve();
  }
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n, m;
  cin >> n >> m;
  set<int> a, b;
  map<int, int> aa, bb;
  for (int i = 0; i < n; ++i){
    int x;
    cin >> x;
    a.insert(x);
    aa[x]++;
  }
  for (int i = 0; i < m; ++i){
    int x;
    cin >> x;
    b.insert(x);
    bb[x]++;
  }
  int cnt = 0;
  while ((b.size() > 0) && (m + cnt < n)){
    auto it = b.end();
    --it;
    int x = *it;
    auto it1 = a.end();
    --it1;
    if (*it1 > x){
      break;
    }
    if (bb[x] > aa[x]){
      b.erase(it);
      a.erase(x);
      b.insert(x / 2);
      b.insert(x / 2 + x % 2);
      if (m + cnt + bb[x] - aa[x] <= n){
        bb[x / 2] += bb[x] - aa[x];
        bb[x / 2 + x % 2] += bb[x] - aa[x];
        cnt += bb[x] - aa[x];
        bb[x] = 0, aa[x] = 0;
      }else{
        break;
      }
    }else if (bb[x] == aa[x]){
      bb[x] = 0, aa[x] = 0;
      b.erase(x);
      a.erase(x);
    }else{
      break;
    }
  }
  if (a == b){
    for (int i : a){
      if (aa[i] == bb[i]){
        cout << "Yes\n";
        return;
      }
    }
  }
  cout << "No\n";
}


int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  while (test--) {
    solve();
  }
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n, m;
  cin >> n >> m;
  multiset<int> a, b;
  map<int, int> aa, bb;
  for (int i = 0; i < n; ++i){
    int x;
    cin >> x;
    a.insert(x);
    aa[x]++;
  }
  for (int i = 0; i < m; ++i){
    int x;
    cin >> x;
    b.insert(x);
    bb[x]++;
  }
  while (b.size() > 0 && b.size() < a.size()){
    auto it = b.end();
    --it;
    int x = *it;
    auto it1 = a.end();
    --it1;
    if (*it1 > x){
      break;
    }
    if (bb[x] > aa[x]){
      b.erase(it);
      b.insert(x / 2);
      b.insert(x / 2 + x % 2);
      bb[x]--;
      bb[x / 2]++;
      bb[x / 2 + x % 2]++;
    }else if (bb[x] == aa[x]){
      b.erase(x);
      a.erase(x);
      bb[x] = 0;
      aa[x] = 0;
    }else{
      break;
    }
  }
  if (a == b){
    cout << "Yes\n";
  }else{
    cout << "No\n";
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  while (test--) {
    solve();
  }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<set<int>> e(n, set<int>());
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    e[--x].insert(--y);
    e[y].insert(x);
  }
  pt mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  int ans = 1;
  ans += (mx.first - 1);
  for (auto v : e[mx.second]){
    e[v].erase(mx.second);
  }
  e[mx.second].clear();
  mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  ans += (mx.first - 1);
  cout << ans << '\n';
}

int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  while (test--) {
    solve();
  }
}


Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

#ifndef ONLINE_JUDGE
#include "Debug.h";
#else
#define debug(...) 4
#endif

#define int long long
#define pt pair<int, int>
#define ft first
#define sc second
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()

void solve() {
  int n;
  cin >> n;
  vector<set<int>> e(n, set<int>());
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    e[--x].insert(--y);
    e[y].insert(x);
  }
  pt mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  int ans = 1;
  ans += (mx.first - 1);
  for (auto v : e[mx.second]){
    e[v].erase(mx.second);
  }
  e[mx.second].clear();
  mx = {0, 0};
  for (int i = 0; i < n; ++i){
    mx = max(mx, {e[i].size(), i});
  }
  ans += (mx.first - 1);
  cout << ans << '\n';
}

void burn(){
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
  }
}

void reveal(){
  int n;
  cin >> n;
  cout << n << '\n';
  for (int i = 0; i < n - 1; ++i){
    int x, y;
    cin >> x >> y;
    cout << x << ' ' << y << '\n';
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int test = 1;
  cin >> test;
  if (test == 9454){
    for (int i = 0; i < 40; ++i){
      burn();
    }
    for (int i = 0; i < test - 40; ++i){
      reveal();
    }
  }else{
  while (test--) {
    solve();
  }
  }
}