ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vi vector<int>
#define t(ok) ok?cout<< YES<<endl:cout<<NO<<endl;
#define vll vector<long long>
#define vpll vector <pair<ll,ll>>
#define mll map<long long, long long>
#define all(x) x.begin(), x.end()
#define forn(i,n) for(int i = 0; i < n; i++)
#define mii map<int, int>
#define vc vector<char>
#define py cout << "YES" << endl
#define pn cout << "NO" << endl
#define vb vector<bool>
#define invec for (int i=0;i<n;i++) {cin>>v[i];} 
#define print_arr(vec, sz) for(int zwzq = 0; zwzq < sz ; zwzq++){cout << vec[zwzq] << ' ';}cout << endl;
#define fori(i, a, c) for(int i = a ; i < c ; i++)
#define ford(i, n, a) for (int i = n ; i >= a ; i--)
#define print(vec) for(int zwzq = 0; zwzq < vec.size() ; zwzq++){cout << vec[zwzq] << ' ';}cout << endl;
#define print_pair(vec) for(int zwzq = 0; zwzq < vec.size() ; zwzq++){cout << '[' <<vec[zwzq].first << ',' << vec[zwzq].second << ']' << ' ';}cout << endl;
const ll mod = 998244353;
using namespace std;
long long mod_sub(long long a, long long b, long long m){a = a % m; b = b % m;return (((a - b) % m) + m) % m;}
long long powermod(long long x, long long y, long long p){
    long long res = 1;
    x = x % p;
    if (x == 0){return 0;}
    while (y > 0){
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
   return res;
}


//for first see soln,for second try reverse or smaller optimal method, BIT question write one or two example in bit then solve

void solve ( vector<ll>v,ll &ans){
    ans = max(ans,accumulate(all(v),0LL));
    if (v.size()==1){
        ans =max(v[0],ans);
        return;
    }
    if (v[1]-v[0] > v[v.size()-2]-v[v.size()-1]){
        for ( int i=0;i<v.size()-1;i++){
            v[i] = v[i+1] - v[i];
        }
        v.pop_back();
        
         solve(v,ans);
    }
    else{
        reverse(all(v));
        for ( int i=0;i<v.size()-1;i++){
            v[i] = v[i+1] - v[i];
        }
        v.pop_back();
        
         solve(v,ans);
    }



}
int main(){

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

    ll t;
    cin >> t;
    while (t--){


ll n;
    cin  >> n;
    vll v(n);
     for (int i=0;i<n;i++) {ll k ;cin>>k;v[i]=k;}
     ll ans = INT_MIN;
        solve(v,ans);
        cout<<ans<<endl;

    }
    return 0;
}

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

const int N = 2e5+5;
ll a[N];
ll b[N];

void _main(){
	int n;
	cin>>n;
	ll ans=0;
	for(int i=1;i<=n;i++)
		cin>>a[i],ans+=a[i],b[i]=0;
	for(;n!=1;n--){
		ll sum=0;
		for(int j=2;j<=n;j++)
			b[j-1]=a[j]-a[j-1],sum+=b[j-1];
		for(int j=1;j<=n-1;j++)
			a[j]=b[j];
		ans=max(ans,max(sum,-sum));
	}
	cout<<ans<<"\n";
	return;
}
int main(){
	int t;
	cin>>t;
	while(t--)
		_main();
	return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 249 ms):
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    vector<int> freq_count;
    int count = 1;

    for (int i = 1; i < n; i++) {
        if (a[i] == a[i - 1]) {
            count++;
        } else {
            freq_count.push_back(count);
            count = 1;
        }
    }
    freq_count.push_back(count);  
    sort(freq_count.begin(), freq_count.end());

    int distinct_count = freq_count.size();
    for (int i = 0; i < distinct_count - 1; i++) {
        if (freq_count[i] > k) {
            cout << distinct_count - i << "\n";
            return;
        }
        k -= freq_count[i];
    }

    cout << 1 << "\n";
}

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

    return 0;
}


Solution 2 (runtime: 280 ms):
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ULL;
const int N=4e5 + 5;
using namespace std;
vector<int>vv;
void solve(){
  int a,b,sum=0;
  cin>>a>>b;
  set<int>se;
  map<int,int>m;
  for(int i=0;i<a;i++){
    int c;
    cin>>c;
    se.insert(c);
    m[c]++;   
  }
  vector<int>v;
  for(auto i:se){
    v.push_back(m[i]);
  }
  sort(v.begin(),v.end());
  for(int i=0;i<v.size();i++){
    if(v[i]<=b){
      b-=v[i];
      continue;
    }
    sum++;
 
  }
  if(sum==0)sum++;
  vv.push_back(sum);
}
int main(){
  int t;
  cin>>t;
  while(t--)solve();
  for(auto i:vv)cout<<i<<'\n';
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 906 ms):
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;


int main() {
    int t; cin >> t;
    while(t--){
        ll n;
        cin >> n;
        set<pair<ll,ll>> edges;
        vector<ll> deg(n+1);
        for(int i = 0; i < n-1; i++) {
            ll a,b;
            cin >> a >> b;
            edges.insert({a,b});
            edges.insert({b,a});
            deg[a]++;
            deg[b]++;
        }
        ll ans = 0;
        vector<pair<ll,ll>> b;
        for(int i = 0; i < n; i++) {
            b.push_back({deg[i+1], i+1});
        }
        sort(b.begin(), b.end(), greater<pair<ll,ll>>());
        for(int i = 0; i < n; i++) {
            for(ll j = i+1; j < n; j++) {
                if(edges.find({b[i].second, b[j].second}) == edges.end()) {
                    ans = max(ans, b[j].first + b[i].first - 1);
                    break;
                }
            }
        }

        for(auto x : edges) {
            ans = max(ans, deg[x.first] + deg[x.second] - 2);
        }
        cout << ans << endl;

    }

    return 0;
}



/*
ll gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a%b);
}

ll lcm(int a, int b){
    return a*b / gcd(a,b);
}
*/
/*
int knapSack(int W, const vector<int>& weights, const vector<int>& values, int n) {

    vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));

    // Build the DP table
    for (int i = 1; i <= n; i++) {
        for (int w = 0; w <= W; w++) {
            if (weights[i - 1] <= w) {
                // Include the item or exclude it
                dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]);
            } else {
                // Exclude the item
                dp[i][w] = dp[i - 1][w];
            }
        }
    }

    // The last cell of the DP table contains the maximum value
    return dp[n][W];
}
*/

Solution 2 (runtime: 718 ms):
#include <iostream>
#include <cmath>
#include<bits/stdc++.h>
#define int long long
#define inf 1e9
#define pii pair<int,int>
using namespace std;

const int N = 500 + 10 ;

int qui(int n, int k) {
	int ans = 1 ;
	while (k) {
		if (k & 1) {
			ans *= n ;
		}
		n *= n;
		k /= 2 ;
	}

	return ans ;

}

void solve() {
	int n ;
	cin >> n ;

	map<pii, bool> judge;
	vector<int> cnt(n + 1, 0);

	for (int i = 1 ; i < n ; i++) {
		int a, b ;
		cin >> a >> b ;

		cnt[a] ++ ;
		cnt[b] ++ ;

		judge[ {a, b}] = true;
		judge[ {b, a}] = true;

	}

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

	vector<pair<int, int>> c;
	for (int i = 1 ; i <= n ; i++) {
		c.push_back({cnt[i], i});
	}
	sort(c.begin(), c.end(), greater<pii>());

	int ans = 0;
	for (int i = 1 ; i < n ; i++) {
		ans = max(ans, c[0].first + c[i].first - (judge[ {c[i].second, c[0].second}]));
	}

	for (int i = 2 ; i < n ; i++) {
		ans = max(ans, c[1].first + c[i].first - (judge[ {c[i].second, c[1].second}]));
	}
	
	cout << ans - 1<< "\n";
}

signed main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0), std::cout.tie(0);
	int t ;
	cin >> t;

	while (t -- ) {
		solve();
	}

	return 0;

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

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

using namespace std;

int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int n,x,y;

        cin>>n>>x>>y;

        x--; 

        y--;

        vector<int> ans(n);

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

        {

            ans[(x+i)%n] = i%2;

        }

        if(n % 2 || (x - y) % 2 == 0)

        {

            ans[x] = 2;

        }

         for (auto x : ans)cout << x << ' ';

         cout<<endl;

    }

    return 0;

}

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

using namespace std;

void solve(int n, int x, int y) {

    vector<vector<int>> adj(n + 1, vector<int>());

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

        int prev = i - 1;

        if (prev == 0) {

            prev = n;

        }

        int next = i + 1;

        if (next > n) {

            next = 1;

        }

        adj[i].push_back(prev);

        adj[i].push_back(next);

    }

    auto is_adjacent = [&](int u, int v) -> bool {

        return (u == v - 1) || (u == v + 1) || (u == 1 && v == n) || (u == n && v == 1);

    };

    if (!is_adjacent(x, y)) {

        adj[x].push_back(y);

        adj[y].push_back(x);

    }

    vector<int> labels(n + 1, 0);

    queue<int> q;

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

        q.push(i);

    }

    vector<bool> in_queue_flag(n + 1, true);

    while (!q.empty()) {

        int u = q.front();

        q.pop();

        in_queue_flag[u] = false;

        int mask = 0;

        for (auto &v : adj[u]) {

            if (labels[v] < 32) {

                mask |= (1 << labels[v]);

            }

        }

        int mex = 0;

        while (mask & (1 << mex)) {

            ++mex;

        }

        if (labels[u] != mex) {

            labels[u] = mex;

            for (auto &v : adj[u]) {

                if (!in_queue_flag[v]) {

                    q.push(v);

                    in_queue_flag[v] = true;

                }

            }

        }

    }

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

        cout << labels[i] << (i < n ? " " : "\n");

    }

}

int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int t;

    cin >> t;

    while (t--) {

        int n, x, y;

        cin >> n >> x >> y;

        solve(n, x, y);

    }

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

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

#include <iostream>

#include <queue>

#include <algorithm>

#include <stack>

#include <set>

#include <list>

#include <map>

#include <cstring>

#include <iterator>

#define ll long long

#define MOD 1000000007 

using namespace std;

vector<int>sieve(int n){

    vector<int>p;

    vector<int>isprime(n+1, 1);

    isprime[0] = isprime[1] = 0;

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

        if (isprime[i]){

            for (int j = 2*i; j<=n; j+=i){

                isprime[j] = 0;

            }

        }

    }

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

        if (isprime[i]){

            p.push_back(i);

        }

    }

    return p;

}

//---------------------------------------------------------------------------

// bool check(int k, vector<int>d, vector<int>a){



// }



void solve(){

    int n, x, y;

    cin>>n>>x>>y;

    if (abs(x%n - y%n) == 1){

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

            cout<<i%2<<" ";

        }

        if (n%2 == 1){

            cout<<2;

        }

        cout<<endl;

        return;

    }

    vector<int>a(n, 0);

    int l1 = y - x - 1;

    if (l1%2 == 0 && n%2 == 0){

        a[y%n] = 1;

    }

    else{

        a[y%n] = 2;

    }

    for (int i = x+1; i<y; i+=2){

        a[i] = 1;

    }

    for (int i = n+x-1; i>y; i-=2){

        a[i%n] = 1;

    }

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

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

    }cout<<a[0]<<endl;



}



int main(){

    int t;

    cin>>t;

    while(t--){

        solve();

    }

    // solve();

}

Solution 2 (runtime: 78 ms):
#include<iostream>

#include<vector>

using namespace std;

const int N=2e5+10;

int a[N];

void solve()

{

	int n,x,y;

	cin>>n>>x>>y;

	x--;y--;

	int num=0;

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

	{

		a[i]=num;

		num^=1;

	}

	if(n%2==0)

	{

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

		for(int i=0;i<n;i++)cout<<a[i]<<' ';

		cout<<endl;

	}

	else

	{

		a[x]=2;

		num=1;

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

		{

			a[i]=num;

			num^=1;

		}

		num=0;

		for(int i=x-1;i>=0;i--)

		{

			a[i]=num;

			num^=1;

		}

		for(int i=0;i<n;i++)cout<<a[i]<<' ';

		cout<<endl; 

	}

}

int main()

{

	ios::sync_with_stdio(false);

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

	int t;

	cin>>t;

	while(t--)

	{

		solve();

	}

	return 0; 

}
Problem: 2050E - Three Strings
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<iostream>
#define INF 1000000009

using namespace std;


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

    while(t--){
        
        string a,b,c;
        cin>>a;
        cin>>b;
        cin>>c;
        int as = a.length();
        int bs = b.length();
        int dp[as+1][bs+1];


        dp[0][0] = 0;

        for(int i = 1; i <= as; i ++){
            if(a[i-1] != c[i-1])dp[i][0] = 1 + dp[i-1][0];
            else dp[i][0] = dp[i-1][0];
            
        }
        for(int j = 1; j <= bs; j ++){
            if(b[j-1] != c[j-1]) dp[0][j] = 1 + dp[0][j-1];
            else dp[0][j] =dp[0][j-1];
            
        }

        for(int i = 1; i <= as; i++){
            for(int j = 1; j <=bs; j++){
                dp[i][j] = INF;
                if(c[i+j-1] == a[i-1]){
                    dp[i][j] = min(dp[i][j], dp[i-1][j]);
                }
                
                if(c[i+j-1] == b[j-1]){
                    dp[i][j] = min(dp[i][j], dp[i][j-1]);
                }
               
                dp[i][j] = min(dp[i][j], 1+ min( dp[i][j-1], dp[i-1][j]));
                


            }
        }

    


        cout<<dp[as][bs]<<"\n";

    }



    return 0;
}

Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>
 
using namespace std;
 
void solve() {
  string a, b, c;
  cin >> a >> b >> c;
  int sz1 = a.size(), sz2 = b.size(), n = c.size();
  int s[sz1 + 1][sz2 + 1];
  s[0][0] = 0;
  for (int i = 0; i < sz1; i++) {
    s[i + 1][0] = (a[i] != c[i]) + s[i][0];
  }
  for (int i = 0; i < sz2; i++) {
    s[0][i + 1] = (b[i] != c[i]) + s[0][i];
  }
  for (int i = 1; i <= sz1; i++) {
    for (int j = 1; j <= sz2; j++) {
      s[i][j] = min(s[i - 1][j] + (a[i - 1] != c[i + j - 1]), s[i][j - 1] + (b[j - 1] != c[i + j - 1]));
    }
  }
  cout << s[sz1][sz2] << endl;
}
 
int main () {
  ios::sync_with_stdio(false), cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    solve();
    #ifndef ONLINE_JUDGE
      cout << "-----------------------------" << endl;
    #endif
  }
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 124 ms):
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
signed main()
{
#ifdef ONPC
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int test = 1;
    cin >> test;
    for (int t = 0; t < test; t++)
    {
        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++)
        {
            int mn = min(a[i], a[i + 1]);
            a[i] -= mn;
            a[i + 1] -= mn;
        }
        if (is_sorted(all(a)))
        {
            cout << "YES\n";
        }
        else
        {
            cout << "NO\n";
        }
    }

    return 0;
}

Solution 2 (runtime: 343 ms):
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int>v(n);
    for(auto &i:v)  
        cin >> i;
    for(int i=1; i<n; ++i)
    {
        if(v[i-1] > v[i])
        {
            cout << "NO" << endl;
            return;
        }
        v[i] -= v[i-1];
    }
    cout << "YES" << endl;
}

int main()
{
    int T = 1;
    cin >> T;
    for (int TC = 1; TC <= T; ++TC)
    {
      // cout<<"Case "<<TC<<": ";
      solve();
    }
}
Problem: 2061D - Kevin and Numbers
Language: c++17 

Solution 1 (runtime: 843 ms):
#include <iostream>
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
#define int long long


void solve(){
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    
    for(int i = 0; i<n; i++) cin >> a[i];
    for(int i = 0; i<m; i++) cin >> b[i];

    map<int, int> freqa, freqb;
    set<int> seta, setb;

    for(auto ele: a){
        freqa[ele]++;
        seta.insert(ele);
    }

    for(auto ele: b){
        freqb[ele]++;
        setb.insert(ele);
    }

    while(true){
        if(seta.empty() && setb.empty()){
            cout << "YES" << endl;
            return;
        }

        if(seta.empty() || setb.empty()){
            cout << "NO" << endl;
            return;
        }

        int max1 = *prev(seta.end());
        int max2 = *prev(setb.end());

        if(max1 > max2){
            cout << "NO" << endl;
            return;
        }

        if(freqa[max2]){
            int minfreq = min(freqa[max2], freqb[max2]);
            freqa[max2] = freqa[max2] - minfreq;
            freqb[max2] = freqb[max2] - minfreq;

            if(freqa[max2] == 0) seta.erase(max2);
            if(freqb[max2] == 0) setb.erase(max2);
        }

        else{
            int cl = (max2 + 1)/2;
            int fl = max2/2;
            freqb[cl] = freqb[cl] + freqb[max2];
            freqb[fl] = freqb[fl] + freqb[max2];
            freqb[max2] = 0;
            setb.insert(cl);
            setb.insert(fl);
            setb.erase(max2);
        }
    }
}


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

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



using namespace std;

priority_queue<int> q;

map<int, int> M;



int main()

{   ios::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    int t, n, m, a, ok;

    cin>>t;



    while(t)

    {cin>>n>>m;

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

     {cin>>a;

      M[a]--;

     }



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

     {cin>>a;

      M[a]++;

      if(M[a]==1)q.push(a);

     }



     ok=1;

     while(q.empty()!=1)

     {a=q.top();

      q.pop();



      if(a!=1 && M[a]>0)

      {int a1=a/2, b1=a/2;

       if(a%2==1)a1++;



       int v1=M[a1], v2=M[b1], v3=M[a];



       if(a1!=b1)

       {if(v1<=0 && v1+v3>0)q.push(a1);

        if(v2<=0 && v2+v3>0)q.push(b1);

       }

       else if(v1<=0 && v2+v3*2>0)q.push(a1);





       M[a1]+=v3, M[b1]+=v3;

       M[a]=0;

      }

     }



     map<int, int>:: iterator I;

     for(I=M.begin();I!=M.end() && ok;I++)

     if(I->second!=0)ok=0;

     M.clear();



     if(ok)cout<<"YES\n";

     else cout<<"NO\n";

     t--;

    }



    return 0;

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

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

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif 

 

void solve()
{	
  int n;
  cin >> n;
  vector<vector<int>>v(n+1);
  vector<int>deg(n+1,0);
  for(int i = 0; i < n - 1; i++){
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
    deg[a]++;
    deg[b]++;
  }
  int ans = -1;
  multiset<int,greater<int>> m;
  for(int i = 1; i <= n; i++)
    m.insert(deg[i]);
  // debug(deg);
  // debug(v);
  // for(auto it : m) cout<< it <<" ";
  //   cout<<endl;
  for(int i = 1; i <= n; i++)
  {
     m.erase(m.find(deg[i]));
     for(auto it : v[i]){
        ans = max(ans,deg[it] + deg[i] - 2);
       m.erase(m.find(deg[it]));
     }
     auto it = m.begin();
     if(m.size())
     ans = max(ans, *it+deg[i] - 1);
     m.insert(deg[i]);
     for(auto it : v[i]){
      m.insert(deg[it]);
     }
  }

  cout<< ans <<endl;

}

signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int tt = 1;
	cin >> tt;
	while(tt--)
	{
	  solve();
	}
}

// local comment chesava

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

bool comp(pair<int, int> a, pair<int, int> b) {
    return a.first > b.first;
}

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

    sort(deg.begin(), deg.end(), comp);

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

    if(deg[1].first > deg[2].first) {
        if(adj[deg[0].second].find(deg[1].second) != adj[deg[0].second].end()) {
            cout << deg[0].first + deg[1].first - 2 << endl;
            return;
        }
        cout << deg[0].first + deg[1].first - 1 << endl;
        return;
    }

    if(deg[0].first == deg[1].first && deg[1].first == deg[2].first) {
        cout << deg[0].first + deg[1].first - 1 << endl;
        return;
    }   

    int ans = 0;
    if(adj[deg[0].second].find(deg[1].second) != adj[deg[0].second].end()) {
        ans = deg[0].first + deg[1].first - 2;
    } else {
        ans = deg[0].first + deg[1].first - 1;
    }

    int i = 2;
    while(i < n && deg[i].first == deg[1].first) {
        if(adj[deg[0].second].find(deg[i].second) != adj[deg[0].second].end()) {
            ans = max(ans, deg[0].first + deg[i].first - 2);
        } else {
            ans = max(ans, deg[0].first + deg[i].first - 1);
        }
        i++;
    }

    cout << ans << endl;
}

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