ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2049C - MEX Cycle
Language: c++17 

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

using namespace std;



int main() {

    int t ;cin>>t; // You can modify t for multiple test cases

    while (t--) {

        int n, x, y;

        cin >> n >> x >> y;



        vector<int> a(n, 0); // Initialize with 0



        if (y==n) {

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

                a[i] = (i % 2 == 0) ? 0 : 1;

            }

            if (n % 2) {

                a[0] = 2;

            

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

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

                    a[x - 1] = 2;

                    if (x == 3) {

                        a[x - 2] = 0;

                        a[0] = 1;

                    }

                } else if (a[x - 1] == 1) {

                    a[y - 1] = 2;

                }

            }

        }

        else{

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

            a[x-1] = 2;

            }

        }

        } else {

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

                a[i] = (i % 2 == 0) ? 0 : 1;

            }

            if (n % 2) {

                a[n - 1] = 2;

            

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

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

                    a[y - 1] = 2;

                    if (y + 1 < n && a[y + 1] == 2) { // Avoid out-of-bounds access

                        a[y] = 0;

                        a[y+1] = 1;

                    }

                } else if (a[x - 1] == 1) {

                    a[x - 1] = 2;

                }

            }

        }else{

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

            a[x-1] = 2;

            }

        }

        }



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

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

        }

        cout << endl;

    }



    return 0;

}



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

using namespace std;



void solve(){

    int n, x, y;

    cin >> n >> x >> y;

    vector<int> res(n + 1);

    if (n % 2) {

        bool ok = false;

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

            if (i == y) ok = true;

            res[i] = (i + ok) % 2;

        }

        res[y] = 2;

    }

    else {

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

            res[i] = i % 2;

        }

        if (res[x] == res[y]) res[y] = 2;

    }

    for (int i = 1; i <= n; i++) cout << res[i] << " \n"[i == n];

}

int main(){

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    

    int T;

    cin >> T;

    while (T--) {

        solve();

    }

    return 0;

}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

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


int main(){
	int t;
	cin>>t;
	while(t--){
		int n,k;
		cin>>n>>k;
		map<int,int> m;
		for(int i = 0;i<n;i++){
			int a;
			cin>>a;
			m[a]++;
		}
		vector<int> nums;
		for(auto i :m){
			nums.push_back(i.second);
		}

		sort(nums.begin(),nums.end());
		int ans = nums.size();
		for(int i : nums){
			k-=i;
			if(k<0)break;
			ans--;
		}


		cout<<max(ans,1)<<endl;
	}
}


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

void printList(vector<int> &v){
	for(auto it=v.begin();it!=v.end();it++){
		cout<<*it<<" ";
	}
    cout<<endl;
}
const int mod = 998244353;

void swap(int& a, int& b){
    int temp=a;
    a=b;
    b=temp;
}





vector<ll> prefixSum(const vector<int>& arr) {
    int n = arr.size();
    vector<ll> prefix(n);
    prefix[0] = arr[0];
    ll pfs=prefix[0];
    for (int i = 1; i < n; i++) {
        prefix[i] = prefix[i - 1] + arr[i];
        pfs += prefix[i];
    }
    return {prefix[n-1],pfs};
}

// ll fn(int l, int r, vector<ll>& v, ll sum){
//     while(l<r){
//         if(v[l]>0){
//             sum += v[l];
//             l=l+1;
//         }
//         if(v[r]<0){
//             sum += (-v[r]);
//             r = r-1;
//         }
//         if(v[l]<0 && v[r]>0){
//             ll temp1=0;
//             int m=l;
//             int n=r;
//             while(v[m]<0) m++;
//             ll a= fn(m,n,v,temp1);
        
//             ll temp2=0;
//             m=l;
//             n=r;
//             while(v[n]>0) n--;
//             ll b= fn(m,n,v,temp2);

//             sum += max(a,b);
//             break;
//         }
//     }
//     if(l==r) sum += abs(v[l]);
//     return sum;
// }
long long power(long long a, long long b)
{
    long long result = 1;
    while (b) {
        if (b & 1)
            result = (result * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return result;
}

int minm(vector<int> &v){
    int ans=v[0];
    for(int i=0;i<v.size();i++){
        ans = min(ans, v[i]);
    }
    return ans;
}



int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        map<int,int> mp;
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            mp[x]++;
        }
        int ans = mp.size();
        vector<int> fq;
        for(auto it=mp.begin();it!=mp.end();it++){
            fq.push_back(it->second);
        }
        sort(fq.begin(),fq.end());
        for(int i=0;i<fq.size();i++){
            if(ans==1) break;
            if(fq[i]<=k){
                ans--;
                k-=fq[i];
            }
        }
        cout<<ans<<endl;



            
        }
        return 0;

    }
Problem: 2062C - Cirno and Operations
Language: c++17 

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

void solve(){
    int n;
    cin>>n;
    vector<long long> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    long long ans=INT64_MIN;
    int curSize=n;
    for(int i=0;i<n;i++){
        long long sum=0;
        for(int i=0;i<curSize;i++) sum=(sum+(long long)(a[i]));
        if(i==0)  ans=max(ans,sum);
        else ans=max(ans,max(sum,(-sum)));
        for(int i=0;i<curSize;i++) a[i]=(a[i+1]-a[i]);
        curSize--;
    }
    cout<<ans<<endl;
}
int main(){
    int tc;
    cin>>tc;
    while(tc--){
        solve();
    }
}

Solution 2 (runtime: 46 ms):
// IN THE NAME OF ALLAH
// Coded by Ahsanul Imam
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
// find_by_order() - Returns an iterator to the k-th largest element(counting from zero)
// order_of_key() - The number of items in a set that are strictly smaller than our item
typedef long long int ll;
const int  MOD = 1000000007;
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define f first
#define s second
#define nl '\n';
#define bit(a) __builtin_popcountll(a)
#define w(x) while (x--)
#define test int t; cin>>t; for (int i = 1; i <= t; i++)
#define loop for (int i = 0; i < n; i++)
#define print(v) for(auto it:v)cout<<it<<' ';cout<<endl;
#define srt(v) sort(v.begin(), v.end());
#define rsrt(v) sort(v.rbegin(), v.rend());
#define gcd(a, b) __gcd<long long int>(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define pi 2*acos(0.0)
#define pr pair<int, int>
#define Ahsanul (ios_base::sync_with_stdio(false), cin.tie(NULL))
vector<pr>knight_posible{{2, 1}, {2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};//knight moves
vector<pr>posible{{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{-1,1},{1,-1},{1,1}};//eight direction moves;
int power(int a,int b){int res=1;while (b){if (b&1){res=(res*1ll*a)%MOD;}a=(a*1ll*a)%MOD;b>>=1;}return res;}
ll inversemod(ll base) { return power(base,MOD-2); }
void solve()
{
    ll n; cin>>n;
    vl v(n);
    ll ans = 0;

    loop
    {
        cin>>v[i];
        ans+=v[i];
    }
    vl tem=v;
    if(n==1)
    {
        cout<<v[0]<<nl;
        return;
    }
    // if(n==2)
    // {
    //     ll sum=0;
    //     for(int i=0;i<n;i++)
    //     {
    //         sum+=abs(v[i]);
    //     }
    //     cout<<sum<<nl;
    //     return;
    // }
    while(tem.size()>1)
    {
        tem.clear();
        for(int i=1;i<(int)v.size();i++)
        {
            tem.pb(v[i]-v[i-1]);
        }
        ll sum=accumulate(tem.begin(),tem.end(),0LL);
        ans=max({sum,-sum,ans});
        v.clear();
        v=tem;
    }
    ans=max(ans,tem[0]);
    cout<<ans<<nl;
}
int main()
{
    Ahsanul;
    test
    {
        //cout<<"Case "<<i<<": ";
        solve();
    }
}
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 327 ms):
#include<iostream>

#include<bits/stdc++.h>

using namespace std;



int ceiling(int a, int b){

    if (a%b==0)

    return a/b;

    else 

    return a/b+1;

}



void transfusion(vector<int>&arr, int n){

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

        int diff = abs(arr[i+1]-arr[i-1]);



        if (arr[i-1]<=arr[i+1])

        {

            if (abs(arr[i]-(arr[i-1]+diff/2)<abs(arr[i]-(arr[i-1]+ceiling(diff,2))))){

                arr[i-1]+=diff/2;

                arr[i+1]-=diff/2;

            }

            else {

                arr[i-1]+=ceiling(diff,2);

                arr[i+1]-=ceiling(diff,2);

            }

        }

        else{

            

            if (abs(arr[i]-(arr[i-1]-diff/2))<abs(arr[i]-(arr[i-1]-ceiling(diff,2)))){

                arr[i-1]-=diff/2;

                arr[i+1]+=diff/2;

            }   

            else{

                arr[i-1]-=ceiling(diff,2);

                arr[i+1]+=ceiling(diff,2);

            }

        }

    }

}



bool check(vector<int> arr, int n){

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

        if (arr[i]!=arr[i-1])

        return 0;

    }

    return 1;

}



int main()

   

{

    int t;

    cin >> t;

    while(t--){

    int n;

    cin >> n;

    vector<int> arr(n);

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

    cin >> arr[i];



    transfusion(arr,n);

    if (!check(arr,n))

    cout << "NO" << endl;

    else 

    cout << "YES" <<endl;

    }



}

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



using namespace std;



void solve() {

  int n;

  cin >> n;

  vector<int> arr(n);



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

    cin >> arr[i];



  long long odds = 0, evens = 0, total = 0;

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

    if (i % 2) {

      odds += arr[i];

    } else {

      evens += arr[i];

    }

  }

  int len_odd = n / 2, len_even = n / 2;

  if (n % 2) {

    len_even++;

  }



  if (odds % len_odd != 0 || evens % len_even != 0 ||

      odds / len_odd != evens / len_even) {

    cout << "NO" << endl;

  } else {

    cout << "YES" << endl;

  }

}



int main() {

  int t;

  cin >> t;

  while (t--) {

    solve();

  }

  return 0;

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

Solution 1 (runtime: 77 ms):
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#define int long long
#define endl '\n'
using namespace std;

signed main()
{
	int t;
	cin >> t;
	while (t--)
	{
		string x;
		cin >> x;
		for (int i = 0; i < x.size() - 1; i++)
		{
			if (x[i] < x[i + 1] - 1)
			{
				swap(x[i], x[i + 1]);
				x[i]--;
				if(i>=1)  i = i - 2;
			}
		}
		cout << x << endl;
	}
	return 0;
}

Solution 2 (runtime: 93 ms):


// Online C++ Compiler - Build, Compile and Run your C++ programs online in your favorite browser



#include<iostream>

#include<string>

#include<unordered_map>

#include<set>

#include<vector>

#include<algorithm>

#include<fstream>

#include<queue>

#include<map>

#include<math.h>



using namespace std;







void solve() {

int n;

string s;

cin >> s;



for (int i = 0; i < s.length(); i++) {

int pos = 0;

for (int j = 1; j <= 9; ++j) {

if (j + i >= s.length()) break;

if (s[i + j] - j > s[i + pos] - pos) pos = j;

}



s[pos + i] -= pos;

for (int j = i + pos; j > i; j--) {

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

}

}



cout << s <<"\n";

}



int main()

{

//ofstream fout;

//fout.open("out1.txt");

//for (int i = 1; i <= 10000000; i++) fout << i << ' ';



int t = 1;

cin >> t;

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

solve();

}

}
Problem: 2061D - Kevin and Numbers
Language: c++17 

Solution 1 (runtime: 405 ms):
#include <bits/stdc++.h>
#define imax INT_MAX
#define imin INT_MIN
#define lmax LLONG_MAX
#define lmin LLONG_MIN
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int maxn = 2e5 + 10;



void slu() {
	int na, nb;
	cin >> na >> nb;
	map<int, int, greater<int> > a, b;
	for (int i = 0; i < na; i++) {
		int t;
		cin >> t;
		a[t]++;
	}
	for (int i = 0; i < nb; i++) {
		int t;
		cin >> t;
		b[t]++;
	}
	while (!a.empty()) {
		while ((*a.begin()).first == (*b.begin()).first) {
			int tmp = (*a.begin()).first, num = min((*a.begin()).second, (*b.begin()).second);
			a[tmp] -= num;
			b[tmp] -= num;
			na -= num;
			nb -= num;
			if ((*a.begin()).second == 0) a.erase(tmp);
			if ((*b.begin()).second == 0) b.erase(tmp);
			if (!na and !nb) {
				cout << "Yes\n";
				return;
			}
			if (na < nb or !nb) {
				cout << "No\n";
				return;
			}
		}
		if ((*a.begin()).first > (*b.begin()).first) {
			cout << "No\n";
			return;
		}
		int tmp = (*b.begin()).first, num = (*b.begin()).second;
		if (tmp % 2) {
			b[tmp / 2] += num;
			b[tmp / 2 + 1] += num;
		}
		else b[tmp / 2] += 2 * num;
		nb += num;
		b.erase(tmp);

	}
	cout << "Yes\n";
	return;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int g;
	cin >> g;
	while (g--) slu();
	return 0;
}

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



using namespace std;



void solve() 

{

    int n, m;

    cin >> n >> m;



    priority_queue<long long> a, b;



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

        long long v;

        cin >> v;

        a.push(v);

    }



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

        long long v;

        cin >> v;

        b.push(v);

    }



    while(!a.empty() && !b.empty()) {

        long long x = a.top();

        long long y = b.top();



        if (x > y || a.size() < b.size()) {

            cout << "No" << endl;

            return;

        }



        if (x == y) {

            a.pop();

            b.pop();

        }

        else {

            b.pop();

            if (y & 1) {

                b.push(y / 2);

                b.push(y / 2 + 1);

            }

            else {

                b.push(y / 2);

                b.push(y / 2);

            }

        }

    }



    cout << (a.empty() && b.empty() ? "Yes" : "No") << endl;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);



    int t = 1;

    cin >> t;



    while (t--) {

        solve();

    }



    return 0;

}
Problem: 2049C - MEX Cycle
Language: c++17 

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



using namespace std;



void dbg_out() { cerr << endl; }

template <typename Head, typename... Tail>

void dbg_out(Head H, Tail... T) {

    cerr << ' ' << H;

    dbg_out(T...);

}

#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define FAST ios::sync_with_stdio(0), cin.tie(0)

#define pb push_back

#define mp make_pair

#define f first

#define s second

#define endl '\n'



typedef long long int  ll;

typedef unsigned long long int ull;

const int inf = (int)1e9;

const ll mod = 1e9 + 7;

const int MAXN = 200007;

//const double eps=numeric_limits<double>::epsilon();



void solve() {

    int n, x, y; cin >> n >> x >> y;

    --x; --y;

    int a[n];

    int f = 0;

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

        a[i] = f;

        f ^= 1;

    }

    if (a[n - 1] == 0) a[n - 1] = 2;



    auto print = [&]() -> void {

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

            cout << a[i] << ' ';

        }

        cout << endl;

        };



    if (a[x] != a[y]) {

        print();

        return;

    }



    if (x == 0) {

        a[x] = 2;

        if (a[n - 1] == 2) a[n - 1] = 0;

    }

    else {

        a[x] = 2;

    }



    print();

}



int main(void) {

    FAST;

    int t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}

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

#include <iostream>

#include <set>

#include <unordered_map>

#include <vector>

#include <algorithm>

#include <string>

#include <map>

#include <cmath>

#include <iomanip>

#include <optional>





using namespace std;



typedef long long int ll;

typedef long double ld;



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

#define IOS                                                                  \

    ios_base::sync_with_stdio(false);                                            \

    cin.tie(0);                                                                  \

    cout.tie(0);cout<<fixed<<setprecision(16);

#define pb push_back

#define mp make_pair





void solve() {



    ll n,x,y;

    cin>>n>>x>>y;



    vector<ll> v(n+1);

    ll counter = x, flip = 0;



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



        if(i==1 && n%2==1) v[counter] = 2;

        else {

            v[counter] = flip;

            flip = !flip;

        }

        counter = (counter%n) + 1;

    }



    if(v[y] == 0 && n%2 == 0) v[y] = 2;



    for(ll i=1; i<n+1; i++) cout<<v[i]<<" ";

    cout<<endl;



}







int main() {

    IOS

    int t;

    cin >> t;



    while(t--) solve();



    return 0;

}
Problem: 2049B - pspspsps
Language: c++17 

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

using namespace std;

#define int long long

#define endl '\n'



void solve(){

    int n;

    cin >> n;

    string a;

    cin >> a;

    a = ' ' + a;

    if (a[1] == 's'){

        a[1] = '.';

    }

    if (a[n] == 'p') {

        a[n] = '.';

    }

    int s = 0,p = 0;

    for(int k = 1;k <= n; ++k){

        if(a[k] == 's') ++ s;

        if (a[k] == 'p') ++ p;

    }

    if (s == 0 || p == 0) {

        cout << "YES" << endl;

    }

    else{

        cout << "NO"  << endl;

    }

    

}



signed main(){

    ios::sync_with_stdio(false);

    cin.tie(0); cout.tie(0);

    int T = 1;

    cin >> T;

    while(T--){

        solve();

    }

    return 0;

}

Solution 2 (runtime: 77 ms):
//bismillahhirrahmanirrahim

#include<bits/stdc++.h>

using namespace std;

#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

#define int long long



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

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

#define line cout<<endl



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

#define pb push_back

#define ppb pop_back

#define MOD 1000000007



const int MAX = 2000006;

int fact[MAX], invFact[MAX];



int modExp(int base, int exp, int mod) {

    int result = 1;

    while (exp > 0) {

        if (exp % 2 == 1) {

            result = (result * base) % mod;

        }

        base = (base * base) % mod;

        exp /= 2;

    }

    return result;

}



void precompute() {

    fact[0] = invFact[0] = 1;

    for (int i = 1; i < MAX; i++) {

        fact[i] = (fact[i - 1] * i) % MOD;

        invFact[i] = modExp(fact[i], MOD - 2, MOD);

    }

}



int nCr(int n, int r) {

    if (r > n | r < 0) return 0;

    return fact[n] * invFact[r] % MOD * invFact[n - r] % MOD;

}



int gcd(int a, int b)

{



    int result = min(a, b);

    while (result > 0) {

        if (a % result == 0 && b % result == 0) {

            break;

        }

        result--;

    }





    return result;

}

void solve()

{



      int n;

      cin>>n;

      string s;

      cin>>s;

      int ss=0;

      int pp=0;

      int idxs=0;

      int idxp=-1;

      for(int i=0;i<s.size();i++)

      {

          if(s[i]=='p')

          {

              pp++;

              if(idxp==-1)

              {





              idxp=i;



              }

          }

          else if(s[i]=='s')

          {

              ss++;

              idxs=i;

          }

      }

      if(pp==0 or ss==0)

      {

          yes;



      }

      else if(idxs==0 or idxp==n-1)

      {

          yes;



      }

      else

      {

          no;



      }





}



signed main()

{

    int t;

    cin>>t;

    while(t--)

    {

      solve();

    }

}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 592 ms):
#include<bits/stdc++.h>
 
#define ll long long int
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 
using namespace std;

void solve() {
    int n; cin >> n;
    map<int, set<int>> tree;

    for(int i=0;i<n-1;i++) {
        int u, v;
        cin >> u >> v;

        tree[u].insert(v);
        tree[v].insert(u);
    }

    int max_indegree = 0;
    int max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    int ans = max_indegree; 
    int count = 0;
    for(auto nei:tree[max_ind_node]) {
        if(tree[nei].size() == max_indegree) count++;
        tree[nei].erase(max_ind_node);
    }
    tree.erase(max_ind_node);

    if(count >= 2) {
        cout << max_indegree*2 - 1 << endl;
        return;
    }

    int last = max_ind_node;
    max_indegree = 0;
    max_ind_node = -1;
    for(auto& [k, v]:tree) {
        if(v.size()>max_indegree) {
            max_indegree = v.size();
            max_ind_node = k;
        }
    }
    ans += max_indegree - 1;
    cout << ans << endl;
}

int main()
{
    fast;
 
    ll t = 1;
    cin >> t;
    while(t--) {
        solve();
    }    
 
}


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

bool isPath(int parent, int child, vector<vector<int>>& adj, vector<int>& vis){
	for(auto it : adj[parent]){
		if(vis[it] == 0){
			vis[it] = 1;
			if(child == it) return true;
			else isPath(it, child, adj, vis);
		}
	}
	return false;
}

void solve(){
	int n;
	cin >> n;
	vector<vector<int>> adj(n + 1);
	set<pair<int, int>> st;
	for(int i = 0; i < n - 1; i++){
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		st.insert({u, v});
		st.insert({v, u});
	}

	vector<pair<int, int>> node;

	for(int i = 1; i <= n; i++){
		node.push_back({adj[i].size(), i});
	}
	sort(node.rbegin(), node.rend());

	int ans = 0;
	for(int i = 0; i < node.size(); i++){
		for(int j = i + 1; j < node.size(); j++){
			if(st.find({node[i].second, node[j].second}) == st.end()){
				ans = max(ans, node[i].first + node[j].first - 1);
				break;
			}
			else ans = max(ans, node[i].first + node[j].first - 2);
		}
	}
	
	cout << ans << endl;
}

signed main() {

#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
#endif

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