ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;
 
bool Order(const string& s) {
    int n = s.size();
    for (int i = 1; i < n; ++i) {
        if (s[i] > s[i - 1]) {  
            return false;
        }
    }
    return true; 
}
 
int main() {
    int t; 
    cin >> t;
    while(t--) {
        string s; 
        cin >> s;
        int n = s.size();
        
        // while(!Order(s)){
        //     int c = 0;
        //     for(int i = n - 1; i >= 1; i--) {
        //         if(s[i] > s[i - 1] + 1) {
        //             swap(s[i], s[i - 1]);
                    
        //             s[i - 1] -= 1;
        //             c = 1;
        //         }
        //     }
        //     if(!c) {
        //         break;
        //     }
        // }
        
        for(int j = 0; j < 9; j++){
            int c = 0;
            for(int i = n - 1; i >= 1; i--) {
                if(s[i] > s[i - 1] + 1) {
                    swap(s[i], s[i - 1]);
                    
                    s[i - 1] -= 1;
                    c = 1;
                }
            }
            if(!c) {
                break;
            }
        }
        
        cout << s << endl;
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
 
bool Order(const string& s) {
    int n = s.size();
    for (int i = 1; i < n; ++i) {
        if (s[i] > s[i - 1]) {  
            return false;
        }
    }
    return true; 
}
 
int main() {
    int t; 
    cin >> t;
    while(t--) {
        string s; 
        cin >> s;
        int n = s.size();
        
        for(int i = 0; i < n-1; i++){
            for(int j = 0; j < 9; j++){
                if(i+j+1 < n && s[i+j+1] > s[i+j] + 1){
                    
                    swap(s[i+j], s[i+j+1]);
                    
                    s[i+j] -= 1;
                }
            }
        }
        
        cout << s << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;
 
bool Order(const string& s) {
    int n = s.size();
    for (int i = 1; i < n; ++i) {
        if (s[i] > s[i - 1]) {  
            return false;
        }
    }
    return true; 
}
 
int main() {
    int t; 
    cin >> t;
    while(t--) {
        string s; 
        cin >> s;
        int n = s.size();
        
        for(int j = 0; j < 9; j++){
            int c = 0;
            for(int i = 0; i < n-1; i++){
                if(s[i+1] > s[i] + 1){
                    
                    swap(s[i], s[i+1]);
                    
                    s[i] -= 1;
                    
                    c = 1;
                }
            }
            if(!c) break;
        }
        
        cout << s << endl;
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
using namespace std;
 
bool Order(const string& s) {
    int n = s.size();
    for (int i = 1; i < n; ++i) {
        if (s[i] > s[i - 1]) {  
            return false;
        }
    }
    return true; 
}
 
int main() {
    int t; 
    cin >> t;
    while(t--) {
        string s; 
        cin >> s;
        int n = s.size();
        
        // while(!Order(s)){
        //     int c = 0;
        //     for(int i = n - 1; i >= 1; i--) {
        //         if(s[i] > s[i - 1] + 1) {
        //             swap(s[i], s[i - 1]);
                    
        //             s[i - 1] -= 1;
        //             c = 1;
        //         }
        //     }
        //     if(!c) {
        //         break;
        //     }
        // }
        
        for(int j = 0; j < 10; j++){
            int c = 0;
            for(int i = n - 1; i >= 1; i--) {
                if(s[i] > s[i - 1] + 1) {
                    swap(s[i], s[i - 1]);
                    
                    s[i - 1] -= 1;
                    c = 1;
                }
            }
            if(!c) {
                break;
            }
        }
        
        cout << s << endl;
    }
    return 0;
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
 
bool Order(const string& s) {
    int n = s.size();
    for (int i = 1; i < n; ++i) {
        if (s[i] > s[i - 1]) {  
            return false;
        }
    }
    return true; 
}
 
int main() {
    int t; 
    cin >> t;
    while(t--) {
        string s; 
        cin >> s;
        int n = s.size();
        
        for(int i = 0; i < n-1; i++){
            for(int j = 0; j < 9; j++){
                if(i+j+1 < n && s[i+j+1] > s[i+j] + 1){
                    
                    swap(s[i+j], s[i+j+1]);
                    
                    s[i+j] -= 1;
                }
            }
        }
        
        cout << s << endl;
    }
    return 0;
}
Problem: 2050C - Uninteresting Number
Language: PyPy 3-64

Previous submission (verdict: Runtime error on test 4):
def change(n,count_2,count_3):
    for i in range(0,min(count_2,8)+1):
        for j in range(0,min(3,count_3)+1):
            if ((n+2*i+6*j)%9 == 0):
                return "YES"
    return "NO"
t = int(input())
for _ in range(t):
    n = int(input())
    count_2 = 0
    count_3 = 0
    for char in str(n):
        if char == "2":
            count_2+=1
        if char == "3":
            count_3+=1
    if(n%9 == 0):
        print("YES")
    else:
        print(change(n,count_2,count_3)) 






Submission (verdict: Accepted):
def change(n,count_2,count_3):
    for i in range(0,min(count_2,8)+1):
        for j in range(0,min(3,count_3)+1):
            if ((n+2*i+6*j)%9 == 0):
                return "YES"
    return "NO"
t = int(input())
for _ in range(t):
    n = input()
    count_2 = 0
    count_3 = 0
    for char in n:
        if char == "2":
            count_2+=1
        if char == "3":
            count_3+=1
    Sum = 0
    for char in n:
        Sum+=int(char)
    if(Sum%9 == 0):
        print("YES")
    else:
        print(change(Sum,count_2,count_3))
Problem: 2050C - Uninteresting Number
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 1):
def change(n,count_2,count_3):
    for i in range(1,min(count_2,8)+1):
        for j in range(0,min(3,count_3)+1):
            if (n+2*i+3*j %9 == 0):
                return "YES"
    return "NO"
t = int(input())
for _ in range(t):
    n = int(input())
    s = ""
    count_2 = 0
    count_3 = 0
    for char in str(n):
        if char == "2":
            count_2+=1
        if char == "3":
            count_3+=1
    if(n%9 == 0):
        print("YES")
    else:
        print(change(n,count_2,count_3)) 

    





Submission (verdict: Runtime error on test 4):
def change(n,count_2,count_3):
    for i in range(0,min(count_2,8)+1):
        for j in range(0,min(3,count_3)+1):
            if ((n+2*i+6*j)%9 == 0):
                return "YES"
    return "NO"
t = int(input())
for _ in range(t):
    n = int(input())
    count_2 = 0
    count_3 = 0
    for char in str(n):
        if char == "2":
            count_2+=1
        if char == "3":
            count_3+=1
    if(n%9 == 0):
        print("YES")
    else:
        print(change(n,count_2,count_3))
Problem: 2050C - Uninteresting Number
Language: PyPy 3-64


Submission (verdict: Wrong answer on test 1):
def change(n,count_2,count_3):
    for i in range(1,min(count_2,8)+1):
        for j in range(0,min(3,count_3)+1):
            if (n+2*i+3*j %9 == 0):
                return "YES"
    return "NO"
t = int(input())
for _ in range(t):
    n = int(input())
    s = ""
    count_2 = 0
    count_3 = 0
    for char in str(n):
        if char == "2":
            count_2+=1
        if char == "3":
            count_3+=1
    if(n%9 == 0):
        print("YES")
    else:
        print(change(n,count_2,count_3))
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        if(n==k){
            cout<< k-1 << endl;
        }
        else{
        map<int,int> mp;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        vector<int> f;
        for(auto& pair: mp){
            f.push_back(pair.second);
        }
        sort(f.begin(),f.end());
        int c=f.size();
        for(int f1: f){
            if(k>=f1){
                k-=f1;
                c--;
            }
            else{
                break;
            }
        }
        cout << c << endl;}

}
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        if(n==k){
            cout<< 1 << endl;
        }
        else{
        map<int,int> mp;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        vector<int> f;
        for(auto& pair: mp){
            f.push_back(pair.second);
        }
        sort(f.begin(),f.end());
        int c=f.size();
        for(int f1: f){
            if(k>=f1){
                k-=f1;
                c--;
            }
            else{
                break;
            }
        }
        cout << c << endl;}

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


Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        int c=0;
        map<int,int> mp;
        for(int i=0;i<n;i++){
            if(mp[a[i]]==0){
                c++;
            }
            mp[a[i]]++;
        }
        cout<< c-k;
    }

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

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        int c=0;
        map<int,int> mp;
        for(int i=0;i<n;i++){
            if(mp[a[i]]==0){
                c++;
            }
            mp[a[i]]++;
        }
        cout<< c-k;
    }

}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        int c=0;
        map<int,int> mp;
        for(int i=0;i<n;i++){
            if(mp[a[i]]==0){
                c++;
            }
            mp[a[i]]++;
        }
        cout<< c-k<<endl;
    }

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

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        map<int,int> mp;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        vector<int> f;
        for(auto& pair: mp){
            f.push_back(pair.second);
        }
        sort(f.begin(),f.end());
        int c=f.size();
        for(int f1: f){
            if(k>=f1){
                k-=f1;
                c--;
            }
            else{
                break;
            }
        }
        cout << c << endl;

}
}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        if(n==k){
            cout<< k-1 << endl;
        }
        else{
        map<int,int> mp;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        vector<int> f;
        for(auto& pair: mp){
            f.push_back(pair.second);
        }
        sort(f.begin(),f.end());
        int c=f.size();
        for(int f1: f){
            if(k>=f1){
                k-=f1;
                c--;
            }
            else{
                break;
            }
        }
        cout << c << endl;}

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

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        int c=0;
        map<int,int> mp;
        for(int i=0;i<n;i++){
            if(mp[a[i]]==0){
                c++;
            }
            mp[a[i]]++;
        }
        cout<< c-k<<endl;
    }

}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin >> t;
    for(int i=0;i<t;i++){
        int n,k;
        cin >> n >> k;
        int a[n];
        for(int i=0;i<n;i++){
            cin >> a[i];
        }
        map<int,int> mp;
        for(int i=0;i<n;i++){
            mp[a[i]]++;
        }
        vector<int> f;
        for(auto& pair: mp){
            f.push_back(pair.second);
        }
        sort(f.begin(),f.end());
        int c=f.size();
        for(int f1: f){
            if(k>=f1){
                k-=f1;
                c--;
            }
            else{
                break;
            }
        }
        cout << c << endl;

}
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)


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

#define ll long long 
#define ull unsigned long long
#define lld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
 
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);
#define vi vector<int>
#define iinf (int)2e9
#define eb emplace_back
#define vb vector<bool>
#define vll vector<ll> 
#define vvll vector<vll>
#define vpll vector<pll>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define nline cout<<"\n";
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define fs first
#define sc second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, x, y) for(int i=x; i<y; i++)
#define repp(i, x, y) for(int i=x; i<=y; i++)
 
#define print(k) {for(auto v : k){cout << v << " ";} cout << endl;}
 
#define fi first
#define se second
#define mp make_pair
 
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;

/* =================================================== */

const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll llinf = (ll)1e18;


void solve() {
    int n ;cin>>n;
    vi final(n); 
    rep(i, 0, n) cin>>final[i];
    
    set<pii> s;
    vpii ans;

    vi initial(n); 
    rep(i, 0, n) initial[i] = i+1;

    rep(i, 0, n) {
        if(initial[i] == final[i]) continue;
        rep(j, i, n){
            if(initial[j] == final[i]) {
                swap(initial[i], initial[j]); 
                s.insert(mp(initial[i], initial[j])); 
                s.insert(mp(initial[j], initial[i]));
                ans.pb(mp(initial[j], initial[i]));
                break;
            }
        }
    }

    repp(i, 1, n) {
        repp(j, 1, n){
            if(i == j) continue;

            if(s.find(mp(i, j)) != s.end() || s.find(mp(j, i)) != s.end()){
                continue;
            }
            else {
                ans.pb(mp(i, j));
                ans.pb(mp(j, i));
                s.insert(mp(i, j));
                s.insert(mp(j, i));
            }

        }
    }

    cout<<ans.size()<<endl;
    for(auto it:ans){
        cout<<it.fi<<" "<<it.se<<endl;
    }

}

int main() {
    fastio()
    // int t;cin>>t;
    int t=1;
    while(t--) solve();

    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Memory limit exceeded on test 1):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;
const int mod= 1e9+7;
ll n,ans,o,p,q,m=2e5+10,now,op,x,y,top,a[N];
string s;
vector<ll> g[N];
struct node
{
	ll x,y;
	bool operator<(const node&c)const
	{
		return x>c.x;
	}
}d[N],t[N];
int main()
{
   	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(int i=1;i<=m;i++) 
    {
    	d[i].y=i;
    	t[i].y=i;
    }
    cin>>q;
    while(q--)
    {
    	cin>>n;
    	for(int i=1;i<=n;i++) d[i].x=t[i].x=0;
    	ans=op=0;
    	for(int i=1;i<n;i++)
    	{
    		cin>>x>>y;
    		g[x].push_back(y);
    		g[y].push_back(x);
    		d[x].x++;
    		d[y].x++;
    		t[x].x++;
    		t[y].x++;
    	}
    	if(n<=2)
    	{
    		cout<<0<<'\n';
    		continue;
    	}
    	sort(d+1,d+1+n);
    	ans+=d[1].x;
    	top=d[1].y;
        for(auto h:g[d[1].y]) 
        {
        	t[h].x--;
        }
        t[d[1].y].x=0;
       // for(int i=1;i<=n;i++) cout<<t[i].x<<'\n';
        sort(t+1,t+1+n);
        ans+=t[1].x;
        //for(auto h:g[d[1].y]) if(h==top) op=1;
       // if(op) ans-=2;
       // else ans-=1;
        ans-=1;
        cout<<ans<<'\n';
    }
	return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;
const int mod= 1e9+7;
ll n,ans,o,p,q,m=2e5+10,now,op,x,y,top,a[N];
string s;
vector<ll> g[N];
struct node
{
	ll x,y;
	bool operator<(const node&c)const
	{
		return x>c.x;
	}
}d[N],t[N];
int main()
{
   	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>q;
    while(q--)
    {
    	cin>>n;
    	for(int i=1;i<=n;i++) 
    	{
    		d[i].x=t[i].x=0;
    		d[i].y=t[i].y=i;
    	}
    	ans=op=0;
    	for(int i=1;i<n;i++)
    	{
    		cin>>x>>y;
    		g[x].push_back(y);
    		g[y].push_back(x);
    		d[x].x++;
    		d[y].x++;
    		t[x].x++;
    		t[y].x++;
    	}
    	if(n<=2)
    	{
    		cout<<0<<'\n';
    		continue;
    	}
    	sort(d+1,d+1+n);
    	ans+=d[1].x;
    	top=d[1].y;
        for(auto h:g[d[1].y]) 
        {
        	t[h].x--;
        }
        t[d[1].y].x=0;
       // for(int i=1;i<=n;i++) cout<<t[i].x<<'\n';
        sort(t+1,t+1+n);
        ans+=t[1].x;
        //for(auto h:g[d[1].y]) if(h==top) op=1;
       // if(op) ans-=2;
       // else ans-=1;
        ans-=1;
        cout<<ans<<'\n';
    }
	return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Runtime error on test 1):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;
const int mod= 1e9+7;
ll n,ans,o,p,q,m,now,op,x,y,top,a[N];
string s;
vector<ll> g[N];
struct node
{
	ll x,y;
	bool operator<(const node&c)const
	{
		return x>c.x;
	}
}d[N],t[N];
int main()
{
   	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>q;
    for(int i=1;i<=N;i++) d[i].y=t[i].y=i;
    while(q--)
    {
    	cin>>n;
    	for(int i=1;i<=n;i++) d[i].x=t[i].x=0;
    	ans=op=0;
    	for(int i=1;i<n;i++)
    	{
    		cin>>x>>y;
    		g[x].push_back(y);
    		g[y].push_back(x);
    		d[x].x++;
    		d[y].x++;
    		t[x].x++;
    		t[y].x++;
    	}
    	if(n<=2)
    	{
    		cout<<0<<'\n';
    		continue;
    	}
    	sort(d+1,d+1+n);
    	ans+=d[1].x;
    	top=d[1].y;
        for(auto h:g[d[1].y]) 
        {
        	t[h].x--;
        }
        t[d[1].y].x=0;
       // for(int i=1;i<=n;i++) cout<<t[i].x<<'\n';
        sort(t+1,t+1+n);
        ans+=t[1].x;
        //for(auto h:g[d[1].y]) if(h==top) op=1;
       // if(op) ans-=2;
       // else ans-=1;
        ans-=1;
        cout<<ans<<'\n';
    }
	return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Runtime error on test 1):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;
const int mod= 1e9+7;
ll n,ans,o,p,q,m,now,op,x,y,top,a[N];
string s;
vector<ll> g[N];
struct node
{
	ll x,y;
	bool operator<(const node&c)const
	{
		return x>c.x;
	}
}d[N],t[N];
int main()
{
   	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>q;
    for(int i=1;i<=N;i++) d[i].y=t[i].y=i;
    while(q--)
    {
    	cin>>n;
    	for(int i=1;i<=n;i++) d[i].x=t[i].x=0;
    	ans=op=0;
    	for(int i=1;i<n;i++)
    	{
    		cin>>x>>y;
    		g[x].push_back(y);
    		g[y].push_back(x);
    		d[x].x++;
    		d[y].x++;
    		t[x].x++;
    		t[y].x++;
    	}
    	if(n<=2)
    	{
    		cout<<0<<'\n';
    		continue;
    	}
    	sort(d+1,d+1+n);
    	ans+=d[1].x;
    	top=d[1].y;
        for(auto h:g[d[1].y]) 
        {
        	t[h].x--;
        }
        t[d[1].y].x=0;
       // for(int i=1;i<=n;i++) cout<<t[i].x<<'\n';
        sort(t+1,t+1+n);
        ans+=t[1].x;
        //for(auto h:g[d[1].y]) if(h==top) op=1;
       // if(op) ans-=2;
       // else ans-=1;
        ans-=1;
        cout<<ans<<'\n';
    }
	return 0;
}

Submission (verdict: Memory limit exceeded on test 1):
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+10;
const int mod= 1e9+7;
ll n,ans,o,p,q,m=2e5+10,now,op,x,y,top,a[N];
string s;
vector<ll> g[N];
struct node
{
	ll x,y;
	bool operator<(const node&c)const
	{
		return x>c.x;
	}
}d[N],t[N];
int main()
{
   	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for(int i=1;i<=m;i++) 
    {
    	d[i].y=i;
    	t[i].y=i;
    }
    cin>>q;
    while(q--)
    {
    	cin>>n;
    	for(int i=1;i<=n;i++) d[i].x=t[i].x=0;
    	ans=op=0;
    	for(int i=1;i<n;i++)
    	{
    		cin>>x>>y;
    		g[x].push_back(y);
    		g[y].push_back(x);
    		d[x].x++;
    		d[y].x++;
    		t[x].x++;
    		t[y].x++;
    	}
    	if(n<=2)
    	{
    		cout<<0<<'\n';
    		continue;
    	}
    	sort(d+1,d+1+n);
    	ans+=d[1].x;
    	top=d[1].y;
        for(auto h:g[d[1].y]) 
        {
        	t[h].x--;
        }
        t[d[1].y].x=0;
       // for(int i=1;i<=n;i++) cout<<t[i].x<<'\n';
        sort(t+1,t+1+n);
        ans+=t[1].x;
        //for(auto h:g[d[1].y]) if(h==top) op=1;
       // if(op) ans-=2;
       // else ans-=1;
        ans-=1;
        cout<<ans<<'\n';
    }
	return 0;
}