#include<iostream> #include<cstdlib> #include<cstdio> #include<vector> #include<cstring> usingnamespacestd; inlineintread() { int f = 1, x = 0; char ch;
do{ ch = getchar(); if (ch == '-') f = -1; }while(ch < '0' || ch > '9'); do{ x = x * 10 + ch - '0'; ch = getchar(); }while(ch >= '0' && ch <= '9'); return f * x; } constint N = 1e5;
int tt; int n; structEdge { int to, next; } edge[N * 2 + 1]; int start[N + 1], tot; int head[N + 1], tail[N + 1], deleted[N + 1]; vector<pair<int, int> > del, add;
inlinevoidaddedge(int u, int v) { edge[++tot] = { v, start[u] }; start[u] = tot; edge[++tot] = { u, start[v] }; start[v] = tot; return; } inlinevoidsolve(int u, int fa) { head[u] = tail[u] = u; vector<int>sons, delsons;
for (int i = start[u]; i; i = edge[i].next) { int v = edge[i].to; if (v == fa) continue; solve(v, u); if (!deleted[v]) { sons.push_back(v); } else { delsons.push_back(v); del.push_back(make_pair(u, v)); } } int h = 0, t = 0;
if (delsons.size()) { h = head[delsons[0]]; t = tail[delsons[0]]; for (int i = 1; i < delsons.size(); i++) { add.push_back(make_pair(t, head[delsons[i]])); t = tail[delsons[i]]; } } if (sons.size() == 1) { head[u] = u; tail[u] = tail[sons[0]]; } if (sons.size() >= 2) { head[u] = tail[sons[0]]; tail[u] = tail[sons[1]]; for (int i = 2; i < sons.size(); i++) { int v = sons[i]; del.push_back(make_pair(u, v)); add.push_back(make_pair(tail[u], head[v])); tail[u] = tail[v]; } deleted[u] = 1; } if (h) { add.push_back(make_pair(tail[u], h)); tail[u] = t; } return; } intmain() { tt = read();
while (tt--) { add.clear(); del.clear(); memset(start, 0, sizeof(int) * (N + 1)); tot = 0; memset(deleted, 0, sizeof(int) * (N + 1)); n = read(); for (int i = 1; i < n; i++) addedge(read(), read()); solve(1, 0); printf("%d\n", add.size()); for (int i = 0; i < add.size(); i++) printf("%d %d %d %d\n", del[i].first, del[i].second, add[i].first, add[i].second); }