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

const long N = 1e5 + 5;

typedef pair<long, long> ii;

struct ope
{
    long x, y, u, v;
};

vector<ope> operations;
long n, answer, deg[N];
vector<long> g[N];
bool isParent[N];
map<long, bool> isDeleted[N];
bool vis[N];
vector<ii> con;

void dfs(long u, long p)
{
    long nchild = 0;
    for (long v: g[u])
    {
        if (v == p) continue;
        dfs(v, u);
        if (!isParent[v]) nchild++;
    }
    if (nchild <= 1) return;
    if (nchild == 2)
    {
        if (u != p)
        {
            answer++;
            isDeleted[u][p] = true;
            isDeleted[p][u] = true;
            operations.push_back({u, p, 0, 0});
            isParent[u] = true;
        }
        return;
    }
    answer += nchild - 2;
    for (long v: g[u])
    {
        if (v == p || isParent[v]) continue;
        isDeleted[u][v] = true;
        isDeleted[v][u] = true;
        operations.push_back({u, v, 0, 0});
        nchild--;
        if (nchild == 2) break;
    }
    if (u != p)
    {
        answer++;
        isDeleted[u][p] = true;
        isDeleted[p][u] = true;
        operations.push_back({u, p, 0, 0});
        isParent[u] = true;
    }
}

void Reset()
{
    for (long i = 1; i <= n; i++) g[i].clear(), isParent[i] = false, isDeleted[i].clear(), vis[i] = false, deg[i] = 0;
    answer = 0;
    operations.clear();
    con.clear();
}

void visit(long u, long p)
{
    vis[u] = true;
    for (long v: g[u])
    {
        if (v == p || isDeleted[u][v]) continue;
        deg[u]++;
        visit(v, u);
    }
    if (u != p) deg[u]++;
    if (deg[u] <= 1)
    {
        if (con[con.size() - 1].first == 0) con[con.size() - 1].first = u;
        else con[con.size() - 1].second = u;
    }
}

void trace()
{
    for (long i = 1; i <= n; i++) if (!vis[i]) con.push_back(ii(0, 0)), visit(i, i);
    long V = con[0].first;
    long pos = 0;
    for (long i = 1; i < con.size(); i++)
    {
        operations[pos].u = V;
        operations[pos].v = con[i].first;
        if (con[i].second == 0) V = con[i].first;
        else V = con[i].second;
        pos++;
    }
    cout << operations.size() << '\n';
    for (long i = 0; i < operations.size(); i++)
    {
        cout << operations[i].x << ' ' << operations[i].y << ' ' << operations[i].u << ' ' << operations[i].v << '\n';
    }
}

void solve()
{
    cin >> n;
    for (long i = 1; i <= n - 1; i++)
    {
        long u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1, 1);
    trace();
    Reset();
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
    long nTest;
    cin >> nTest;
    while (nTest--)
    {
        solve();
    }
    return 0;
}
