/*
 * @Author: your name
 * @Date: 2021-05-08 19:41:57
 * @LastEditTime: 2021-05-08 20:27:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \code_formal\codeforces\cf edu108-\720_4.cpp
 */
#include <bits/stdc++.h>
#define pr printf
#define sc scanf
#define for0(i, n) for (i = 0; i < n; i++)
#define for1n(i, n) for (i = 1; i <= n; i++)
#define forab(i, a, b) for (i = a; i <= b; i++)
#define forba(i, a, b) for (i = b; i >= a; i--)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<vector<int>>
#define vt2 vector<tuple<int, int>>
#define vt3 vector<tuple<int, int, int>>
#define mem(ara, n) memset(ara, n, sizeof(ara))
#define memb(ara) memset(ara, false, sizeof(ara))
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define sz(x) x.size()
const int N = 2e5 + 100;
const int mod = 1e9 + 7;
namespace often
{
    inline void input(int &res)
    {
        char c = getchar();
        res = 0;
        int f = 1;
        while (!isdigit(c))
        {
            f ^= c == '-';
            c = getchar();
        }
        while (isdigit(c))
        {
            res = (res << 3) + (res << 1) + (c ^ 48);
            c = getchar();
        }
        res = f ? res : -res;
    }
    inline int qpow(int a, int b)
    {
        int ans = 1, base = a;
        while (b)
        {
            if (b & 1)
                ans = (ans * base % mod + mod) % mod;
            base = (base * base % mod + mod) % mod;
            b >>= 1;
        }
        return ans;
    }
    int fact(int n)
    {
        int res = 1;
        for (int i = 1; i <= n; i++)
            res = res * 1ll * i % mod;
        return res;
    }
    int C(int n, int k)
    {
        return fact(n) * 1ll * qpow(fact(k), mod - 2) % mod * 1ll * qpow(fact(n - k), mod - 2) % mod;
    }
    int exgcd(int a, int b, int &x, int &y)
    {
        if (b == 0)
        {
            x = 1, y = 0;
            return a;
        }
        int res = exgcd(b, a % b, x, y);
        int t = y;
        y = x - (a / b) * y;
        x = t;
        return res;
    }
    int invmod(int a, int mod)
    {
        int x, y;
        exgcd(a, mod, x, y);
        x %= mod;
        if (x < 0)
            x += mod;
        return x;
    }
}
using namespace often;
using namespace std;

signed main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    int _ = 1;
    cin >> _;
    while (_--)
    {
        int n;
        cin >> n;
        vector<vector<int>> g(n+1);
        for (int i = 1; i < n; i++)
        {
            int u, v;
            cin >> u >> v;
            g[u].emplace_back(v);
            g[v].emplace_back(u);
        }
        vector<tuple<int, int>>  add;
        vector<tuple<int, int>>  remove;
        function<tuple<int, int>(int, int)> dfs = [&](int u, int p) {
            tuple<int, int> cur = make_tuple(u, u);
            vector<tuple<int, int>>  vec;
            for (auto v : g[u])
            {
                if (v != p)
                {
                    auto x = dfs(v, u);
                    if (get<1>(x) == v && get<1>(cur) == u)
                    {
                        (get<0>(cur) == u ? get<0>(cur) : get<1>(cur)) = get<0>(x);
                    }
                    else
                    {
                        vec.emplace_back(x);
                        remove.emplace_back(u, v);
                    }
                }
            }
            for (auto x : vec)
            {
                add.emplace_back(get<0>(cur), get<1>(x));
                get<0>(cur) = get<0>(x);
            }
            return cur;
        };
        dfs(1, -1);
        cout << (int)sz(add) << endl;
        for (int i = 0; i < (int)sz(add); i++)
        {
            int x0, y0, x1, y1;
            tie(x0, y0) = remove[i];
            tie(x1, y1) = add[i];
            cout << x0 << " " << y0 << " " << x1 << " " << y1 << endl;
        }
    }

    return 0;
}