ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

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

int32_t main()
{
    int t;
    cin>>t;
    while(t--) {
        int n,k; cin>>n>>k;
        unordered_map<int,int> fq;
        for(int i=0;i<n;i++) {
            int x;cin>>x;
            fq[x]++;
        }
        vector<int> v;
        for(auto& x : fq) {
            v.push_back(x.second);
        }
        sort(v.begin(),v.end(), greater<int> ());
        while(k > 0) {
            if(v.back() <= k) {
                k -= v.back();
                v.pop_back();
            }
        }
        cout <<  max((int)v.size(), 1LL) << endl;
    }
    return 0;
}

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

int32_t main()
{
    int t;
    cin>>t;
    while(t--) {
        int n,k; cin>>n>>k;
        unordered_map<int,int> fq;
        for(int i=0;i<n;i++) {
            int x;cin>>x;
            fq[x]++;
        }
        vector<int> v;
        for(auto& x : fq) {
            v.push_back(x.second);
        }
        sort(v.begin(),v.end(), greater<int> ());
        while(k > 0 && !v.empty()) {
            if(v.back() <= k) {
                k -= v.back();
                v.pop_back();
            }
        }
        cout <<  max((int)v.size(), 1LL) << endl;
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
/******************************************************************************

 * Filename: sol.cpp

 * Problem: CF - 2063B - Subsequence Update

 *****************************************************************************/

#include <iostream>

#include <algorithm>



using namespace std;



using ll = long long;



const int kMaxN = (int)1e5;



int a[kMaxN];



ll solve(int n, int l, int r) {

	ll ret = 0;

	ll origin_sum = 0;

	int len = r-l+1;

	for (int i = l; i <= r; i++) {

		origin_sum += a[i-1];

	}

	sort(a, a+n);

	for (int i = 0; i < len; i++) {

		ret += a[i];

	}

	if (ret == origin_sum && len < n) ret += a[len] - a[len-1];

	return ret;

}



int main() {

	std::ios::sync_with_stdio(false);

	std::cin.tie(nullptr);



	int t;

	cin >> t;

	while (t--) {

		int n, l, r;

		cin >> n >> l >> r;

		for (int i = 0; i < n; i++) cin >> a[i];

		cout << solve(n, l, r) << endl;

	}



	return 0;

}



Submission (verdict: Accepted):
/******************************************************************************
 * Filename: sol.cpp
 * Problem: CF - 2063B - Subsequence Update
 *****************************************************************************/
#include <iostream>
#include <algorithm>

using namespace std;

using ll = long long;

const int kMaxN = (int)1e5;

int a[kMaxN], b[kMaxN];

ll kthMinSum(int k, int *begin, int *end) {
	ll ret = 0;
	int len = end - begin;
	copy(begin, end, b);
	sort(b, b+len);
	for (int i = 0; i < k; i++) ret += b[i];
	return ret;
}

ll solve(int n, int l, int r) {
	return min(kthMinSum(r-l+1, a, a+r), kthMinSum(r-l+1, a+l-1, a+n));
}

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

	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		for (int i = 0; i < n; i++) cin >> a[i];
		cout << solve(n, l, r) << endl;
	}

	return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
/******************************************************************************
 * Filename: sol.cpp
 * Problem: CF - 2063B - Subsequence Update
 *****************************************************************************/
#include <iostream>
#include <algorithm>

using namespace std;

using ll = long long;

const int kMaxN = (int)1e5;

int a[kMaxN];

ll solve(int n, int l, int r) {
	ll ret = 0;
	sort(a, a+n);
	for (int i = 0; i < r-l+1; i++) {
		ret += a[i];
	}
	return ret;
}

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

	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		for (int i = 0; i < n; i++) cin >> a[i];
		cout << solve(n, l, r) << endl;
	}

	return 0;
}


Submission (verdict: Wrong answer on test 2):
/******************************************************************************

 * Filename: sol.cpp

 * Problem: CF - 2063B - Subsequence Update

 *****************************************************************************/

#include <iostream>

#include <algorithm>



using namespace std;



using ll = long long;



const int kMaxN = (int)1e5;



int a[kMaxN];



ll solve(int n, int l, int r) {

	ll ret = 0;

	ll origin_sum = 0;

	int len = r-l+1;

	for (int i = l; i <= r; i++) {

		origin_sum += a[i-1];

	}

	sort(a, a+n);

	for (int i = 0; i < len; i++) {

		ret += a[i];

	}

	if (ret == origin_sum && len < n) ret += a[len] - a[len-1];

	return ret;

}



int main() {

	std::ios::sync_with_stdio(false);

	std::cin.tie(nullptr);



	int t;

	cin >> t;

	while (t--) {

		int n, l, r;

		cin >> n >> l >> r;

		for (int i = 0; i < n; i++) cin >> a[i];

		cout << solve(n, l, r) << endl;

	}



	return 0;

}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
/******************************************************************************
 * Filename: sol.cpp
 * Problem: CF - 2063B - Subsequence Update
 *****************************************************************************/
#include <iostream>
#include <algorithm>

using namespace std;

using ll = long long;

const int kMaxN = (int)1e5;

int a[kMaxN];

ll solve(int n, int l, int r) {
	ll ret = 0;
	sort(a, a+n);
	for (int i = 0; i < r-l+1; i++) {
		ret += a[i];
	}
	return ret;
}

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

	int t;
	cin >> t;
	while (t--) {
		int n, l, r;
		cin >> n >> l >> r;
		for (int i = 0; i < n; i++) cin >> a[i];
		cout << solve(n, l, r) << endl;
	}

	return 0;
}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll>vll;
typedef vector<int>vi;
const ll intmax = INT_MAX;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    vi a(n);
    vi hsh(10);
    ll sum = 0;
    for(int i = 0; i < n; i++){
        string x = ""; x += s[i];
        a[i] = stoi(x);
        hsh[a[i]]++;
        sum += a[i];
    }
    if(sum % 9 == 0){
        cout << "YES\n";
        return;
    }
    int two = hsh[2], six = hsh[3];
    bool yes = false;
    if(six && two == 1){
        for(int i = 6; i <= 6*six; i += 6){
            sum += 6;
            if((sum % 9 == 0) || ((sum + 2) % 9 == 0)){
                yes = true;
                break;
            }
        }
    }
    else if(two){
        for(int i = 2; i <= 2*two + 6*six; i += 2){
            sum += 2;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
        }
    }else if(six){
        for(int i = 6; i <= 2*two + 6*six; i += 6){
            sum += 6;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
        }
    }
    if(yes){
        cout << "YES\n";
    }else{
        cout << "NO\n";
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    int i = 1;
    while(T--) {
        //cout << "Case #" << i << "\n"; i++;
        solve();
    }
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll>vll;
typedef vector<int>vi;
const ll intmax = INT_MAX;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    vi a(n);
    vi hsh(10);
    ll sum = 0;
    for(int i = 0; i < n; i++){
        string x = ""; x += s[i];
        a[i] = stoi(x);
        hsh[a[i]]++;
        sum += a[i];
    }
    if(sum % 9 == 0){
        cout << "YES\n";
        return;
    }
    int two = hsh[2], six = hsh[3];
    bool yes = false;
    if(two >= 2){
        for(int i = 2; i <= 2*two + 6*six; i += 2){
            sum += 2;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
        }
    }else if(six){
        for(int i = 0; i < six; i++){
            sum += 6;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
            if(two){
                if((sum + 2) % 9 == 0){
                    yes = true;
                    break;
                }
            }
        }
    }else if(two){
        sum += 2;
        if(sum % 9 == 0){
            yes = true;
        }
    }
    if(yes){
        cout << "YES\n";
    }else{
        cout << "NO\n";
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    int i = 1;
    while(T--) {
        //cout << "Case #" << i << "\n"; i++;
        solve();
    }
}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll>vll;
typedef vector<int>vi;
const ll intmax = INT_MAX;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    vi a(n);
    vi hsh(10);
    ll sum = 0;
    for(int i = 0; i < n; i++){
        string x = ""; x += s[i];
        a[i] = stoi(x);
        hsh[a[i]]++;
        sum += a[i];
    }
    if(sum % 9 == 0){
        cout << "YES\n";
        return;
    }
    int two = hsh[2], six = hsh[3];
    bool yes = false;
    if(six && two == 1){
        for(int i = 6; i <= 6*six; i += 6){
            sum += 6;
            if((sum % 9 == 0) || ((sum + 2) % 9 == 0)){
                yes = true;
                break;
            }
        }
    }
    else if(two){
        for(int i = 2; i <= 2*two + 6*six; i += 2){
            sum += 2;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
        }
    }else if(six){
        for(int i = 6; i <= 2*two + 6*six; i += 6){
            sum += 6;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
        }
    }
    if(yes){
        cout << "YES\n";
    }else{
        cout << "NO\n";
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    int i = 1;
    while(T--) {
        //cout << "Case #" << i << "\n"; i++;
        solve();
    }
}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll>vll;
typedef vector<int>vi;
const ll intmax = INT_MAX;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    vi a(n);
    vi hsh(10);
    ll sum = 0;
    for(int i = 0; i < n; i++){
        string x = ""; x += s[i];
        a[i] = stoi(x);
        hsh[a[i]]++;
        sum += a[i];
    }
    if(sum % 9 == 0){
        cout << "YES\n";
        return;
    }
    int two = hsh[2], six = hsh[3];
    bool yes = false;
    if(two >= 2){
        for(int i = 2; i <= 2*two + 6*six; i += 2){
            sum += 2;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
        }
    }else if(six){
        for(int i = 0; i < six; i++){
            sum += 6;
            if(sum % 9 == 0){
                yes = true;
                break;
            }
            if(two){
                if((sum + 2) % 9 == 0){
                    yes = true;
                    break;
                }
            }
        }
    }else if(two){
        sum += 2;
        if(sum % 9 == 0){
            yes = true;
        }
    }
    if(yes){
        cout << "YES\n";
    }else{
        cout << "NO\n";
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    int i = 1;
    while(T--) {
        //cout << "Case #" << i << "\n"; i++;
        solve();
    }
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll>vll;
typedef vector<int>vi;
const ll intmax = INT_MAX;

void solve()
{
    string s;
    cin >> s;
    int n = s.size();
    vi a(n);
    vi hsh(10);
    ll sum = 0;
    for(int i = 0; i < n; i++){
        string x = ""; x += s[i];
        a[i] = stoi(x);
        hsh[a[i]]++;
        sum += a[i];
    }
    if(sum % 9 == 0){
        cout << "YES\n";
        return;
    }
    int two = hsh[2], six = hsh[3];
    bool yes = false;
    if(two >= 2){
        for(int i = 2; i <= 2*two + 6*six; i += 2){
            if((sum + i) % 9 == 0){
                yes = true;
                break;
            }
        }
    }else if(six){
        for(int i = 0; i <= 6*six; i += 6){
            if((sum + i) % 9 == 0){
                yes = true;
                break;
            }
            if(two){
                if((sum + i + 2) % 9 == 0){
                    yes = true;
                    break;
                }
            }
        }
    }else if(two){
        sum += 2;
        if(sum % 9 == 0){
            yes = true;
        }
    }
    if(yes){
        cout << "YES\n";
    }else{
        cout << "NO\n";
    }
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int T;
    cin >> T;
    int i = 1;
    while(T--) {
        //cout << "Case #" << i << "\n"; i++;
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include<iostream>
using namespace std;

void solve() {
    int n;
    cin >> n;
    int m;
    int b = 0;
    int bcnt = 0;
    int c = 0;
    int ccnt = 0;
    int sum = 0;
    for (int i=0; i<n; i++) {
        cin >> m;
        if (i%2) {
            b += m;
            bcnt++;
        } else {
            c += m;
            ccnt++;
        }
        sum += m;
    }
    int avg = sum / n;
    if ((c/ccnt)==(b/bcnt) && (b/bcnt) == avg) {
        cout << "YES\n";
    } else {
        cout << "NO\n";
    }
}

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

Submission (verdict: Time limit exceeded on test 6):
#include<iostream>
#include<cstring>
using namespace std;

void solve() {
    string s;
    cin >> s;
    for (int i=0; i<s.length()-1; ++i) {
        int n = s[i] - '0';
        int max_digit = 0;
        int max_new_n = n;
        for (int j=i+1; j<s.length(); ++j) {
            int new_n = s[j] - '0' - (j - i);
            if (new_n > max_new_n) {
                max_digit = j;
                max_new_n = new_n;
            }
        }
        for (int k=max_digit; k>i; --k) {
            s[k] = s[k-1];
        }
        s[i] = max_new_n + '0';
    }
    cout << s << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 6):
#include<iostream>
#include<cstring>
using namespace std;

void solve() {
    string s;
    cin >> s;
    for (int i=0; i<s.length()-1; ++i) {
        int n = s[i] - '0';
        int max_digit = i;
        int max_new_n = n;
        for (int j=i+1; j<s.length(); ++j) {
            int new_n = s[j] - '0' - (j - i);
            if (new_n > max_new_n) {
                max_digit = j;
                max_new_n = new_n;
            }
        }
        if (max_digit != i) {
            for (int k=max_digit; k>i; --k) {
                s[k] = s[k-1];
            }
            s[i] = max_new_n + '0';
        }
    }
    cout << s << endl;
}

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

Submission (verdict: Accepted):
#include<iostream>
#include<cstring>
using namespace std;

void solve() {
    string s;
    cin >> s;
    for (int i=0; i<s.length()-1; ++i) {
        int n = s[i] - '0';
        int max_digit = i;
        int max_new_n = n;
        for (int j=i+1; j<s.length(); ++j) {
            if ((9 - (j - i)) <=0) {
                break;
            }
            int new_n = s[j] - '0' - (j - i);
            if (new_n > max_new_n) {
                max_digit = j;
                max_new_n = new_n;
            }
        }
        if (max_digit != i) {
            for (int k=max_digit; k>i; --k) {
                s[k] = s[k-1];
            }
            s[i] = max_new_n + '0';
        }
    }
    cout << s << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 1):
#include<iostream>
using namespace std;

void solve() {
    int n;
    cin >> n;
    int m;
    int b = 0;
    int bcnt = 0;
    int c = 0;
    int ccnt = 0;
    int sum = 0;
    for (int i=0; i<n; i++) {
        cin >> m;
        if (i%2) {
            b += m;
            bcnt++;
        } else {
            c += m;
            ccnt++;
        }
        sum += m;
    }
    int avg = sum / n;
    if ((c/ccnt)==(b/bcnt) && (b/bcnt) == avg) {
        cout << "YES\n";
    } else {
        cout << "NO\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 6):
#include<iostream>
#include<cstring>
using namespace std;

void solve() {
    string s;
    cin >> s;
    for (int i=0; i<s.length()-1; ++i) {
        int n = s[i] - '0';
        int max_digit = 0;
        int max_new_n = n;
        for (int j=i+1; j<s.length(); ++j) {
            int new_n = s[j] - '0' - (j - i);
            if (new_n > max_new_n) {
                max_digit = j;
                max_new_n = new_n;
            }
        }
        for (int k=max_digit; k>i; --k) {
            s[k] = s[k-1];
        }
        s[i] = max_new_n + '0';
    }
    cout << s << endl;
}

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

Submission (verdict: Time limit exceeded on test 6):
#include<iostream>
#include<cstring>
using namespace std;

void solve() {
    string s;
    cin >> s;
    for (int i=0; i<s.length()-1; ++i) {
        int n = s[i] - '0';
        int max_digit = i;
        int max_new_n = n;
        for (int j=i+1; j<s.length(); ++j) {
            int new_n = s[j] - '0' - (j - i);
            if (new_n > max_new_n) {
                max_digit = j;
                max_new_n = new_n;
            }
        }
        if (max_digit != i) {
            for (int k=max_digit; k>i; --k) {
                s[k] = s[k-1];
            }
            s[i] = max_new_n + '0';
        }
    }
    cout << s << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

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

void solve() {
  int n; cin >> n;
  vector<pair<int,int>> b;
  set<pair<int,int>> edges;
  vector<int> degree(n,0);
  
  for (int i = 0; i < n-1; i++) {
    int u,v;
    cin >> u >> v;
    u--; v--;
    
    degree[u]++;
    degree[v]++;
    edges.insert(make_pair(u,v));
    edges.insert(make_pair(v,u));
  }
  
  for (int i = 0; i <= n-1; i++) {
    b.push_back(make_pair(degree[i],i));
  }
  sort(b.begin(), b.end(), greater<pair<int,int>>());
  
  if (n == 2) {cout << 0 << endl; return;}
  
  int M = b[0].first, N = b[1].first, K = b[2].first;
  
  if (M == N && N == K) {
    cout << 2*M - 1 << endl;
    return;
  } else if (M == N && N > K) {
    if (edges.find(make_pair(b[0].second, b[1].second)) != edges.end()) {
      cout << M+N-1 << endl;
    } else {
      cout << 2*M - 1 << endl;
    }
    return;
  } else if (M > N && N == K) {
    int flag = 0;
    for (int i = 1; i < n; i++) {
      if (b[i].first != b[1].first) break; 
      if (edges.find(make_pair(b[0].second, b[i].second)) == edges.end()) {
        flag = 1;
        break;
      }
    }
    
    if (flag) {
      cout << M+N-1 << endl;
    } else {
      cout << M+N-2 << endl;
    }
    return;
  } else {
    cout << M+N-1 << endl;
    return;
  }
  
  return;
}

int main() {
  int t; cin >> t;
  while (t--) {
    solve();
  }
}

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

void solve() {
  int n; cin >> n;
  vector<pair<int,int>> b;
  set<pair<int,int>> edges;
  vector<int> degree(n,0);
  
  for (int i = 0; i < n-1; i++) {
    int u,v;
    cin >> u >> v;
    u--; v--;
    
    degree[u]++;
    degree[v]++;
    edges.insert(make_pair(u,v));
    edges.insert(make_pair(v,u));
  }
  
  for (int i = 0; i <= n-1; i++) {
    b.push_back(make_pair(degree[i],i));
  }
  sort(b.begin(), b.end(), greater<pair<int,int>>());
  
  if (n == 2) {cout << 0 << endl; return;}
  
  int M = b[0].first, N = b[1].first, K = b[2].first;
  
  if (M == N && N == K) {
    cout << 2*M - 1 << endl;
    return;
  } else if (M == N && N > K) {
    if (edges.find(make_pair(b[0].second, b[1].second)) != edges.end()) {
      cout << M+N-2 << endl;
    } else {
      cout << 2*M-1 << endl;
    }
    return;
  } else if (M > N && N == K) {
    int flag = 0;
    for (int i = 1; i < n; i++) {
      if (b[i].first != b[1].first) break; 
      if (edges.find(make_pair(b[0].second, b[i].second)) == edges.end()) {
        flag = 1;
        break;
      }
    }
    
    if (flag) {
      cout << M+N-1 << endl;
    } else {
      cout << M+N-2 << endl;
    }
    return;
  } else {
    cout << M+N-1 << endl;
    return;
  }
  
  return;
}

int main() {
  int t; cin >> t;
  while (t--) {
    solve();
  }
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


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

void solve() {
  int n; cin >> n;
  vector<pair<int,int>> b;
  set<pair<int,int>> edges;
  vector<int> degree(n,0);
  
  for (int i = 0; i < n-1; i++) {
    int u,v;
    cin >> u >> v;
    u--; v--;
    
    degree[u]++;
    degree[v]++;
    edges.insert(make_pair(u,v));
    edges.insert(make_pair(v,u));
  }
  
  for (int i = 0; i <= n-1; i++) {
    b.push_back(make_pair(degree[i],i));
  }
  sort(b.begin(), b.end(), greater<pair<int,int>>());
  
  if (n == 2) {cout << 0 << endl; return;}
  
  int M = b[0].first, N = b[1].first, K = b[2].first;
  
  if (M == N && N == K) {
    cout << 2*M - 1 << endl;
    return;
  } else if (M == N && N > K) {
    if (edges.find(make_pair(b[0].second, b[1].second)) != edges.end()) {
      cout << M+N-1 << endl;
    } else {
      cout << 2*M - 1 << endl;
    }
    return;
  } else if (M > N && N == K) {
    int flag = 0;
    for (int i = 1; i < n; i++) {
      if (b[i].first != b[1].first) break; 
      if (edges.find(make_pair(b[0].second, b[i].second)) == edges.end()) {
        flag = 1;
        break;
      }
    }
    
    if (flag) {
      cout << M+N-1 << endl;
    } else {
      cout << M+N-2 << endl;
    }
    return;
  } else {
    cout << M+N-1 << endl;
    return;
  }
  
  return;
}

int main() {
  int t; cin >> t;
  while (t--) {
    solve();
  }
}