#include   <bits/stdc++.h>
using   namespace  std;
 
#define   int   long   long
#define  mod  1000000007
#define  pb push_back
#define  ss second
#define  ff first
#define  all(v)  v.begin(),  v.end()
#define  deb(x)  cerr  <<   "\n" \
                    << #x << "=" << x << "\n";
#define  deb2(x,  y)  cerr  <<   "\n"                   \
                        << #x << "=" << x << "\n" \
                        << #y << "=" << y << "\n";
#define  w(x) \
  int  x;     \
cin  >>  x;  \
  while   (x--)
 
int  n,m;
const   int  N  =   1e5   +   2;
vector<int>  v[N];
vector<vector<int>>  add,erase;
vector<pair<int,int>>  ds;
 
pair<int,int>  dfs(int  x,int  p  =   0) {
	vector<vector<int>>  children;
 
	for(int  i:  v[x]) {
		if(i  !=  p) {
pair<int,int>  res  =  dfs(i,x);
			if(res.ff  !=  INT_MAX) {
children.pb({i,res.ff,res.ss});
			}
		}
	}
 
	if(children.size()   <=   1) {
		if(p  ==   0)
ds.pb({x,x});
		if(children.empty())
			return   {x,x};
		return   {children[0][1],children[0][2]};
	}
	
	if(children.size()   >   2) {
		for(int  i  =   0;i  <  children.size()   -   2;  ++i) {
erase.pb({children[i][0],x});
ds.pb({children[i][0],children[i][1]});
		}
	}
	if(p  !=   0) {
erase.pb({x,p});
	}
 
ds.pb({children[children.size()   -   1][1],children[children.size()   -   2][1]});
 
	return   {INT_MAX,INT_MIN};
}
 
int32_t  main() {
 
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
	
w(tt) {
cin  >>  n;
m  =  n  -   1;
 
		for(int  i  =   0;i  <  m;  ++i) {
			int  a,  b;
cin  >>  a  >>  b;
v[a].push_back(b);
v[b].push_back(a);
		}
 
dfs(1);
 
		for(int  i  =   1;i  <  ds.size();  ++i) {
add.pb({ds[i].ff,ds[i  -   1].ss});
		}
 
assert(add.size()   ==  erase.size());
 
cout  <<  add.size()   <<   "\n";
 
		for(int  i  =   0;i  <  add.size();  ++i) {
cout  <<  erase[i][0]   <<   " "   <<  erase[i][1]   <<   " "   <<  add[i][0]   <<   " "   <<  add[i][1]   <<   "\n";
		}
 
add.clear();
erase.clear();
ds.clear();
		for(int  i  =   0;i  <=  n;  ++i)
v[i].clear();
	}
	return   0;
}
