ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)


Submission (verdict: Time limit exceeded on test 2):
#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

int dp[100005][10];
bool f(int i, int cur, vector<int> &v, int sum)
{
    if (i >= v.size())
        return 0;
    // dbg(dp[i][cur]);

    if (dp[i][cur] != -1)
        return dp[i][cur];
    int x = cur;
    cur += v[i];
    if ((cur % 9) == (9 - sum))
        return 1;
    x %= 9;
    cur %= 9;
    return dp[i][cur] = f(i + 1, cur, v, sum) || f(i + 1, x, v, sum);
}
void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> h;
    int sum = 0;
    vector<int> v;
    for (char &c : h)
    {
        int x = c - '0';
        if (x <= 3)
        {
            if (x == 2)
                v.push_back(2);
            if (x == 3)
                v.push_back(6);
        }
        sum += x;
    }
    sum %= 9;
    dbg(sum, v);
    if (!sum)
    {
        cout << "YES" << endl;
        return;
    }
    memset(dp, -1, sizeof(dp));
    if (f(0, 0, v, sum))
    {
        cout << "YES" << endl;
    }
    else
        cout << "NO" << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    int node, maxx = -1;
    for (int i = 0; i < n; i++)
    {
        if ((int)g[i].size() > maxx)
        {
            maxx = g[i].size();
            node = i;
        }
    }

    vector<vector<int>> g1(n);
    for (int i = 0; i < n; i++)
    {
        if (i == node)
            continue;
        for (auto &j : g[i])
            if (j != node)
                g1[i].push_back(j);
    }
    swap(g, g1);
    dbg(g);

    node, maxx = -1;
    for (int i = 0; i < n; i++)
    {
        if ((int)g[i].size() > maxx)
        {
            maxx = g[i].size();
            node = i;
        }
    }
    g1 = vector<vector<int>>(n);
    for (int i = 0; i < n; i++)
    {
        if (i == node)
            continue;
        for (auto &j : g[i])
            if (j != node)
                g1[i].push_back(j);
    }
    swap(g, g1);
    dbg(g);
    vector<bool> vis(n, false);
    function<void(int)> dfs = [&](int u)
    {
        vis[u] = true;
        for (int v : g[u])
        {
            if (!vis[v])
                dfs(v);
        }
    };
    int ans = 0;
    for (int i = 0; i < n; ++i)
    {
        if (vis[i])
            continue;
        ans++;
        dfs(i);
    }
    cout << ans - 2 << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

int dp[1003][1003];

int fxn(string a,string b,string c,int i,int j)

{

    int n=c.length();

    

    if(i+j==n)return 0;

    //if(i==a.length()||j==b.length())return 1e9;

    if(dp[i][j]!=-1)return dp[i][j];

    int a1=1e9,b1=1e9;

    if(i<a.length()&&a[i]==c[i+j])

    {

        a1=fxn(a,b,c,i+1,j);

    }

    else if(i<a.length())

    {

         a1=1+fxn(a,b,c,i+1,j);

    }

     if(j<b.length()&&b[j]==c[i+j])

    {

        b1=fxn(a,b,c,i,j+1);

    }

    else if(j<b.length())

    {

         b1=1+fxn(a,b,c,i,j+1);

    }

    return dp[i][j]=min(a1,b1);

}

int main() {

    int t;

    cin>>t;

      

    while(t--){

         string a,b,c;

         cin>>a>>b>>c;

         memset(dp,-1,sizeof(dp));

         int z=fxn(a,b,c,0,0);

         

         cout<<z<<endl;

         

  

    }

    return 0;

}

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

using namespace std;

vector<vector<int>>dp;

int fxn(string a,string b,string c,int i,int j)

{

    int n=c.length();

    

    if(i+j==n)return 0;

    //if(i==a.length()||j==b.length())return 1e9;

    if(dp[i][j]!=-1)return dp[i][j];

    int a1=1e9,b1=1e9;

    if(i<a.length()&&a[i]==c[i+j])

    {

        a1=fxn(a,b,c,i+1,j);

    }

    else if(i<a.length())

    {

         a1=1+fxn(a,b,c,i+1,j);

    }

     if(j<b.length()&&b[j]==c[i+j])

    {

        b1=fxn(a,b,c,i,j+1);

    }

    else if(j<b.length())

    {

         b1=1+fxn(a,b,c,i,j+1);

    }

    return dp[i][j]=min(a1,b1);

}

int main() {

    int t;

    cin>>t;

      

    while(t--){

         string a,b,c;

         cin>>a>>b>>c;

         //memset(dp,-1,sizeof(dp));

         dp=vector<vector<int>>(a.length()+1,vector<int>(b.length(),-1));

         int z=fxn(a,b,c,0,0);

         

         cout<<z<<endl;

         

  

    }

    return 0;

}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>



using namespace std;



#define ll long long

#define ld long double

#define mp make_pair

#define F first 

#define S second

#define pb push_back

#define upb upper_bound

#define lwb lower_bound



const int maxn = (1 << 10);

int n, m, dp[maxn][maxn];

string a, b, c;



void clear_array() {

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

        fill(dp[i], dp[i] + 3 + m, 0);

    }

    a.clear(), b.clear(), c.clear();

    n = m = 0;

}



void solve() {

    cin >> a >> b >> c;

    n = (int)(a.size());

    m = (int)(b.size());

    a = "#" + a; b = "#" + b; c = "#" + c;

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

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

            dp[i][j] = n + n + m + m;

        }

    }

    dp[0][0] = 0;

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

        dp[i][0] = dp[i - 1][0];

        if (a[i] != c[i]) dp[i][0]++;

    }

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

        dp[0][i] = dp[0][i - 1];

        if (b[i] != c[i]) dp[0][i]++;

    }

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

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

            int from_a = (a[i] == c[i + j] ? dp[i - 1][j] : dp[i - 1][j] + 1);

            int from_b = (b[j] == c[i + j] ? dp[i][j - 1] : dp[i][j - 1] + 1);

            dp[i][j] = min(from_a, from_b);

        }

    }

    cout << dp[n][m] << "\n";

    clear_array();

}



void boost() {

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

    ios_base :: sync_with_stdio(false);

}



int main() {

    boost();

    int TT;

    cin >> TT;

    while (TT--) {

        solve();

    }

    return 0;

}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

int dp[1001][1001];

int fxn(string a,string b,string c,int i,int j)

{

    int n=c.length();

    

    if(i+j==n)return 0;

    //if(i==a.length()||j==b.length())return 1e9;

    if(dp[i][j]!=-1)return dp[i][j];

    int a1=1e9,b1=1e9;

    if(i<a.length()&&a[i]==c[i+j])

    {

        a1=fxn(a,b,c,i+1,j);

    }

    else if(i<a.length())

    {

         a1=1+fxn(a,b,c,i+1,j);

    }

     if(j<b.length()&&b[j]==c[i+j])

    {

        b1=fxn(a,b,c,i,j+1);

    }

    else if(j<b.length())

    {

         b1=1+fxn(a,b,c,i,j+1);

    }

    return dp[i][j]=min(a1,b1);

}

int main() {

    int t;

    cin>>t;

      

    while(t--){

         string a,b,c;

         cin>>a>>b>>c;

         memset(dp,-1,sizeof(dp));

         int z=fxn(a,b,c,0,0);

         

         cout<<z<<endl;

         

  

    }

    return 0;

}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

int dp[1003][1003];

int fxn(string a,string b,string c,int i,int j)

{

    int n=c.length();

    

    if(i+j==n)return 0;

    //if(i==a.length()||j==b.length())return 1e9;

    if(dp[i][j]!=-1)return dp[i][j];

    int a1=1e9,b1=1e9;

    if(i<a.length()&&a[i]==c[i+j])

    {

        a1=fxn(a,b,c,i+1,j);

    }

    else if(i<a.length())

    {

         a1=1+fxn(a,b,c,i+1,j);

    }

     if(j<b.length()&&b[j]==c[i+j])

    {

        b1=fxn(a,b,c,i,j+1);

    }

    else if(j<b.length())

    {

         b1=1+fxn(a,b,c,i,j+1);

    }

    return dp[i][j]=min(a1,b1);

}

int main() {

    int t;

    cin>>t;

      

    while(t--){

         string a,b,c;

         cin>>a>>b>>c;

         memset(dp,-1,sizeof(dp));

         int z=fxn(a,b,c,0,0);

         

         cout<<z<<endl;

         

  

    }

    return 0;

}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>



using namespace std;



#define ll long long

#define ld long double

#define mp make_pair

#define F first 

#define S second

#define pb push_back

#define upb upper_bound

#define lwb lower_bound



const int maxn = (1 << 10);

int n, m, dp[maxn][maxn];

string a, b, c;



void clear_array() {

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

        fill(dp[i], dp[i] + 3 + m, 0);

    }

    a.clear(), b.clear(), c.clear();

    n = m = 0;

}



void solve() {

    cin >> a >> b >> c;

    n = (int)(a.size());

    m = (int)(b.size());

    a = "#" + a; b = "#" + b; c = "#" + c;

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

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

            dp[i][j] = n + n + m + m;

        }

    }

    dp[0][0] = 0;

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

        dp[i][0] = dp[i - 1][0];

        if (a[i] != c[i]) dp[i][0]++;

    }

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

        dp[0][i] = dp[0][i - 1];

        if (b[i] != c[i]) dp[0][i]++;

    }

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

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

            int from_a = (a[i] == c[i + j] ? dp[i - 1][j] : dp[i - 1][j] + 1);

            int from_b = (b[j] == c[i + j] ? dp[i][j - 1] : dp[i][j - 1] + 1);

            dp[i][j] = min(from_a, from_b);

        }

    }

    cout << dp[n][m] << "\n";

    clear_array();

}



void boost() {

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

    ios_base :: sync_with_stdio(false);

}



int main() {

    boost();

    int TT;

    cin >> TT;

    while (TT--) {

        solve();

    }

    return 0;

}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

using namespace std;

int dp[1001][1001];

int fxn(string a,string b,string c,int i,int j)

{

    int n=c.length();

    

    if(i+j==n)return 0;

    //if(i==a.length()||j==b.length())return 1e9;

    if(dp[i][j]!=-1)return dp[i][j];

    int a1=1e9,b1=1e9;

    if(i<a.length()&&a[i]==c[i+j])

    {

        a1=fxn(a,b,c,i+1,j);

    }

    else if(i<a.length())

    {

         a1=1+fxn(a,b,c,i+1,j);

    }

     if(j<b.length()&&b[j]==c[i+j])

    {

        b1=fxn(a,b,c,i,j+1);

    }

    else if(j<b.length())

    {

         b1=1+fxn(a,b,c,i,j+1);

    }

    return dp[i][j]=min(a1,b1);

}

int main() {

    int t;

    cin>>t;

      

    while(t--){

         string a,b,c;

         cin>>a>>b>>c;

         memset(dp,-1,sizeof(dp));

         int z=fxn(a,b,c,0,0);

         

         cout<<z<<endl;

         

  

    }

    return 0;

}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)


Submission (verdict: Runtime error on test 11):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        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>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(k>=0){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(int i=0;i<oc.size();i++){
                if(oc[i]!=0){
                ans++;}
            }
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        int n,k;cin>>n>>k;
        vector<long long int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a.begin(),a.end());
        vector<long long int>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(k>=0){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(;i<oc.size();i++){
                if(oc[i]!=0)
                ans++;}
            
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
    }

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        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>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(i<oc.size()){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(;i<oc.size();i++){
                if(oc[i]!=0)
                ans++;}
            
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
    }
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Runtime error on test 11):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        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>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(k>=0){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(int i=0;i<oc.size();i++){
                if(oc[i]!=0){
                ans++;}
            }
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
}

Submission (verdict: Runtime error on test 11):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        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>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(k>=0){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(;i<oc.size();i++){
                if(oc[i]!=0)
                ans++;}
            
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
    }
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Runtime error on test 11):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        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>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(k>=0){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(;i<oc.size();i++){
                if(oc[i]!=0)
                ans++;}
            
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
    }

Submission (verdict: Runtime error on test 2):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while(t--){
        int n,k;cin>>n>>k;
        vector<long long int> a(n);
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        sort(a.begin(),a.end());
        vector<long long int>oc;
        int top=1;
        for(int i=0;i<n-1;i++){
            if(a[i]!=a[i+1]){
                oc.push_back(top);
                top=1;

            }else{
                top++;
            }
        }
            oc.push_back(top);
            sort(oc.begin(),oc.end());
            int i=0;
            while(k>=0){
                if(!(k-oc[i]<0)){
                    k=k-oc[i];
                    oc[i]=0;
                    i++;
                }else{
                    break;
                }
            }
            int ans=0;
            for(;i<oc.size();i++){
                if(oc[i]!=0)
                ans++;}
            
            if(!ans){
                ans++;
            }
            cout<<ans<<endl;
    }
    }
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

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

#define ll long long

using namespace std;

void fast_in_out() {
    cout.precision(18);
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll sz[200010];
signed main() {
    fast_in_out();
    ll t;
    cin >> t;
    while(t--){
        ll n,i,x,y,m,a,b,j;
        vector<pair<ll,ll>> v;
        set<pair<ll,ll>> s;
        cin >> n;
        m=n-1;
        for(i=1;i<=n;i++)sz[i]=0;
        while(m--){
            cin >> x >> y;
            sz[x]++;
            sz[y]++;
            s.insert({x,y});
            s.insert({y,x});
        }
        ll ch=1;
        for(i=1;i<=n;i++)v.push_back({sz[i],i});
        sort(v.begin(),v.end(),greater());
        for(i=0;i<v.size() && v[i].first>=v[0].first && ch==1;i++){
            for(j=i+1;j<v.size() && v[j].first>=v[1].first && ch==1;j++){
                if(s.find({v[i].second,v[j].second})!=s.end())ch=0;
            }
        }
        cout << min(n-2,v[0].first-1+v[1].first-1-ch+1) << "\n";
    }
    return 0;
}

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

#define ll long long

using namespace std;

void fast_in_out() {
    cout.precision(18);
    cin.tie(nullptr)->sync_with_stdio(false);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
ll sz[200010];
signed main() {
    fast_in_out();
    ll t;
    cin >> t;
    while(t--){
        ll n,i,x,y,m,a,b,j;
        vector<pair<ll,ll>> v;
        set<pair<ll,ll>> s;
        cin >> n;
        m=n-1;
        for(i=1;i<=n;i++)sz[i]=0;
        while(m--){
            cin >> x >> y;
            sz[x]++;
            sz[y]++;
            s.insert({x,y});
            s.insert({y,x});
        }
        ll ch=1;
        for(i=1;i<=n;i++)v.push_back({sz[i],i});
        sort(v.begin(),v.end(),greater());
        for(i=0;i<v.size() && v[i].first>=v[0].first && ch==1;i++){
            for(j=i+1;j<v.size() && v[j].first>=v[1].first && ch==1;j++){
                if(s.find({v[i].second,v[j].second})==s.end())ch=0;
            }
        }
        cout << v[0].first-1+v[1].first-1-ch+1 << "\n";
    }
    return 0;
}