ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        int sum = 0;

        while (n > 0) {
            int temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }

        bool possible = false;

        for (int i = 0; i <= count2 && !possible; i++) { 
            for (int j = 0; j <= count3 && !possible; j++) { 
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        int sum = 0;

        while (n > 0) {
            int temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (int i = 0; i <= count2 && !possible; i++) { 
            for (int j = 0; j <= count3 && !possible; j++) { 
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
            if(possible){break;}
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        int sum = 0;

        while (n > 0) {
            int temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (int i = 0; i <= count2 && !possible; i++) { 
            for (int j = 0; j <= count3 && !possible; j++) { 
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
            if(possible){break;}
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        ll sum = 0;

        while (n > 0) {
            ll temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        ll count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (ll i = 0; i <= count2 && !possible; i++) { 
            for (ll j = 0; j <= count3 && !possible; j++) { 
                ll newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
            if(possible){break;}
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        int sum = 0;

        while (n > 0) {
            int temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }

        bool possible = false;

        for (int i = 0; i <= count2 && !possible; i++) { 
            for (int j = 0; j <= count3 && !possible; j++) { 
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 8):
#include <bits/stdc++.h>
using namespace std;

int main() 
{
    int t;
    cin>>t;
    while(t--){
      int n,l,r;
      cin>>n>>l>>r;
      int ar[n];
      for(int i=0; i<n; i++){
        cin>>ar[i];
      }
      if(l!=1) sort(ar,ar+l-1);
      sort(ar+l-1,ar+r);
      sort(ar+r,ar+n);
      int sum=0;
      int count=0;
      int i=0,j=l-1;
      while(count<r-l+1){
        if(ar[i]<ar[j] && i<l-1){
          sum+=ar[i];
          i++;
        }
        else{
          sum+=ar[j];
          j++;
        }
        count++;
      }
      count=0;
      int ans1=0;
      i=l-1,j=r;
      while(count<r-l+1){
        if(ar[i]<ar[j] || j>=n){
          ans1+=ar[i];
          i++;
        }
        else{
          ans1+=ar[j];
          j++;
        }
        count++;
      }
      cout<<min(sum,ans1)<<"\n";
    }
    return 0;
}

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

int main() 
{
    int t;
    cin>>t;
    while(t--){
      int n,l,r;
      cin>>n>>l>>r;
      int ar[n];
      for(int i=0; i<n; i++){
        cin>>ar[i];
      }
      if(l!=1) sort(ar,ar+l-1);
      sort(ar+l-1,ar+r);
      sort(ar+r,ar+n);
      long long sum=0;
      int count=0;
      int i=0,j=l-1;
      while(count<r-l+1){
        if(ar[i]<ar[j] && i<l-1){
          sum+=(long long)ar[i];
          i++;
        }
        else{
          sum+=(long long)ar[j];
          j++;
        }
        count++;
      }
      count=0;
      long long ans1=0;
      i=l-1,j=r;
      while(count<r-l+1){
        if(ar[i]<ar[j] || j>=n){
          ans1+=(long long)ar[i];
          i++;
        }
        else{
          ans1+=(long long)ar[j];
          j++;
        }
        count++;
      }
      cout<<min(sum,ans1)<<"\n";
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)


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

int main() 
{
    int t;
    cin>>t;
    while(t--){
      int n,l,r;
      cin>>n>>l>>r;
      int ar[n];
      for(int i=0; i<n; i++){
        cin>>ar[i];
      }
      sort(ar,ar+n);
      int ans=0;
      for(int i=0; i<r-l+1; i++){
        ans+=ar[i];
      }
      cout<<ans<<"\n";
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

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

int main() 
{
    int t;
    cin>>t;
    while(t--){
      int n,l,r;
      cin>>n>>l>>r;
      int ar[n];
      for(int i=0; i<n; i++){
        cin>>ar[i];
      }
      sort(ar,ar+n);
      int ans=0;
      for(int i=0; i<r-l+1; i++){
        ans+=ar[i];
      }
      cout<<ans<<"\n";
    }
    return 0;
}

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

int main() 
{
    int t;
    cin>>t;
    while(t--){
      int n,l,r;
      cin>>n>>l>>r;
      int ar[n];
      for(int i=0; i<n; i++){
        cin>>ar[i];
      }
      if(l!=1) sort(ar,ar+l-1);
      sort(ar+l-1,ar+r);
      sort(ar+r,ar+n);
      int sum=0;
      int count=0;
      int i=0,j=l-1;
      while(count<r-l+1){
        if(ar[i]<ar[j] && i<l-1){
          sum+=ar[i];
          i++;
        }
        else{
          sum+=ar[j];
          j++;
        }
        count++;
      }
      count=0;
      int ans1=0;
      i=l-1,j=r;
      while(count<r-l+1){
        if(ar[i]<ar[j] || j>=n){
          ans1+=ar[i];
          i++;
        }
        else{
          ans1+=ar[j];
          j++;
        }
        count++;
      }
      cout<<min(sum,ans1)<<"\n";
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define pf push_front
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
constexpr ll MOD = 1000000007;
string a, b, c;
ll f(ll inda, ll indb, ll indc, vector<ll> &dp)
{
     if(indc == c.size() - 1)
     {
          if(inda == a.size())return !(c[indc] == b[indb]);
          else return !(c[indc] == a[inda]);
     }
     if(dp[indc] != -1)dp[indc];
     ll p = !(a[inda] == c[indc]) + f(inda+1, indb, indc+1, dp);
     ll q = !(b[indb] == c[indc]) + f(inda, indb+1, indc+1, dp);
     return dp[indc] = min(p, q);
}
int32_t main() {
Hello;

          ll t;
          cin >> t;
          while(t--)
          {
               cin >> a >> b >> c;
               vector<ll> dp(c.size()+5, -1);
               cout << f(0, 0, 0, dp) << endl;;
          }
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 6):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define pf push_front
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
constexpr ll MOD = 1000000007;
string a, b, c;
ll f(ll inda, ll indb, ll indc, vector<ll> &dp)
{
     if(indc == c.size() - 1)
     {
          if(inda == a.size())return !(c[indc] == b[indb]);
          else return !(c[indc] == a[inda]);
     }
     if(dp[indc] != -1)dp[indc];
     ll p = 1e9, q = 1e9;
     if(inda != a.size()) p = !(a[inda] == c[indc]) + f(inda+1, indb, indc+1, dp);
     if(indb != b.size()) q = !(b[indb] == c[indc]) + f(inda, indb+1, indc+1, dp);
     return dp[indc] = min(p, q);
}
int32_t main() {
Hello;

          ll t;
          cin >> t;
          while(t--)
          {
               cin >> a >> b >> c;
               vector<ll> dp(c.size()+5, -1);
               cout << f(0, 0, 0, dp) << endl;;
          }
}




Submission (verdict: Accepted):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define pf push_front
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
constexpr ll MOD = 1000000007;
string a, b, c;
ll f(ll i, ll k, vector<vector<ll> >& dp)
{
     if(i == a.size() and k == b.size())return 0;
     if(dp[i][k] != -1)return dp[i][k];
     ll p = 1e9, q = 1e9;
     if(i != a.size())p = f(i+1, k, dp) + (c[i+k] != a[i]);
     if(k != b.size())q = f(i, k+1, dp) + (c[i+k] != b[k]);
     return dp[i][k] = min(p, q);
}
int32_t main() {
Hello;

          ll t;
          cin >> t;
          while(t--)
          {
               cin >> a >> b >> c;
               vector<vector<ll> > dp(a.size()+5, vector<ll> (b.size()+2, -1));
               cout << f(0, 0, dp) << endl;
          }
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define pf push_front
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
constexpr ll MOD = 1000000007;
string a, b, c;
ll f(ll inda, ll indb, ll indc, vector<ll> &dp)
{
     if(indc == c.size() - 1)
     {
          if(inda == a.size())return !(c[indc] == b[indb]);
          else return !(c[indc] == a[inda]);
     }
     if(dp[indc] != -1)dp[indc];
     ll p = !(a[inda] == c[indc]) + f(inda+1, indb, indc+1, dp);
     ll q = !(b[indb] == c[indc]) + f(inda, indb+1, indc+1, dp);
     return dp[indc] = min(p, q);
}
int32_t main() {
Hello;

          ll t;
          cin >> t;
          while(t--)
          {
               cin >> a >> b >> c;
               vector<ll> dp(c.size()+5, -1);
               cout << f(0, 0, 0, dp) << endl;;
          }
}




Submission (verdict: Time limit exceeded on test 6):
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define pf push_front
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
constexpr ll MOD = 1000000007;
string a, b, c;
ll f(ll inda, ll indb, ll indc, vector<ll> &dp)
{
     if(indc == c.size() - 1)
     {
          if(inda == a.size())return !(c[indc] == b[indb]);
          else return !(c[indc] == a[inda]);
     }
     if(dp[indc] != -1)dp[indc];
     ll p = 1e9, q = 1e9;
     if(inda != a.size()) p = !(a[inda] == c[indc]) + f(inda+1, indb, indc+1, dp);
     if(indb != b.size()) q = !(b[indb] == c[indc]) + f(inda, indb+1, indc+1, dp);
     return dp[indc] = min(p, q);
}
int32_t main() {
Hello;

          ll t;
          cin >> t;
          while(t--)
          {
               cin >> a >> b >> c;
               vector<ll> dp(c.size()+5, -1);
               cout << f(0, 0, 0, dp) << endl;;
          }
}
Problem: 2063B - Subsequence Update
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 8):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<int> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<int> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

}

Submission (verdict: Wrong answer on test 8):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

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

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        for(auto& el : left)

		std::cout << el << ' ';

std::cout << "\n";

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

}

Submission (verdict: Accepted):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0ULL), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0ULL)) << "\n";

    }

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

Previous submission (verdict: Wrong answer on test 8):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

}

Submission (verdict: Wrong answer on test 1):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<unsigned long long> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<unsigned long long> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        for(auto& el : left)

		std::cout << el << ' ';

std::cout << "\n";

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

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


Submission (verdict: Wrong answer on test 8):
#include <iostream>

#include <numeric>

#include <algorithm>

#include <vector>

#include <ranges>



int main()

{

    std::size_t tc, n, l, r;

    std::cin >> tc;

    for(std::size_t i{0}; i<tc; ++i)

    {

        std::cin >> n >> l >> r;

        std::vector<int> seq(n);

        for(auto& el : seq) 

            std::cin >> el;

        std::vector<int> left(seq.begin(), seq.begin()+r),

        right(seq.begin()+l-1, seq.end());

        std::ranges::sort(left);

        std::ranges::sort(right);

        

        std::cout << std::min(std::accumulate(left.begin(), left.begin()+r-l+1, 0), 

                     std::accumulate(right.begin(), right.begin()+r-l+1, 0)) << "\n";

    }

}
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 ll long long

void solve(int n) {
    vector<vector<int>> adj(n+1);
    vector<int> deg(n+1, 0);
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++; deg[b]++;
    }
    int mx1 = 0;
    for (int i = 1; i <= n; i++) if (deg[i] > deg[mx1]) mx1 = i;
    int ans = deg[mx1];
    deg[mx1] = 0;
    for (int u : adj[mx1]) deg[u]--;
    int mx2 = 0;
    for (int i = 1; i <= n; i++) if (deg[i] > deg[mx2]) mx2 = i;
    ans += deg[mx2] - 1;
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        solve(n);
    }

    return 0;
}

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

void solve(int n) {
    vector<vector<int>> adj(n+1);
    vector<int> deg(n+1, 0);
    for (int i = 0; i < n-1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        deg[a]++; deg[b]++;
    }
    int mx1 = 0;
    for (int i = 1; i <= n; i++) if (deg[i] > deg[mx1]) mx1 = i;
    int ans = deg[mx1] - 1;
    deg[mx1] = 0;
    for (int u : adj[mx1]) deg[u]--;
    int mx2 = 0;
    for (int i = 1; i <= n; i++) if (deg[i] > deg[mx2]) mx2 = i;
    ans += deg[mx2];
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
    int tt;
    cin >> tt;
    while (tt--) {
        int n;
        cin >> n;
        solve(n);
    }

    return 0;
}