ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)


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

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        int max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 += lArr[k];
            max2 += rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

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

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        int max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 += lArr[k];
            max2 += rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}


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

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        long max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 += lArr[k];
            max2 += rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

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

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        int max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 = (max1 + lArr[k] < 0) ? INT_MAX : max1 + lArr[k];
            max2 = (max2 + rArr[k] < 0) ? INT_MAX : max2 + rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}


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

int main() {
    int t, n, l, r;
    cin >> t;
    for (int i = 0; i < t; i++) {
        vector<int> lArr, rArr;
        cin >> n >> l >> r;
        for (int j = 1; j <= n; j++) {
            int a;
            cin >> a;
            if (j <= r) lArr.push_back(a);
            if (j >= l) rArr.push_back(a);
        }

        sort(lArr.begin(), lArr.end());
        sort(rArr.begin(), rArr.end());
        long max1 = 0, max2 = 0;

        for (int k = 0; k < r - l + 1; k++) {
            max1 = (max1 + lArr[k] < 0) ? LONG_MAX : max1 + lArr[k];
            max2 = (max2 + rArr[k] < 0) ? LONG_MAX : max2 + rArr[k];
        }
        cout << min(max1, max2) << "\n";
    }
}
Problem: 2060D - Subtract Min Sort
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];
    if(n == 2 && nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n-1) if(nums[i] < nums[i-1] && nums[i] > nums[i+1]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}
Problem: 2060D - Subtract Min Sort
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];
    if(nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n-1) if(nums[i] < nums[i-1] && nums[i] > nums[i+1]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];

    if(nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n) nums[i] = abs(nums[i]-nums[i-1]);
    nums[0] = 0;

    rp(i, 1, n) if(nums[i] < nums[i]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}
Problem: 2060D - Subtract Min Sort
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];
    if(n == 2 && nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n-1) if(nums[i] < nums[i-1] && nums[i] > nums[i+1]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];
    if(nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n-1) if(nums[i] < nums[i-1] && nums[i] > nums[i+1]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}
Problem: 2060D - Subtract Min Sort
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];

    if(nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n) nums[i] = abs(nums[i]-nums[i-1]);
    nums[0] = 0;

    rp(i, 1, n) if(nums[i] < nums[i]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define rp(i,a,b)        for(ll i=a;i<b;i++)
typedef pair<ll, ll> pli;
typedef vector<ll> vi;
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

void solvetask(){ 
    int n;
    cin >> n;
    vector<int> nums(n);
    rp(i, 0, n) cin >> nums[i];

    if(nums[0] > nums[1]){
        cout << "NO\n";
        return;
    }

    rp(i, 1, n){
        int temp = min(nums[i], nums[i-1]);
        nums[i] -= temp;
        nums[i-1] -= temp;
    } 

    rp(i, 1, n) if(nums[i] < nums[i-1]){
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while(t--) solvetask();
}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b)        for(ll i=a;i<b;i++)

#define rp(i,a,b)        for(ll i=a;i<b;i++)

typedef pair<ll, ll> pli;

typedef vector<ll> vi;

#define endl '\n'

#define all(x) (x).begin(), (x).end()

#define sz(x) (ll)(x).size()



void solvetask(){ 

    int n;

    cin >> n;

    vector<ll> nums(n);

    rp(i, 0, n) cin >> nums[i];

    sort(all(nums));



    for(int i = n-2; i >= 0; i--) if(nums[i] == nums[i+1]){

        int j = 0, j1 = 1;

        while(j1 < n) {

            while(j == i || j == i+1) j++;

            while(j1 == i || j1 == i+1 || j1 <= j) j1++;

            if(double(1)*(nums[j1]-nums[j])/2 < nums[i]){

            // cout << double(1)*(nums[j]-nums[j+1])/2 << "\n";

                cout << nums[i] << " " << nums[i+1] << " " << nums[j1] << " " << nums[j] << "\n";

                return;

            }

            j++;

            j1++;

        } 

        

    }



    cout << "-1\n";

}



int main(){

    cin.tie(0)->sync_with_stdio(0);

    int t = 1;

    cin >> t;

    while(t--) solvetask();

}

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

using namespace std;

typedef long long ll;

#define rep(i,a,b)        for(ll i=a;i<b;i++)

#define rp(i,a,b)        for(ll i=a;i<b;i++)

typedef pair<ll, ll> pli;

typedef vector<ll> vi;

#define endl '\n'

#define all(x) (x).begin(), (x).end()

#define sz(x) (ll)(x).size()



void solvetask(){ 

    int n, L, R;

    cin >> n >> L >> R;

    vector<ll> nums(n), lef, rit;

    rp(i, 0, n) cin >> nums[i];



    rp(i, 0, L-1) lef.push_back(nums[i]);

    rp(i, R, n) rit.push_back(nums[i]);

    ll resp = 0;

    rp(i, L-1, R){

        resp += nums[i];

        lef.push_back(nums[i]);

        rit.push_back(nums[i]);

    } 



    sort(all(lef));

    sort(all(rit));



    ll temp1 = 0, temp2 = 0;

    if(lef.size() > R-L) rp(i, 0, R-L+1) temp1 += lef[i];

    else temp1 = 1e18;

    if(rit.size() > R-L) rp(i, 0, R-L+1) temp2 += rit[i];

    else temp2 = 1e18;

    cout << min({temp1, temp2, resp}) << "\n";

}



int main(){

    cin.tie(0)->sync_with_stdio(0);

    int t = 1;

    cin >> t;

    while(t--) solvetask();

}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)


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

using namespace std;

typedef long long ll;

#define rep(i,a,b)        for(ll i=a;i<b;i++)

#define rp(i,a,b)        for(ll i=a;i<b;i++)

typedef pair<ll, ll> pli;

typedef vector<ll> vi;

#define endl '\n'

#define all(x) (x).begin(), (x).end()

#define sz(x) (ll)(x).size()



void solvetask(){ 

    int n, L, R;

    cin >> n >> L >> R;

    vector<ll> nums(n), lef, rit;

    rp(i, 0, n) cin >> nums[i];



    rp(i, 0, L) lef.push_back(nums[i]);

    rp(i, R, n) rit.push_back(nums[i]);

    ll resp = 0;

    rp(i, L-1, R){

        resp += nums[i];

        lef.push_back(nums[i]);

        rit.push_back(nums[i]);

    } 



    sort(all(lef));

    sort(all(rit));



    ll temp1 = 0, temp2 = 0;

    if(lef.size() > R-L) rp(i, 0, R-L+1) temp1 += lef[i];

    else temp1 = 1e18;

    if(rit.size() > R-L) rp(i, 0, R-L+1) temp2 += rit[i];

    else temp2 = 1e18;



    cout << min({temp1, temp2, resp}) << "\n";

}



int main(){

    cin.tie(0)->sync_with_stdio(0);

    int t = 1;

    cin >> t;

    while(t--) solvetask();

}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

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

using namespace std;

typedef long long ll;

#define rep(i,a,b)        for(ll i=a;i<b;i++)

#define rp(i,a,b)        for(ll i=a;i<b;i++)

typedef pair<ll, ll> pli;

typedef vector<ll> vi;

#define endl '\n'

#define all(x) (x).begin(), (x).end()

#define sz(x) (ll)(x).size()



void solvetask(){ 

    int n, L, R;

    cin >> n >> L >> R;

    vector<ll> nums(n), lef, rit;

    rp(i, 0, n) cin >> nums[i];



    rp(i, 0, L) lef.push_back(nums[i]);

    rp(i, R, n) rit.push_back(nums[i]);

    ll resp = 0;

    rp(i, L-1, R){

        resp += nums[i];

        lef.push_back(nums[i]);

        rit.push_back(nums[i]);

    } 



    sort(all(lef));

    sort(all(rit));



    ll temp1 = 0, temp2 = 0;

    if(lef.size() > R-L) rp(i, 0, R-L+1) temp1 += lef[i];

    else temp1 = 1e18;

    if(rit.size() > R-L) rp(i, 0, R-L+1) temp2 += rit[i];

    else temp2 = 1e18;



    cout << min({temp1, temp2, resp}) << "\n";

}



int main(){

    cin.tie(0)->sync_with_stdio(0);

    int t = 1;

    cin >> t;

    while(t--) solvetask();

}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i,a,b)        for(ll i=a;i<b;i++)

#define rp(i,a,b)        for(ll i=a;i<b;i++)

typedef pair<ll, ll> pli;

typedef vector<ll> vi;

#define endl '\n'

#define all(x) (x).begin(), (x).end()

#define sz(x) (ll)(x).size()



void solvetask(){ 

    int n;

    cin >> n;

    vector<ll> nums(n);

    rp(i, 0, n) cin >> nums[i];

    sort(all(nums));



    for(int i = n-2; i >= 0; i--) if(nums[i] == nums[i+1]){

        int j = 0, j1 = 1;

        while(j1 < n) {

            while(j == i || j == i+1) j++;

            while(j1 == i || j1 == i+1 || j1 <= j) j1++;

            if(double(1)*(nums[j1]-nums[j])/2 < nums[i]){

            // cout << double(1)*(nums[j]-nums[j+1])/2 << "\n";

                cout << nums[i] << " " << nums[i+1] << " " << nums[j1] << " " << nums[j] << "\n";

                return;

            }

            j++;

            j1++;

        } 

        

    }



    cout << "-1\n";

}



int main(){

    cin.tie(0)->sync_with_stdio(0);

    int t = 1;

    cin >> t;

    while(t--) solvetask();

}
Problem: 2049C - MEX Cycle
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;





int mex(int a,int b){

    int less = min(a,b);

    int more = max(a,b);

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

        if(i!=less && i!=more){

            return i;

        }

    }

}

int main(){

    int tests;

    cin>>tests;

    for(int test=0;test<tests;test++){

        int n,x,y;

        cin>>n>>x>>y;

        vector<int> a;

        if(n%2==0){

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

                if(i%2==0){

                    a.push_back(0);

                }

                else{

                    a.push_back(1);

                }

            }

        }

        else{

            for(int i=0;i<n-2;i++){

                if(i%2==0){

                    a.push_back(0);

                }

                else{

                    a.push_back(1);

                }

            }

            a.push_back(2);

            a.push_back(1);

        }

        if(a[x-1] == a[y-1]){

            a[x-1] = 2;

            for(int i=0;i<n-1;i++){

                if(i==(x-1))continue;

                if(i==0){

                    a[i] = mex(a[n-1],a[i+1]);

                }

                else{

                    a[i] = mex(a[i-1],a[i+1]);

                }

            }

        }



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

            cout<<a[i]<<" ";

        }

        cout<<endl;

        

    }

}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;





int mex(int a,int b){

    int less = min(a,b);

    int more = max(a,b);

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

        if(i!=less && i!=more){

            return i;

        }

    }

}

int main(){

    int tests;

    cin>>tests;

    for(int test=0;test<tests;test++){

        int n,x,y;

        cin>>n>>x>>y;

        vector<int> a;

        if(n%2==0){

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

                if(i%2==0){

                    a.push_back(0);

                }

                else{

                    a.push_back(1);

                }

            }

        }

        else{

            for(int i=0;i<n-2;i++){

                if(i%2==0){

                    a.push_back(0);

                }

                else{

                    a.push_back(1);

                }

            }

            a.push_back(2);

            a.push_back(1);

        }

        if(a[x-1] == a[y-1]){

            a[x-1] = 2;

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

                if(i==(x-1))continue;

                if(i==0){

                    a[i] = mex(a[n-1],a[i+1]);

                }

                else if(i==n-1){

                    a[i] = mex(a[i-1],a[0]);

                }

                else{

                    a[i] = mex(a[i-1],a[i+1]);

                }

            }

        }



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

            cout<<a[i]<<" ";

        }

        cout<<endl;

        

    }

}
Problem: 2049C - MEX Cycle
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;





int mex(int a,int b){

    int less = min(a,b);

    int more = max(a,b);

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

        if(i!=less && i!=more){

            return i;

        }

    }

}

int main(){

    int tests;

    cin>>tests;

    for(int test=0;test<tests;test++){

        int n,x,y;

        cin>>n>>x>>y;

        vector<int> a;

        if(n%2==0){

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

                if(i%2==0){

                    a.push_back(0);

                }

                else{

                    a.push_back(1);

                }

            }

        }

        else{

            for(int i=0;i<n-2;i++){

                if(i%2==0){

                    a.push_back(0);

                }

                else{

                    a.push_back(1);

                }

            }

            a.push_back(2);

            a.push_back(1);

        }

        if(a[x-1] == a[y-1]){

            a[x-1] = 2;

            for(int i=0;i<n-1;i++){

                if(i==(x-1))continue;

                if(i==0){

                    a[i] = mex(a[n-1],a[i+1]);

                }

                else{

                    a[i] = mex(a[i-1],a[i+1]);

                }

            }

        }



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

            cout<<a[i]<<" ";

        }

        cout<<endl;

        

    }

}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 2):
#include <algorithm>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define vinput(v) for(int i=0;i<n;i++){cin>>v[i];}
#define loop(i,m,n) for(int i=m;i<n;i++)
#define vi vector<int>
#define vll vector<long long int>
#define pb push_back
#define ppb pop_back
#define MOD 1000000007
#define MOD1 998244353
#define nl endl 
#define mp make_pair
#define allr(x) (x).rbegin(),(x).rend()
#define ff first
#define sx(x) ((int)(x).size())
#define ss second
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define big1 998244353
#define big2 1000000007
#define all(v) v.begin(),v.end()
typedef long long ll;

vector<vector<ll> > arr; 
vector<set<ll> > adj;
set<pair<ll,ll> > d;
vector<ll> a;

void dfs(int node, int par) {
    for (auto i : arr[node]) {
        if (i != par) {
            auto it = d.find(mp(a[i], i));
            if (it != d.end()) {
                d.erase(it);
                d.insert(mp(a[i] - 1, i));
            }
        }
    }
}

void solve() {
    ll n;
    cin >> n;
    arr.assign(n + 1, vector<ll>());
    adj.assign(n + 1, set<ll>());
    a.assign(n + 1, 0);
    
    for (ll i = 1; i < n; i++) {
        ll u, v;
        cin >> u >> v;
        arr[u].pb(v);
        arr[v].pb(u);
        adj[u].insert(v);
        adj[v].insert(u);
        a[u]++;
        a[v]++;
    }

    if (n == 2) {
        cout << 0 << endl;
        return;
    }

    for (ll i = 1; i <= n; i++) {
        d.insert(mp(a[i], i));
    }

    ll ans = 0;
    auto it = d.rbegin();
    ans += it->ff;
    dfs(it->ss, 0);
    
    d.erase(prev(it.base()));  // Corrected erase
    it = d.rbegin();
    
    ans += it->ff;
    ans--;

    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
#endif
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}