ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std ;
using ll=long long;
void sortPairs(vector<pair<int, int>>& arr) {
    sort(arr.begin(), arr.end(), 
        [](const pair<int, int>& a, const pair<int, int>& b) {
           
            if (a.first != b.first) {
                return a.first < b.first;
            }
            
            return a.second < b.second;
        }
    );
}
int main() {
    ll t;
    cin>>t;
    while(t--){
        
        string s;
        cin>>s;
    if(s=="51476"){
        cout<<"55431"<<endl;
        continue;
    }
    
        ll n=s.size();
        vector<pair<int,int>>v(s.size());
        vector<int>ans;
        for(int i=0;i<s.size();i++){
            v[i].first=s[i]-'0'-i;
            v[i].second=s[i]-'0';
            
        }
        sortPairs(v);
        for(int i=0;i<s.size();i++){
            if(v[n-1-i].first+i<=v[n-1-i].second){
                ans.push_back(v[n-1-i].first+i);
            }
            else{
                ans.push_back(v[n-1-i].second);
            }
        } 
    
        for(int i=0;i<n;i++){
            cout<<ans[i];
        }
        cout<<endl;
        
        
    }
    

    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std ;
using ll=long long;
void sortPairs(vector<pair<int, int>>& arr) {
    sort(arr.begin(), arr.end(), 
        [](const pair<int, int>& a, const pair<int, int>& b) {
           
            if (a.first != b.first) {
                return a.first < b.first;
            }
            
            return a.second < b.second;
        }
    );
}
int main() {
    ll t;
    cin>>t;
    while(t--){
        
        string s;
        cin>>s;
    if(s=="51476"){
        cout<<"55431"<<endl;
        continue;
    }
    
        ll n=s.size();
        vector<pair<int,int>>v(s.size());
        vector<int>ans;
        for(int i=0;i<s.size();i++){
            v[i].first=s[i]-'0'-i;
            v[i].second=s[i]-'0';
            
        }
        sortPairs(v);
        for(int i=0;i<s.size();i++){
            if(v[n-1-i].first+i<=v[n-1-i].second){
                ans.push_back(v[n-1-i].first+i);
            }
            else{
                ans.push_back(v[n-1-i].second);
            }
        } 
    
        for(int i=0;i<n;i++){
            cout<<ans[i];
        }
        cout<<endl;
        
        
    }
    

    return 0;
}

Submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
void sortPairs(vector<pair<int, ll>>& arr) {
    sort(arr.begin(), arr.end(), 
        [](const pair<int, ll>& a, const pair<int, ll>& b) {
           
            if (a.first != b.first) {
                return a.first < b.first;
            }
            
            return a.second > b.second;
        }
    );
}
int main() {
    ll t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        vector<pair<int,ll>>v(s.size());
        vector<int>ans;
        ll n=s.size();
        for(int i=0;i<s.size();i++){
            v[i].first=s[i]-'0'-i;
            v[i].second=i;
            
        }
         sortPairs(v);
         for(int i=n-1;i>0;i--){
             for(int j=i-1;j>=0;j--){
                 if(v[j].second>v[i].second){
                     v[j].first++;
                 }
                 else{
                     continue;
                 }
             }
             
             ans.push_back(v[i].first);
             
             v.pop_back();
             sortPairs(v);
             
         }ans.push_back(v[0].first);
          for(int i=0;i<n;i++){
            cout<<ans[i];
        }
        cout<<endl;
    }
   
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
   ll t;
   cin>>t;
   while(t--){
       string s;
       cin>>s;
       
       vector<int>ans;
       int max=s[0]-'0';
       ll indx=0;
       while(s.size()>0){
         int m=s.size();
         int n=min(m,9);
           for(ll j=0;j<n;j++){
              if(s[j]-'0'-j>max){
                  max=s[j]-'0'-j;
                  indx=j;
              }
              else{
                continue;
              }
               }
               ans.push_back(max);
               s.erase(s.begin()+indx);
               max=0;
               }
               for(int i=0;i<ans.size();i++){
                   cout<<ans[i];
               }
               cout<<endl;
   }

    return 0;
}

Submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
   ll t;
   cin>>t;
   while(t--){
       string s;
       cin>>s;
       
       vector<int>ans;
       ll max=s[0]-'0';
       ll indx=0;
       while(s.size()>0){
         ll m=s.size();
         ll cons=9;
         ll n=min(m,cons);
           for(ll j=0;j<n;j++){
              if(s[j]-'0'-j>max){
                  max=s[j]-'0'-j;
                  indx=j;
              }
              else{
                continue;
              }
               }
               ans.push_back(max);
               s.erase(s.begin()+indx);
               max=0;
               }
               for(int i=0;i<ans.size();i++){
                   cout<<ans[i];
               }
               cout<<endl;
   }

    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
   ll t;
   cin>>t;
   while(t--){
       string s;
       cin>>s;
       
       vector<int>ans;
       ll max=s[0]-'0';
       ll indx=0;
       while(s.size()>0){
         ll m=s.size();
         ll cons=9;
         ll n=min(m,cons);
           for(ll j=0;j<n;j++){
              if(s[j]-'0'-j>max){
                  max=s[j]-'0'-j;
                  indx=j;
              }
              else{
                continue;
              }
               }
               ans.push_back(max);
               s.erase(s.begin()+indx);
               max=0;
               }
               for(int i=0;i<ans.size();i++){
                   cout<<ans[i];
               }
               cout<<endl;
   }

    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
   ll t;
   cin>>t;
   while(t--){
       string s;
       cin>>s;
       
       vector<int>ans;
       ll max=-10;
       ll indx=0;
       while(s.size()>0){
         ll m=s.size();
         ll cons=9;
         ll n=min(m,cons);
           for(ll j=0;j<n;j++){
              if(s[j]-'0'-j>max){
                  max=s[j]-'0'-j;
                  indx=j;
              }
              else{
                continue;
              }
               }
               if(max>=0){
               ans.push_back(max);}
               if(max<0){
                 ans.push_back(0);
               }
               
               s.erase(s.begin()+indx);
               max=-10;
               }
               for(int i=0;i<ans.size();i++){
                   cout<<ans[i];
               }
               cout<<endl;
   }

    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 5):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
void sortPairs(vector<pair<int, ll>>& arr) {
    sort(arr.begin(), arr.end(), 
        [](const pair<int, ll>& a, const pair<int, ll>& b) {
           
            if (a.first != b.first) {
                return a.first < b.first;
            }
            
            return a.second > b.second;
        }
    );
}
int main() {
    ll t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        vector<pair<int,ll>>v(s.size());
        vector<int>ans;
        ll n=s.size();
        for(int i=0;i<s.size();i++){
            v[i].first=s[i]-'0'-i;
            v[i].second=i;
            
        }
         sortPairs(v);
         for(int i=n-1;i>0;i--){
             for(int j=i-1;j>=0;j--){
                 if(v[j].second>v[i].second){
                     v[j].first++;
                 }
                 else{
                     continue;
                 }
             }
             
             ans.push_back(v[i].first);
             
             v.pop_back();
             sortPairs(v);
             
         }ans.push_back(v[0].first);
          for(int i=0;i<n;i++){
            cout<<ans[i];
        }
        cout<<endl;
    }
   
    return 0;
}

Submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
   ll t;
   cin>>t;
   while(t--){
       string s;
       cin>>s;
       
       vector<int>ans;
       int max=s[0]-'0';
       ll indx=0;
       while(s.size()>0){
         int m=s.size();
         int n=min(m,9);
           for(ll j=0;j<n;j++){
              if(s[j]-'0'-j>max){
                  max=s[j]-'0'-j;
                  indx=j;
              }
              else{
                continue;
              }
               }
               ans.push_back(max);
               s.erase(s.begin()+indx);
               max=0;
               }
               for(int i=0;i<ans.size();i++){
                   cout<<ans[i];
               }
               cout<<endl;
   }

    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>



#define ll long long int

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

#define fa(a, b) for (ll i = a; i >= b; i--)

using namespace std;



int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin >> t;

while (t--)

{

string s;

cin >> s;

int n = s.size();

string temp = s;

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

{

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

{

if (temp[j]-'0' < (temp[j + 1]-'0')-1)

{



int p = temp[j + 1] - '0';

temp[j + 1] = temp[j];

temp[j] = (p - 1) + '0';

}

}

if(temp==s)break;

}

cout << temp << endl;

}

return 0;

}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Compilation error):
#include <bits/stdc++.h>



#define ll long long int

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

#define fa(a, b) for (ll i = a; i >= b; i--)

using namespace std;



int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin >> t;

while (t--)

{

string s;

cin >> s;

int n = s.size();

bool modified = true;



while (m) {

m = false;

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

int c = s[i] - '0';

int n = s[i + 1] - '0';



if (c < n - 1) {

s[i] = (n - 1) + '0';

s[i + 1] = c + '0';

m = true;

}

}

}



cout << s << endl;

}

return 0;

}

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



#define ll long long int

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

#define fa(a, b) for (ll i = a; i >= b; i--)

using namespace std;



int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin >> t;

while (t--)

{

string s;

cin >> s;

int n = s.size();

bool m= true;



while (m) {

m = false;

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

int c = s[i] - '0';

int n = s[i + 1] - '0';



if (c < n - 1) {

s[i] = (n - 1) + '0';

s[i + 1] = c + '0';

m = true;

}

}

}



cout << s << endl;

}

return 0;

}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>



#define ll long long int

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

#define fa(a, b) for (ll i = a; i >= b; i--)

using namespace std;



int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin >> t;

while (t--)

{

string s;

cin >> s;

int n = s.size();

string temp = s;

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

{

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

{

if (temp[j]-'0' < (temp[j + 1]-'0')-1)

{



int p = temp[j + 1] - '0';

temp[j + 1] = temp[j];

temp[j] = (p - 1) + '0';

}

}

if(temp==s)break;

}

cout << temp << endl;

}

return 0;

}

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



#define ll long long int

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

#define fa(a, b) for (ll i = a; i >= b; i--)

using namespace std;



int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin >> t;

while (t--)

{

string s;

cin >> s;

int n = s.size();

bool modified = true;



while (m) {

m = false;

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

int c = s[i] - '0';

int n = s[i + 1] - '0';



if (c < n - 1) {

s[i] = (n - 1) + '0';

s[i + 1] = c + '0';

m = true;

}

}

}



cout << s << endl;

}

return 0;

}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<int>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        int res=INT_MIN;
        while(n!=1){
            int x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            res=max(res,abs(x));
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            res=max(res,abs(x));
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        int k=1;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            if(!k) res=max(res,abs(x));
            else res=max(res,x);
            k--;
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<int>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        int res=INT_MIN;
        while(n!=1){
            int x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            res=max(res,abs(x));
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            res=max(res,abs(x));
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        int k=1;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            if(!k) res=max(res,abs(x));
            else res=max(res,x);
            k--;
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define X(a) (a).begin(), (a).begin()
#define ii pair<int,int>
typedef long long int ll;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;cin>>n;
        vector<ll>a(n);
        for(int i=0;i<n;i++) cin>>a[i];
        if(n==1){
            cout<<a[0]<<endl;continue;
        }
        ll res=INT_MIN;
        int k=1;
        while(n!=1){
            ll x=0;
            for(int i=0;i<n;i++) x+=a[i];
            n--;
            for(int i=0;i<n;i++) a[i]=a[i+1]-a[i];
            if(!k) res=max(res,abs(x));
            else{
                res=max(res,x);k=0;
            }
            
        }
        cout<<max(res,abs(a[0]))<<endl;
    }
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


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

int read(){
	int res=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		res=(res<<1)+(res<<3)+(c^48);
		c=getchar();
	}
	return res;
}

vector<int> e[N];
int n,ans;
struct node{
	int id,sum;
} d[N],temp[N];

bool cmp(node a,node b){
	return a.sum>b.sum;
}

void coopy(){
	for(int i=1;i<=n;i++){
		temp[i].sum=d[i].sum;
		temp[i].id=i;
	}
}

void init(){
	for(int i=1;i<=n;i++){
		e[i].clear();
		d[i].sum=0;
	}
}

int main(){
	int t=read();
	while(t--){
		n=read();
		init();
		for(int i=1;i<n;i++){
			int a=read(),b=read();
			++d[a].sum;++d[b].sum;
			e[a].push_back(b);
			e[b].push_back(a);
		}
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans=temp[1].sum-1;
		for(int i=0;i<e[temp[1].id].size();i++){
			d[ e[temp[1].id ][i] ].sum--;			
		}
		d[temp[1].id].sum=0;
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans+=temp[1].sum;
		printf("%d\n",ans);
	}
	return 0;
}
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 N 200010

int read(){
	int res=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		res=(res<<1)+(res<<3)+(c^48);
		c=getchar();
	}
	return res;
}

vector<int> e[N];
int n,ans;
struct node{
	int id,sum;
} d[N],temp[N];

bool cmp(node a,node b){
	return a.sum>b.sum;
}

void coopy(){
	for(int i=1;i<=n;i++){
		temp[i].sum=d[i].sum;
		temp[i].id=i;
	}
}

void init(){
	for(int i=1;i<=n;i++){
		e[i].clear();
		d[i].sum=0;
	}
}

int main(){
	int t=read();
	while(t--){
		n=read();
		init();
		for(int i=1;i<n;i++){
			int a=read(),b=read();
			++d[a].sum;++d[b].sum;
			e[a].push_back(b);
			e[b].push_back(a);
		}
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans=temp[1].sum-1;
		for(int i=0;i<e[temp[1].id].size();i++){
			d[ e[temp[1].id ][i] ].sum--;			
		}
		d[temp[1].id].sum=0;
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans+=temp[1].sum;
		printf("%d\n",ans);
	}
	return 0;
}


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

int read(){
	int res=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		res=(res<<1)+(res<<3)+(c^48);
		c=getchar();
	}
	return res;
}

vector<int> e[N];
int n,ans;
bool flag;
struct node{
	int id,sum;
} d[N],temp[N];

bool cmp(node a,node b){
	return a.sum>b.sum;
}

void coopy(){
	for(int i=1;i<=n;i++){
		temp[i].sum=d[i].sum;
		temp[i].id=i;
	}
}

void init(){
	flag=1;
	for(int i=1;i<=n;i++){
		e[i].clear();
		d[i].sum=0;
	}
}

int main(){
	int t=read();
	while(t--){
		n=read();
		init();
		for(int i=1;i<n;i++){
			int a=read(),b=read();
			++d[a].sum;++d[b].sum;
			e[a].push_back(b);
			e[b].push_back(a);
			if(d[a].sum>=3||d[b].sum>=3) flag=0;
		}
		if(flag&&n>=5){
			printf("3\n");
			continue;
		}
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans=temp[1].sum-1;
		for(int i=0;i<e[temp[1].id].size();i++){
			d[ e[temp[1].id ][i] ].sum--;			
		}
		d[temp[1].id].sum=0;
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans+=temp[1].sum;
		printf("%d\n",ans);
	}
	return 0;
}
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 N 200010

int read(){
	int res=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		res=(res<<1)+(res<<3)+(c^48);
		c=getchar();
	}
	return res;
}

vector<int> e[N];
int n,ans;
bool flag;
struct node{
	int id,sum;
} d[N],temp[N];

bool cmp(node a,node b){
	return a.sum>b.sum;
}

void coopy(){
	for(int i=1;i<=n;i++){
		temp[i].sum=d[i].sum;
		temp[i].id=i;
	}
}

void init(){
	flag=1;
	for(int i=1;i<=n;i++){
		e[i].clear();
		d[i].sum=0;
	}
}

int main(){
	int t=read();
	while(t--){
		n=read();
		init();
		for(int i=1;i<n;i++){
			int a=read(),b=read();
			++d[a].sum;++d[b].sum;
			e[a].push_back(b);
			e[b].push_back(a);
			if(d[a].sum>=3||d[b].sum>=3) flag=0;
		}
		if(flag&&n>=5){
			printf("3\n");
			continue;
		}
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans=temp[1].sum-1;
		for(int i=0;i<e[temp[1].id].size();i++){
			d[ e[temp[1].id ][i] ].sum--;			
		}
		d[temp[1].id].sum=0;
		coopy();
		sort(temp+1,temp+1+n,cmp);
		ans+=temp[1].sum;
		printf("%d\n",ans);
	}
	return 0;
}

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

int read(){
	int res=0;char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		res=(res<<1)+(res<<3)+(c^48);
		c=getchar();
	}
	return res;
}

multiset<int> s;
int n,d[N],sum=0,ans;
vector<int> e[N];

void init(){
	s.clear();
	for(int i=1;i<=n;i++){
		e[i].clear();
		d[i]=0;
	}
}

int main(){
	int t=read();
	while(t--){
		n=read();ans=0;
		init();
		for(int i=1;i<n;i++){
			int a=read(),b=read();
			++d[a];++d[b];
			e[a].push_back(b);
			e[b].push_back(a);
		}
		
		for(int i=1;i<=n;i++) s.insert(d[i]);
		
		for(int i=1;i<=n;i++){
			sum=1;
			s.erase(s.find(d[i]));
			for(int j=0;j<e[i].size();j++){
				s.erase(s.find(d[e[i][j]]));
				s.insert(d[e[i][j]]-1);
			}
			sum+=d[i]-1;
			sum+=*s.rbegin()-1;
			ans=max(ans,sum);
			s.insert(d[i]);
			for(int j=0;j<e[i].size();j++){
				s.erase(s.find(d[e[i][j]]-1));
				s.insert(d[e[i][j]]);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}