ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)


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


#define int long long

signed main() {
    int t;
    cin >> t;
    while (t--) {
        int n,m;
        cin >> n >> m ;
        
    vector<int>a(n);
    vector<int>b(m);

    map<int,int>mp;
    multiset<int>st;
    for(int i=0;i<n;i++){
        cin >> a[i];
        mp[a[i]]++;
        st.insert(a[i]);
    }
    for(int i=0;i<m;i++){
        cin >> b[i];
    }
    queue<int>q;
    
    for(int i=0;i<m;i++){
        q.push(b[i]);
    }

    bool ok = true;
       
    while(!q.empty()){
        int x = q.front();
        q.pop();
        
        if(q.size()>mp.size()){
            ok = false;
            break;
        }
        if(mp[x]>0){
            mp[x]--;
        }
        else{
            
            if(x>1){
                q.push(x/2);
                q.push((x+1)/2);
            }
            
        }
    }

    for(auto i:mp){
        if(i.second>0){
            ok = false;
            break;
        }
    }
    
    

    if(ok){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
            
    }
       
    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 3):
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastIO\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);\
    cout.precision(numeric_limits<double>::max_digits10);
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define pb push_back
#define all(x) (x).begin(), (x).end()
const int P = 1e9+7;

int32_t main(){
    fastIO;
    int t;
    cin >> t;

    while(t--){
        int n, m;
        cin >> n >> m;

        priority_queue<int, vi, greater<int>> pqa, pqb;
        for(int i = 0; i < n; i++){
            int temp;
            cin >> temp;

            pqa.push(temp);
        }
        for(int i = 0; i < m; i++){
            int temp;
            cin >> temp;

            pqb.push(temp);
        }

        int flag = 1; // default: true

        while(!pqb.empty() && !pqa.empty()){

            if(pqa.top() == pqb.top()){
                pqa.pop();
                pqb.pop();
            }
            else if(pqa.top() < pqb.top()){
                //cout << "here1 " << pqa.top() << " " << pqb.top() << "\n";

                int a1 = pqa.top();
                pqa.pop();

                //cout << "here3 " << (a1+1) << " " << pqa.top() << "\n";

                if(pqa.empty() || (pqa.top()-a1) > 1){
                    flag = 0;
                    break;
                }
                else{
                    int a2 = pqa.top();
                    pqa.pop();

                    pqa.push(a1+a2);
                }

                //cout << "here2 " << pqa.top() << " " << pqb.top() << "\n";
            }
            else{ // (pqa.top() > pqb.top())
                flag = 1;
                break;
            }
        }

        if(flag == 1 && pqa.empty() && pqb.empty()){
            cout << "Yes\n";
        }
        else{
            cout << "No\n";
        }

    }
    return 0;
}

// Think from the result
// Think wrt range (only 26 alphabets/ operations, range of int)
// Think it as separate
// Think if it can be done linearly/ bitwise
// Observe the very basic thing that it follows
// Think of limits of answer
// Reduce the number of variables used (make 2 variables as +&- and convert to 1)
// Think to iterate from both left to right & right to left
// Think of result required before calculation
// Change the initial assumption which you set
// Dont use predefined function (set or map or sort) if the no. of elements is large
// Think it as very simple
// Change the order of operation in multiple for-loops
// Think if it can be done in dp
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define vector_output for(int i=0;i<vec.size();i++) cout<<vec[i]<<" ";
#define vector_input for(int i=0;i<n;i++) cin>>vec[i];
#define all(x)      x.begin(), x.end()
#define rall(x)     x.rbegin(), x.rend()
#define mp(x, y)    make_pair(x, y)
#define pb          push_back
#define ppb         pop_back

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl   cout<<'\n';
#define MOD1 1000000007
#define MOD2 998244353
#define inf  1e9
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;

void urod() {
    string a,b,c;
    cin>>a>>b>>c;
    int n=(int)a.size(), m=(int)b.size();
    int cnt = 0;
    int dp[n+1][ m+1];
    fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
    dp[0][0]=0;
    for(int i=0;i<n;i++){
    	dp[i+1][0]=dp[i][0]+(a[i]!=c[i]);
	}
	for(int j=0;j<m;j++){
		dp[0][j+1]=dp[0][j]+(b[j]!=c[j]);
	}
	for(int i=0;i<=n;i++){
		for(int j=0;j<=m;j++){
			dp[i][j]=min(dp[i-1][j]+c[i+j-1],dp[i][j-1]+c[i+j-1]);
		}
	}
	cout<<dp[n][m];nl
}

int32_t main() {
    tada;
    int t=1;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}


Submission (verdict: Wrong answer on test 1):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define vector_output for(int i=0;i<vec.size();i++) cout<<vec[i]<<" ";
#define vector_input for(int i=0;i<n;i++) cin>>vec[i];
#define all(x)      x.begin(), x.end()
#define rall(x)     x.rbegin(), x.rend()
#define mp(x, y)    make_pair(x, y)
#define pb          push_back
#define ppb         pop_back

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl   cout<<'\n';
#define MOD1 1000000007
#define MOD2 998244353
#define inf  1e9
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;

void urod() {
    string a,b,c;
    cin>>a>>b>>c;
    int n=(int)a.size(), m=(int)b.size();
    int cnt = 0;
    int dp[n+1][ m+1];
    fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
        dp[i][0] = dp[i-1][0] + (a[i-1] != c[i-1]);
    }
    for (int j = 1; j <= m; ++j) {
        dp[0][j] = dp[0][j-1] + (b[j-1] != c[j-1]);
    }
	for(int i=0;i<=n;i++){
		for(int j=0;j<=m;j++){
			dp[i][j]=min(dp[i-1][j]+(a[i-1] != c[i+j-1]),dp[i][j-1]+(b[j-1]!=c[i+j-1]));
		}
	}
	cout<<dp[n][m];nl
}

int32_t main() {
    tada;
    int t=1;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define vector_output for(int i=0;i<vec.size();i++) cout<<vec[i]<<" ";
#define vector_input for(int i=0;i<n;i++) cin>>vec[i];
#define all(x)      x.begin(), x.end()
#define rall(x)     x.rbegin(), x.rend()
#define mp(x, y)    make_pair(x, y)
#define pb          push_back
#define ppb         pop_back

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl   cout<<'\n';
#define MOD1 1000000007
#define MOD2 998244353
#define inf  1e9
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;

void urod() {
    string a,b,c;
    cin>>a>>b>>c;
    int n=(int)a.size(), m=(int)b.size();
    int cnt = 0;
    int dp[n+1][ m+1];
    fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
        dp[i][0] = dp[i-1][0] + (a[i-1] != c[i-1]);
    }
    for (int j = 1; j <= m; ++j) {
        dp[0][j] = dp[0][j-1] + (b[j-1] != c[j-1]);
    }
	for(int i=0;i<=n;i++){
		for(int j=0;j<=m;j++){
			dp[i][j]=min(dp[i-1][j]+(a[i-1] != c[i+j-1]),dp[i][j-1]+(b[j-1]!=c[i+j-1]));
		}
	}
	cout<<dp[n][m];nl
}

int32_t main() {
    tada;
    int t=1;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}


Submission (verdict: Accepted):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl cout << '\n';
#define inf 1e9

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;

void urod() {
    string a, b, c;
    cin >> a >> b >> c;
    int n = (int)a.size(), m = (int)b.size();

    vector<vector<int>> dp(n + 1, vector<int>(m + 1, inf));
    dp[0][0] = 0;

    for (int i = 1; i <= n; ++i) {
        dp[i][0] = dp[i - 1][0] + (a[i - 1] != c[i - 1]);
    }
    for (int j = 1; j <= m; ++j) {
        dp[0][j] = dp[0][j - 1] + (b[j - 1] != c[j - 1]);
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            dp[i][j] = min(
                dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                dp[i][j - 1] + (b[j - 1] != c[i + j - 1])
            );
        }
    }
    cout << dp[n][m];
    nl;
}

int32_t main() {
    tada;
    int t;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define vector_output for(int i=0;i<vec.size();i++) cout<<vec[i]<<" ";
#define vector_input for(int i=0;i<n;i++) cin>>vec[i];
#define all(x)      x.begin(), x.end()
#define rall(x)     x.rbegin(), x.rend()
#define mp(x, y)    make_pair(x, y)
#define pb          push_back
#define ppb         pop_back

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl   cout<<'\n';
#define MOD1 1000000007
#define MOD2 998244353
#define INF numeric_limits<ll>::max()
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;

void urod() {
    string a,b,c;
    cin>>a>>b>>c;
    int cnt = 0;
    int i=0, j=0;
    
    for(char ch : c){
    	if(i<a.size() && ch==a[i]){
    		i++;
		}
		else if(j<b.size() && ch==b[j]){
			j++;
		}
		else{
			cnt++;
		}
	}
	cout<<cnt;nl
}

int32_t main() {
    tada;
    int t=1;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}


Submission (verdict: Wrong answer on test 1):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define vector_output for(int i=0;i<vec.size();i++) cout<<vec[i]<<" ";
#define vector_input for(int i=0;i<n;i++) cin>>vec[i];
#define all(x)      x.begin(), x.end()
#define rall(x)     x.rbegin(), x.rend()
#define mp(x, y)    make_pair(x, y)
#define pb          push_back
#define ppb         pop_back

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl   cout<<'\n';
#define MOD1 1000000007
#define MOD2 998244353
#define inf  1e9
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;

void urod() {
    string a,b,c;
    cin>>a>>b>>c;
    int n=(int)a.size(), m=(int)b.size();
    int cnt = 0;
    int dp[n+1][ m+1];
    fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
    dp[0][0]=0;
    for(int i=0;i<n;i++){
    	dp[i+1][0]=dp[i][0]+(a[i]!=c[i]);
	}
	for(int j=0;j<m;j++){
		dp[0][j+1]=dp[0][j]+(b[j]!=c[j]);
	}
	for(int i=0;i<=n;i++){
		for(int j=0;j<=m;j++){
			dp[i][j]=min(dp[i-1][j]+c[i+j-1],dp[i][j-1]+c[i+j-1]);
		}
	}
	cout<<dp[n][m];nl
}

int32_t main() {
    tada;
    int t=1;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
#pragma gcc target("sse,sse2,sse3,sse4,ssse3,popcnt,abm,mmx,avx,tune=native")
#pragma gcc optimize("ofast,unroll-loops,fast-math,o3")

#include <bits/stdc++.h>
using namespace std;

#define vector_output for(int i=0;i<vec.size();i++) cout<<vec[i]<<" ";
#define vector_input for(int i=0;i<n;i++) cin>>vec[i];
#define all(x)      x.begin(), x.end()
#define rall(x)     x.rbegin(), x.rend()
#define mp(x, y)    make_pair(x, y)
#define pb          push_back
#define ppb         pop_back

#define tada ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define nl   cout<<'\n';
#define MOD1 1000000007
#define MOD2 998244353
#define INF numeric_limits<ll>::max()
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double lld;
typedef vector <ll> vl;
typedef pair <ll, ll> pll;

void urod() {
    string a,b,c;
    cin>>a>>b>>c;
    int cnt = 0;
    int i=0, j=0;
    
    for(char ch : c){
    	if(i<a.size() && ch==a[i]){
    		i++;
		}
		else if(j<b.size() && ch==b[j]){
			j++;
		}
		else{
			cnt++;
		}
	}
	cout<<cnt;nl
}

int32_t main() {
    tada;
    int t=1;
    cin >> t;
    while (t--) {
        urod();
    }
    return 0;
}
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;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

#define pb push_back                    
#define ll long long
//#define sort(all(v)) sort(v.begin(),v.end())

int mod = 998244353;
const int N = 4e5 + 10;
const int inf = 1e9;
int fact[200001];
ll binpow(ll a, ll b){
 if(b == 0) return 1;
 else if(b % 2 == 1) return (a * binpow(a, b - 1)) % mod;
 ll p = binpow(a,b / 2);
 return (p * p) % mod;
}
vector <int> g[N];
int sum[N],was[N];
void dfs(int u){
    was[u] = 1;
    if(sum[u] == 0 && u != 1) sum[u]++;
    for(int to : g[u]){
        if(was[to] != 0) continue;
        sum[u]++;
        dfs(to);
    }
}
signed main(){
    //freopen("mootube.in", "r", stdin);
    //freopen("mootube.out", "w", stdout);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i < n; i++){
            int u,v;
            cin >> u >> v;
            g[u].pb(v);
            g[v].pb(u);
        }
        dfs(1);
        int mx = 0;
        multiset<int> mt;
        for(int i = 1; i <= n; i++){
            mx = max(mx,sum[i]);  
            mt.insert(sum[i]);
        }
        int mx1 = 0,v = 0,ans = 0;
        for(int i = 1; i <= n; i++){
            if(sum[i] == mx){
                for(int to : g[i]){
                    mt.erase(mt.find(sum[to]));
                    mt.insert(sum[to] - 1);
                }
                if(mx1 < *mt.rbegin()){
                    mx1 = *mt.rbegin();
                    v = i;
                }
                for(int to : g[i]){
                    mt.erase(mt.find(sum[to] - 1));
                    mt.insert(sum[to]);
                }
            }
        }
        ans += mx;
        for(int to : g[v]){
            sum[to]--;
        }
        mx = 0;
        for(int i = 1; i <= n; i++){
            if(v == i) continue;
            mx = max(mx,sum[i]);   
        }
        cout << ans + mx - 1 << '\n';
        for(int i = 1; i <= n; i++){
            sum[i] = was[i] = 0;
            g[i].clear();
        }
    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)


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


#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define inf 1e9
#define nline "\n"
#define pb push_back
#define ppb pop_back
// #define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i,a,b)    for(int i{a};i<b;i++);


typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}


//01111
//10000
//



void solve() {
    ll l = 0 , r = 0;
    cin >> l >> r;
    ll y = 1;
    int bit = 0;
    while (y <= r) {
        y = y * 2;
        bit++;
    }
    bit--;
    ll val = (1LL << bit);
    if (val > l && val <= r) {
        cout << val << " " << val - 1 << " ";
        if (l == val - 1) {
            cout << r << nline;
        }
        else cout << l << nline;
    }
    else {
        cout << l << " " << l + 1 << " " << r << nline;
        return;
    }
}


int main() {
#ifndef ONLINE_JUDGE
    freopen("error.txt", "w", stderr);
    freopen("output.txt", "w", stdout);
    freopen("input.txt", "r", stdin);
#endif
    fastio();
    int t{1};
    cin >> t;
    while (t--) solve();
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <complex>
#include <string> 
#include <cstring>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <tuple>
#include <random>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int modn = 1000000007;
const int maxn = 2e6;
const long double eps = 1e-8;

struct node {
	int x, y, z;
};

int _;
int n, m;
vector<int>a;
ll deg[maxn];
vector<int>g[maxn];

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> _;
	while (_--) {
		cin >> n;
		for (int i = 1;i <= n;++i) {
			deg[i] = 0;
		}
		for (int i = 1, u, v;i < n;++i) {
			cin >> u >> v;
			g[u].push_back(v);
			g[v].push_back(u);
			deg[u] += 1;
			deg[v] += 1;
		}
		ll ans = 1;
		ll ma = 0, mp = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
				mp = i;
			}
		}
		if (ma == 1) {
			cout << 0 << '\n';
			continue;
		}
		ans += ma - 1;
		for (auto x : g[mp]) {
			deg[x] -= 1;
		}
		deg[mp] = 0;
		ma = 0, mp = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
				mp = i;
			}
		}
		ans += ma - 1;
		cout << ans << '\n';
	}
	return 0;
}

Submission (verdict: Wrong answer on test 2):
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <complex>
#include <string> 
#include <cstring>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <utility>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <tuple>
#include <random>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int modn = 1000000007;
const int maxn = 2e6;
const long double eps = 1e-8;

struct node {
	int x, y, z;
};

int _;
int n, m;
vector<int>a;
ll deg[maxn];
vector<int>g[maxn];

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> _;
	while (_--) {
		cin >> n;
		for (int i = 1;i <= n;++i) {
			deg[i] = 0;
			g[i].clear();
		}
		for (int i = 1, u, v;i < n;++i) {
			cin >> u >> v;
			g[u].push_back(v);
			g[v].push_back(u);
			deg[u] += 1;
			deg[v] += 1;
		}
		ll ans = 1;
		ll ma = 0, mp = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
				mp = i;
			}
		}
		if (ma == 1) {
			cout << 0 << '\n';
			continue;
		}
		ans += ma - 1;
		for (auto x : g[mp]) {
			deg[x] -= 1;
		}
		deg[mp] = 0;
		ma = 0, mp = 0;
		for (int i = 1;i <= n;++i) {
			if (deg[i] > ma) {
				ma = deg[i];
				mp = i;
			}
		}
		ans += ma - 1;
		cout << ans << '\n';
	}
	return 0;
}