ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 359 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long

int T,n;
vector<int> a[10005];
signed main(){
	cin>>T;
	while(T--){
		cin>>n;
		for(int i=1;i<=n;i++){
			int tmp;
			cin>>tmp;
			a[T].push_back(tmp);
		}
		bool f=true;
		for(int i=0;i<n-1;i++){
			int k=min(a[T][i],a[T][i+1]);
			a[T][i]-=k;
//			cout<<i<<':'<<a[T][i]<<' ';
			if(a[T][i]!=0){
				cout<<"NO"<<'\n';
				f=false;
				break;
			}
			a[T][i+1]-=k;
		}
		if(f){
			cout<<"YES"<<'\n';
		}
	}
	return 0;
}


Solution 2 (runtime: 93 ms):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9+7;

void dice() {
   int n;cin>>n;
   vector<int>x(n);
   for(int &it:x)cin>>it;
   for(int i=0;i<n-1;i++){
    int tem = min(x[i],x[i+1]);
        x[i+1]-= tem;
        x[i]-=tem;
        
    // if(is_sorted(x.begin(),x.end())){
    //     cout<<"YES"<<endl;
    //     return;
    // }
   }
   is_sorted(x.begin(),x.end()) ? cout<<"YES" : cout<<"NO";
   cout<<endl;
}

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

   int t = 1;
   cin>>t; //
   while (t--)dice();
   return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

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

#define ll long long
#define mp make_pair
#define pii pair<int, int> 
#define pll pair<ll, ll>
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(v) begin(v), end(v)
#define cy {cout<<"YES";return;}
#define cn {cout<<"NO";return;}
#define sz(v) (int)v.size()
#define FOR(i,x,n) for(int i=(x);i<=(n);++i)
#define FORD(i,x,n) for(int i=(x);i>=(n);--i)
#define debug(var) cout<<#var<<": "<<var<<" "

const ll inf = 1e18 + 7;
const ll mod = 1e9 + 7;
const int N = 2e5 +5 ;
const char nl = '\n';

int n;
int a[N];
void solve(){
    cin >> n;
    FOR(i, 0, n - 1){
        cin >> a[i];
    }

    FOR(i, 0, n - 2){
        int tmp = min(a[i], a[i + 1]);
        a[i] -= tmp;
        a[i + 1] -= tmp;
    }

    bool flag = true;
    FOR(i, 1, n - 1){
        if (a[i] < a[i - 1]){
            flag = false;
            break;
        }
    }

    if (flag){
        cy;
    }
    else{ 
        cn;
    }

}


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

    int T = 1;
    cin >> T;
    while(T--){
        solve();
        cout << nl;
    }
}




Solution 2 (runtime: 343 ms):
#include <bits/stdc++.h>



using namespace std;



int main()

{

int t;

cin >> t;



while(t--)

{

int n;

cin >> n;



vector<int> a(n);

for(int &i : a)

cin >> i;



bool flag = true;

for(int i = 1; i < n; i++)

{

int tmp = min(a[i], a[i-1]);



a[i-1] -= tmp;

a[i] -= tmp;



if(a[i-1] > a[i])

{

flag = false;

break;

}

}



cout << ((flag) ? "YES" : "NO") << endl;

}



return 0;

}
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 77 ms):
#include<bits/stdc++.h>



using namespace std;



#define fi first

#define se second

#define yes cout<<"YES\n";

#define no cout<<"NO\n";

#define nl cout << '\n';

#define pb push_back

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

#define fr(a, b) for( int i = a; i < b; i++)

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

#define allr(x) (x).rbegin(), (x).rend()

#define vll vector<long long>

#define vi vector<int>





/*──────▄▌▐▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▌

 * ───▄▄██▌█ beep beep--------------

 * ▄▄▄▌▐██▌█ -20rr delivery------------

 * ███████▌█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▌

 * ▀(@)▀▀▀▀(@)(@)▀▀▀▀▀▀▀▀▀▀▀(@)▀

  */



typedef long double ld;

typedef long long int lli;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<long long, long long> pll;





void shawerma() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

#ifdef yoyo

    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

#endif

}





void Solve() {

    int n;

    cin >> n;

    vll a(n);

    ll sum = 0;

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

        cin >> a[i];

        sum += a[i];

    }

    if(sum % n){

        no

        return;

    }



    ll targ = sum / n;

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

        if(a[i-1] != targ){

            ll dif = targ - a[i-1];

            a[i-1] += dif;

            a[i+1] -= dif;

        }

    }



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

        if(a[i] != targ){

            no

            return;

        }

    }

    yes

}



int main() {

    shawerma();

    int t = 1;

    cin >> t;

    while (t--) {

        Solve();

    }

}



/*  */



/*  */



// atcoder 395



Solution 2 (runtime: 406 ms):
#include<bits/stdc++.h>

using namespace std;

void gotoo(){

    long long n;

    cin>>n;

    long long a[n],i,sum=0;

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

        cin>>a[i];

        sum+=a[i];

    }

    if(sum%n!=0){

        cout<<"NO"<<endl;

        return;

    }

    long long ch=sum/n;

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

        if(a[i-1]>ch){

            long long ne1=a[i-1]-ch;

            a[i-1]=a[i-1]-ne1;

            a[i+1]=a[i+1]+ne1;

        }

        else if(a[i-1]<ch){

            long long nee=ch-a[i-1];

            a[i+1]=a[i+1]-nee;

            a[i-1]=a[i-1]+nee;

        }

    }

    if(a[n-2]==ch && a[n-1]==ch)

        cout<<"YES"<<endl;

    else

        cout<<"NO"<<endl;

        

}

int main(){

    int t;

    cin>>t;

    while(t--){

        gotoo();

    }

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 109 ms):
// Codeforces Round 998: Problem D

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define f first
#define s second

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    if (a[0] > a[1]) {
        cout << "NO\n";
        return;
    }
    for (int i = 1; i < n; i++) {
        if (a[i] >= a[i - 1] && (i == 1 || a[i - 2] == 0)) {
            a[i] -= a[i - 1], a[i - 1] = 0;
        }
    }

    // for (auto x : a) cout << x << ' ';
    // cout << '\n';

    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            cout << "NO\n";
            return;
        }
    }

    cout << "YES\n";
}

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

Solution 2 (runtime: 358 ms):
#include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define ll long long

//
//bool check(ll k, ll mid, ll n, vector<ll>& a, vector<ll>& b){
//    ll counter = 0;
//    bool f = false;
//    for (int i = 0; i < n; ++i){
//        if (mid <= b[i] && mid > a[i]) {
//            counter++;
//            f = true;
//        }
//        else if (mid <= a[i]) {
//            f = true;
//        }
//    }
//    return (counter <= k && f);
//}

void solve() {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n - 1; ++i){
        if (a[i + 1] >= a[i]){
            a[i + 1] = a[i + 1] - a[i];
        }
        else {
            cout << "NO" << el;
            return;
        }
    }
    cout << "YES" << el;
}

int main() {
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 296 ms):
#include <bits/stdc++.h>

using namespace std;

#define all(X) (X).begin(), (X).end()
#define sz(X) ((int)(X).size())

#define st first
#define nd second

using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;

void solvesub(int n, const vector<int>& pos, vector<int>& curr, vector<pll>& ans) {
    int i = n - 1;
    while(i > 0 && pos[curr[i]] < pos[curr[i - 1]]) {
        ans.push_back({curr[i], curr[i - 1]});
        swap(curr[i], curr[i - 1]);
        i--;
    }
    for(int j = i - 1; j >= 0; j--) {
        ans.push_back({curr[i], curr[j]});
    }
    for(int j = 0; j < i; j++) {
        ans.push_back({curr[j], curr[i]});
    }
}

void solve() {
    int n;
    cin >> n;
    vector<int> pos(n);
    for(int i = 0; i < n; i++) {
        int v;
        cin >> v;
        v--;
        pos[v] = i;
    }
    vector<int> curr(n);
    iota(curr.begin(), curr.end(), 0);
    vector<pll> ans;
    for(int i = 1; i < n; i++) {
        solvesub(i + 1, pos, curr, ans);
    }

    cout << ans.size() << "\n";
    for(auto v : ans) {
        cout << v.first + 1 << " " << v.second + 1 << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}

Solution 2 (runtime: 280 ms):
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    vector<pair<int,int>> ans;
    int an=0;
    for(int i=0;i<n;i++){
        int j;
        cin >> j;
        a[i]=j;
    }
    unordered_set<int> bks;
    for(int i=n-1;i>0;i--){
        int cur=a[i]-1;
        while(cur>0){
            if(bks.find(cur)==bks.end() && cur!=a[i]){
                ans.push_back(make_pair(a[i],cur));
                cur--;
            }else{
                cur--;
            }
        }
        cur=1;
        while(cur<=n){
            if(bks.find(cur)==bks.end()&&cur!=a[i]){
                ans.push_back(make_pair(cur,a[i]));
                cur++;
            }else{
                cur++;
            }
        }
        bks.emplace(a[i]);
    }
    cout << ans.size();
    cout << "\n";
    for(auto& [a,b]:ans){
        cout << a << " " << b << "\n";
    }
    return 0;
}
Problem: 2057C - Trip to the Olympiad
Language: c++17 

Solution 1 (runtime: 77 ms):
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2")
#define int long long
using namespace std;
main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int l, r;
        cin >> l >> r;
        int sum = 0;
        for (int j = 30; j >= 0; j--) {
            if (((1<<j) & l) == ((1<<j) & r)) {
                sum += ((1LL << j) & l);
            } else {
                int a = sum + (1LL << j);
                int b = a - 1;
                int x = l;
                while (x == a || x == b) {
                    x++;
                }
                cout << a << ' ' << b << ' ' << x << endl;
                break;
            }
        }
    }
}


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

int main() {
	int t; cin>>t; while(t--){
	    int l,r; cin>>l>>r;
	    int a=r;int  b=l;int cnt=0;
	    
	    while(a/2!=b/2){
	        cnt++;
	        a/=2; b/=2;
	    }
	    
	    a=a<<cnt;
	    
	    b=r>a?r:l;
	    
	    cout<<a<<" "<<a-1<<" "<<b<<"\n";
	}

}
Problem: 2052A - Adrenaline Rush
Language: c++17 

Solution 1 (runtime: 327 ms):
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
	cin>>n;
	vector<int>a(n+1);
	vector<int>m(n+1);
	vector<pair<int,int> >ans;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		m[i]=i;
	}
	for(int i=n;i>=1;i--){
		int p=0;
		while(m[p]!=a[i])p++;
		for(int j=p-1;j>=1;j--){
			ans.push_back({m[p],m[j]});
		} 
		for(int j=1;j<p;j++){
			ans.push_back({m[j],m[p]});
		}
		while(p<i){
			ans.push_back({m[p+1],m[p]});
			swap(m[p+1],m[p]);
			p++;
		}
	}
	cout<<ans.size()<<"\n";
	for(int i=0;i<ans.size();i++){
		cout<<ans[i].first<<" "<<ans[i].second<<"\n";
	}
	return 0;
}

Solution 2 (runtime: 1296 ms):
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ld long double
#define int long long
#include <iomanip>

int indexFind(vector<int>& arr , int x){
    int n = arr.size()-1;
    for(int i = 1 ; i <= n ; i++){
        if(arr[i] == x)return i;
    }
    return 0;
}

void operation(vector<int>& arr , vector<int>& perm , int start){
    int n = arr.size()-1;
    int idx = indexFind(perm , arr[start]);
    stack<int> bigger ,smaller;
    for(int i = idx+ 1 ; i <= n ; i++){
        cout<<perm[i]<<' '<<arr[start]<<endl;
        bigger.push(perm[i]);
    }
    while(bigger.size()){
        cout<<arr[start]<<' '<<bigger.top()<<endl;
        bigger.pop();
    }
    for(int i = start ;i < idx ; i++){
        smaller.push(perm[i]);
    }
    while(smaller.size()){
        cout<<arr[start]<<' '<<smaller.top()<<endl;
        smaller.pop();
    }
    for(int i = idx ; i> start ; i--){
        swap(perm[i] , perm[i-1]);
    }
}

int inversionCnt(vector<int>& arr){
    int n = arr.size() - 1;
    int ans(0);
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j < i ; j++){
            if(arr[j] > arr[i])ans++;
        }
    }
    return (n-1)*n - ans;
}

void solve(){
    int n;  cin >> n;
    vector<int> arr(n+1);
    for(int i = 1 ; i <= n ; i++){
        cin >> arr[i];
    }
    int sz = inversionCnt(arr);
    cout<<sz<<endl;
    if(sz == 0)return;

    vector<int> perm(n+1);
    for(int i = 1 ; i <= n ; i++){
        perm[i] = i;
    }
    
    for(int i = 1 ; i < n ; i++){
        operation(arr , perm , i);
    }

}


int32_t main(){
    int tt = 1;
    //cin>>tt;
    while(tt--){
        solve(); 
    }
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 46 ms):
#include<bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int t;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		int n=(int)s.size();
		for(int i=1;i<n;i++){
			for(int j=i-1;j>=0;j--){
				int a=(int)(s[j+1]-48)-1;
				int b=(int)(s[j]-48);
				if(a>b){
					s[j+1]=(char)(a+48);
					swap(s[j+1],s[j]);
				}
				else{
					break;
				}
			}
		}
		cout<<s<<'\n';
	}
	return 0;
}

Solution 2 (runtime: 61 ms):
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define lcm(a,b) (a*(b/__gcd(a,b)))
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define rep(i,j,k) for(ll i=j;i<k;i++)
#define rrep(i,j,k) for(ll i=j;i>=k;i--)
#define oset tree <ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define omset tree <pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
#define endl "\n" 
const ll MOD= 1e9+7;
const ll N=1e5+7;

ll binexp(ll a, ll b, ll MOD = 1e9 + 7)
{
    ll ans=1;
    while(b>0)
    {
        if(b&1)
        ans=(ans*a)%MOD;
        a=(a*a)%MOD;
        b>>=1;
    }
    return ans;
}

void forrealtho()
{
    string s;
    cin>>s;
    ll n=s.size();
    rep(i,0,n-1){
        if(s[i]<s[i+1]-1){
            swap(s[i],s[i+1]);
            s[i]--;
            if(i>=1) i-=2;
        }
    }
    cout<<s<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    ll t=1;
    cin>>t;
    while(t--) forrealtho();
    return 0;
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 78 ms):
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

#define ll long long
int main() {
	// your code goes here
	int t=0;
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		ll n=s.size();
		if(n==1){
			cout<<s<<"\n";
			continue;
		}
		for(ll i=0;i<n;++i){
			ll max_num=0;
			ll max_idx=-1;
			for(ll j=i+1;j<=min(n-1,i+9);++j){
				ll c=s[j]-'0';
				if(max_num<c-j+i){
					max_num=c-j+i;
					max_idx=j;
				}
			}
			ll c=s[i]-'0';
			if(max_num>c){
				char tmp=s[i];
				char k=max_num+'0';
				s[i]=k;
				for(ll j=i+1;j<=max_idx;++j){
					char ttmp=s[j];
					s[j]=tmp;
					tmp=ttmp;
				}
			}
		}
		cout<<s<<"\n";
	} 
	return 0;
}

Solution 2 (runtime: 61 ms):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()

{

	ios::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

	ll t;

	cin>>t;

	while(t--)

	{

		string s;

		cin>>s;

		ll len=s.length();

		for(ll i=0;i<=len-1;i++)

		{

			ll pos=i;

			ll max=s[i]-'0';

			for(ll j=i+1;j<=min(len-1,i+8);j++)

			{

				if(max<s[j]-'0'-(j-i))

				{

					pos=j;

					max=s[j]-'0'-(j-i);

				}

			}

			for(ll j=pos;j>i;j--)

			{

				swap(s[j],s[j-1]);

			}

			char temp=max+'0';

			s[i]=temp;

		}

		cout<<s<<"\n";

	}

}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 78 ms):
// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int n = s.size();
        for(int i=0;i<n;i++){
            char mxEle = s[i];
            int idx = i;
            for(int j=1;j<=8;j++){
                if(i+j == n){
                    break;
                }
                if(mxEle < (s[i+j] - j)){
                    mxEle = (s[i+j] - j);
                    idx = (i+j);
                }
            }
            while(i != idx){
                swap(s[idx],s[idx-1]);
                idx--;
            }
            s[i] = mxEle;
        }
        cout<<s<<"\n";
    }

    return 0;
}

Solution 2 (runtime: 61 ms):
#include <bits/stdc++.h>

using namespace std;

//#define int long long

signed main()

{

     ios::sync_with_stdio(false);

     cin.tie(0);

     int tt;

    cin >> tt;

    while (tt--)

    {

        string s;

        cin>>s;

        while(true)

        {

            bool flag=false;

        for(int i=0;i<9;i++)

        {

            for(int j=i;j<s.size()-1;j++)

            {

                if(s[j]<s[j+1]-1)

                {

                    s[j+1]--;

                    flag=true;

                    swap(s[j],s[j+1]);

                }

            }

        }

        if(flag==false)

            break;

        }

        cout<<s<<endl;

    }

    return 0;

}
Problem: 2050C - Uninteresting Number
Language: c++17 

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

int binary_search(vector<long long int>&arr,long long int target){
    long long int l=1;
    long long int r= arr.size();
    int cnt=0;
    while(l<=r){
        long long int mid = l+(r-l)/2;
        
        if(arr[mid]<target){
            cnt=mid;
            l=mid+1;
        }
        else{
            r=mid-1;
        }
    }
    
    return cnt;
    
}
int main(){
    int t;
    cin>>t;
    while(t--){
        
        string n;
        cin>>n;
        int sum =0;
        int cnt2=0;
        int cnt3=0;
        for(int i=0;i<n.size();i++){
            sum+= n[i]-'0';
            if(n[i]-'0'==2){
                cnt2++;
            }
            if(n[i]-'0'==3){
                cnt3++;
            }
        }
        
        if(sum%9==0){
            cout<<"YES"<<endl;
            continue;
        }
        
        int flag =0;
        map<int,int>mpp;
        for(int i=0;i<=cnt2;i++){
            for(int j=0;j<=cnt3;j++){
                int sumr = i*2 + j*6;
                
                int rem = (sum+sumr)%9;
                mpp[rem]++;
                if(rem==0){
                    
                    flag=1;
                    break;
                }
                
                
                
                
            }
            if(mpp.size()==9){
                break;
            }
        }
        
        if(flag) cout<<"YES"<<endl;
        
        else cout<<"NO"<<endl;
        
    }
}

Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;

    while(t--) {
        string num;
        cin >> num;
        
        long long sumOfDigits = 0;
        long long countOfTwos = 0, countOfThrees = 0;
        
        for (char c : num) {
            int digit = c - '0';
            sumOfDigits += digit;
            if (digit == 2) countOfTwos++;
            if (digit == 3) countOfThrees++;
        }
        
        long long target = (-sumOfDigits) % 9;
        if (target < 0) target += 9;
        
        bool isPossible = false;
        
        for (long long b = 0; b <= 2 && b <= countOfThrees; b++) {
            long long temp = target - 6 * b;
            long long r = (5 * temp) % 9;
            if (r < 0) r += 9;
            if (r <= countOfTwos) {
                isPossible = true;
                break;
            }
        }

        if (isPossible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }

}
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 N 200010
int du[N];
vector<int>E[N];
void work() {
    int n; cin >> n;
    int mxid = 0, mxdu = 0;
    for (int i = 1; i <= n; ++i)E[i].clear(), du[i] = 0;
    multiset<int>mst;
    for (int i = 1; i < n; ++i) {
        int x, y; cin >> x >> y;
        E[x].push_back(y);
        E[y].push_back(x);
        du[x]++; du[y]++;
    }
    for (int i = 1; i <= n; ++i)mst.insert(du[i]);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        auto it = mst.find(du[i]);
        mst.erase(it);
        for (auto v : E[i]) {
            mst.erase(mst.find(du[v]));
            ans = max(ans, du[i] + du[v] - 2);
        }
        if (mst.size())
            ans = max(ans, du[i] + *mst.rbegin() - 1);
        mst.insert(du[i]);
        for (auto v : E[i]) {
            mst.insert(du[v]);
        }
    }
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T; cin >> T;
    while (T--)work();
    return 0;
}

Solution 2 (runtime: 296 ms):

// Author //

// Md Nehal // 




#include<bits/stdc++.h>



using namespace std;



void solve(){
	int n;
	cin>>n;
	int arr[n+10];
	for(int i=0;i<=n+6;++i){
		arr[i] =0;
	}
	vector<int>graph[n+10];
	for(int i=1;i<n;++i){
		int u ,v;
		cin>>u>>v;
		graph[v].push_back(u);
		graph[u].push_back(v);
		arr[u]++,arr[v]++;
	}
	int maxi =0;
	for(int i=1;i<=n;++i){
		maxi = max(arr[i],maxi);
	}
	int cnt =0;
	for(int i=1;i<=n;++i){
		if(arr[i] == maxi){
			cnt++;
		}
	}
	if(cnt>2){
		cout<<maxi*2 - 1<<endl;
		return ;
	}
	int result =maxi,ind=0;
	for(int i=1;i<=n;++i){
		if(arr[i] == maxi){
			ind = i;
			for(auto &child:graph[i]){
				arr[child]--;
			}
			break ;
		}
	}
	maxi =0;
	for(int i=1;i<=n;++i){
		if(i == ind){
			continue;
		}
		maxi = max(maxi,arr[i]);
	}
	cout<<result+maxi-1<<endl;
	return ;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}