#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100005
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x) & (x))
#pragma GCC optimize(2)
LL read() {
	LL f = 1,x = 0;char s = getchar();
	while(s < '0' || s > '9') {if(s=='-')f = -f;s = getchar();}
	while(s >= '0' && s <= '9') {x=x*10+(s-'0');s = getchar();}
	return f * x;
}
int n,m,i,j,s,o,k,rt;
vector<int> g[MAXN];
int tl[MAXN];
int a[MAXN],b[MAXN],c[MAXN],d[MAXN],cnt;
void dfs(int x,int ff) {
	int ct = 0;
	for(int i = 0;i < (int)g[x].size();i ++) {
		int y = g[x][i];
		if(y != ff) {
			dfs(y,x);
			if(tl[y]) {
				if(!ct) ct = y;
				else if(ct > 0) {
					int l = tl[ct],r = tl[y];
					++ cnt;
					a[cnt] = x,b[cnt] = ff;
					c[cnt] = rt,d[cnt] = l;
					rt = r;
					ct = -1;
				}
				else {
					++ cnt;
					a[cnt] = y,b[cnt] = x;
					c[cnt] = rt,d[cnt] = y;
					rt = tl[y];
				}
			}
		}
	}
	if(ct == 0) tl[x] = x;
	else if(ct > 0) tl[x] = tl[ct];
	else tl[x] = 0;
	return ;
}
int main() {
	int T = read();
	while(T --) {
		n = read();
		for(int i = 1;i <= n;i ++) g[i].clear(),tl[i] = 0;
		for(int i = 1;i < n;i ++) {
			s = read();o = read();
			g[s].push_back(o);
			g[o].push_back(s);
		}
		rt = 0;cnt = 0;
		for(int i = 1;i <= n;i ++) {
			if((int)g[i].size() == 1) {rt = i;break;}
		}
		dfs(rt,0);
		printf("%d\n",cnt);
		for(int i = 1;i <= cnt;i ++) {
			printf("%d %d %d %d\n",a[i],b[i],c[i],d[i]);
		}
	}
	return 0;
}
