//
// Created by zhujunchao on 2021/5/7.
//

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 2e5 + 7;
const int mod = 1e9 + 7;
int head, nh;
int d[N], hd[N], tl[N];
bool vis[N];
vector<pair<int, int> > ans1, ans2;
vector<vector<int> > e;
void dfs(int x, int fa, int r){
    for(int i = 0; i < e[x].size(); i++){
        if(e[x][i] == fa) continue;
        int v = e[x][i];
        dfs(v, x, 1);
        if(hd[v] == v){
            if(tl[x] == x){
                tl[x] = tl[v];
                vis[v] = 1;
            }
            else if(hd[x] == x){
                hd[x] = tl[v];
                vis[v] = 1;
            }
        }
    }
    for(int i = 0; i < e[x].size(); i++){
        if(e[x][i] == fa) continue;
        int v = e[x][i];
        if(vis[v]) continue;
        vis[v] = 1;
        ans1.push_back({x, v});
        ans2.push_back({tl[x], hd[v]});
        tl[x] = tl[v];
    }
}
void solve(){
    int n, u, v;
    scanf("%d", &n);
    e.resize(n + 1);
    for(int i = 1; i <= n; i++){
        e[i].clear();
        d[i] = 0;
        hd[i] = tl[i] = i;
        vis[i] = 0;
    }

    ans1.clear();
    ans2.clear();
    for(int i = 1; i < n; i++){
        scanf("%d%d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
        d[u]++;
        d[v]++;
    }
    for(int i = 1; i <= n; i++){
        if(d[i] == 1){
            nh = head = i;
            break;
        }
    }
    dfs(1, 0, 0);
    printf("%d\n", ans1.size());
    for(int i = 0; i < ans1.size(); i++){
        printf("%d %d %d %d\n",ans1[i].first, ans1[i].second, ans2[i].first, ans2[i].second);
    }
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        solve();
    }
}