#include<bits/stdc++.h>
using namespace std;
const int max_n=1e5+10;
vector<int> g[max_n],ans2[max_n];
typedef pair<int, int> P;
vector<P> ans1;
int root;
int dp[max_n][3];
bool cmp(int u,int v){
    return dp[u][1]-dp[u][2]<dp[v][1]-dp[v][2];
}
bool vis[max_n];
int dfs(int u,int fa,int num){
    if(g[u].size()==1){
        dp[u][num]=0;
        // if(!vis[u]){
        //     ans2.push_back(u);
        //     vis[u]=1;
        // }
        //cout<<u<<endl;
        return 0;
    }
    if(dp[u][num]!=-1){
        return dp[u][num];
    }
    for(auto v:g[u]){
        if(v==fa){
            continue;
        }
        dfs(v,u,1);
        dfs(v,u,2);
    }
    sort(g[u].begin(),g[u].end(),cmp);
    int sum=num;
    int res=0;
    for(auto v:g[u]){
        if(v==fa){
            continue;
        }
        if(sum&&dp[v][1]<1+dp[v][2]){
            sum--;
            res+=dp[v][1];
        }else{
            res+=dp[v][2]+1;
        }
    }
    dp[u][num]=res;
    //cout<<u<<" "<<num<<" "<<res<<endl;
    return res;
}
int now;
void solve(int u,int fa,int num,int z){
    if(g[u].size()==1){
        if(num==2){
            ans2[z].push_back(u);
        }
        ans2[z].push_back(u);
        return;
    }
    int sum=num;
    for(auto v:g[u]){
        if(v==fa){
            continue;
        }
        if(sum&&dp[v][1]<1+dp[v][2]){
            sum--;
            solve(v,u,1,z);
        }else{
            ans1.push_back(P(u,v));
            solve(v,u,2,++now);
        }
    }
    if(sum>0){
        ans2[z].push_back(u);
        if(num==2&&sum==2){
            ans2[z].push_back(u);
        }
    }
}
int main(){
    int t,n,u,v;
    cin>>t;
    while(t--){
        now=0;
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            //vis[i]=0;
            dp[i][0]=dp[i][1]=dp[i][2]=-1;
        }
        for(int i=1;i<n;++i){
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        root=-1;
        for(int i=1;i<=n;++i){
            if(g[i].size()>2){
                root=i;
                break;
            }
        }
        if(root==-1){
            cout<<0<<endl;
            for(int i=0;i<=max(n,now);++i){
                g[i].clear();
                ans2[i].clear();
            }
            ans1.clear();
            continue;
        }
        int tot=dfs(root,-1,2);
        solve(root,-1,2,0);
        printf("%d\n",now);
        for(int i=0;i<now;++i){
            //cout<<ans2[i].size()<<" "<<ans2[i+1].size()<<endl;
            printf("%d %d %d %d\n",ans1[i].first,ans1[i].second,ans2[i][1],ans2[i+1][0]);
        }
        for(int i=0;i<=max(n,now);++i){
            g[i].clear();
            ans2[i].clear();
        }
        ans1.clear();
    }
    return 0;
}