#include<bits/stdc++.h>
using namespace std;
#define maxn 100020
#define rep(i,l,r) for(register int i = l ; i <= r ; i++)
#define repd(i,r,l) for(register int i = r ; i >= l ; i--)
#define rvc(i,S) for(register int i = 0 ; i < (int)S.size() ; i++)
#define rvcd(i,S) for(register int i = ((int)S.size()) - 1 ; i >= 0 ; i--)
#define fore(i,x)for (register int i = head[x] ; i ; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 0x3f3f3f3f
typedef long long ll;
typedef pair<int,int> pr;

struct node{
	int next,to;
}e[maxn],e2[maxn];
int head[maxn],head2[maxn],cnt,cnt2;
int fa[maxn],fa2[maxn],n,T,deg[maxn],q[maxn],hh,tt,rt,tag[maxn],tot,isleaf[maxn];
int tmp1[maxn],tmp2[maxn],ans,bl;

void clear(){
	rep(i,1,n) head[i] = head2[i] = 0 , tag[i] = tmp1[i] = tmp2[i] = isleaf[i] = deg[i] = fa[i] = fa2[i] = 0;
	tot = cnt = cnt2 = 0;
	hh = tt = 0;
}
void clear2(){
	rep(i,1,n) head[i] = 0 , tag[i] = fa2[i] = fa[i] = deg[i] = 0;
	tot = cnt = cnt2 = 0;
	hh = tt = 0;
	bl = 1;
}
void adde(int x,int y){
	e[++cnt].to = y;
	e[cnt].next = head[x];
	head[x] = cnt;
}

#define e e2
#define head head2
#define cnt cnt2

void adde2(int x,int y){
	e[++cnt].to = y;
	e[cnt].next = head[x];
	head[x] = cnt;
}
void dfs2(int x){
	for (int i = head[x] ; i ; i = e[i].next){
		if ( e[i].to == fa2[x] ) continue;
		fa2[e[i].to] = x;
		dfs2(e[i].to);
	}
}
#undef cnt
#undef e
#undef head
void dfs(int x){
	fore(i,x){
		if ( e[i].to == fa[x] ) continue;
		fa[e[i].to] = x;
		dfs(e[i].to);
	}
}
void init(){
	rt = 1;
	while ( deg[rt] <= 1 ) rt++;
	dfs(rt) , dfs2(rt);
	rep(i,1,n) if ( deg[i] == 1 ) isleaf[i] = 1;
	rep(i,1,n) tmp1[i] = fa[i] , tmp2[i] = fa2[i];
}
bool same(){
	rep(i,1,n) if ( fa[i] != fa2[i] ) tag[i] = 1 , ++tot;
	return !tot;
}
void topo(){
	rep(i,1,n) if ( !deg[i] ) q[tt++] = i;
	while ( hh < tt ){
		int x = q[hh++];
		tag[x] = 0;
		fore(i,x){
			deg[e[i].to]--;
			if ( !deg[e[i].to] ) q[tt++] = e[i].to;
		}
	}
}
void solve(){
	if ( same() ){
		ans = 0;
		return;
	}
	ans = inf;
	rep(rt,1,n){
		if ( !isleaf[rt] ) continue;
		rep(j,1,n){
		   	if ( j == rt || j == tmp1[rt] ) continue;
			clear2();
			rep(i,1,n){
				if ( i == rt ){
					adde(j,i) , adde(i,j);
			//		adde2(j,i) , adde2(i,j);
				}
				else{
					if ( tmp1[i] ) adde(tmp1[i],i) , adde(i,tmp1[i]);
			//		if ( tmp2[i] ) adde2(tmp2[i],i) , adde2(i,tmp2[i]);
				}
			}
			dfs(rt);
			dfs2(rt);
			if ( same() ){ ans = 1; return; }
			rep(i,1,n){
				int x = i;
				while ( x ) tag[i] |= tag[x] , x = fa[x];
				x = i;
				while ( x ) tag[i] |= tag[x] , x = fa2[x];
			}
			rep(i,1,n) head[i] = 0;
			cnt = 0;
			rep(i,1,n){
				if ( i == rt ) continue;
				if ( tag[fa[i]] ) adde(i,fa[i]) , deg[fa[i]]++;
				if ( tag[fa2[i]] ) adde(fa2[i],i) , deg[i]++;
			}
			topo();
			rep(i,1,n) if ( tag[i] ) bl = 0;
			if ( bl ) ans = min(ans,tot + 1);
		}
	}
}
int main(){
	//freopen("input.txt","r",stdin);
	scanf("%d",&T);
	while ( T-- ){
		clear();
		scanf("%d",&n);
		rep(i,1,n - 1){
			int x,y;
			scanf("%d %d",&x,&y);
			adde(x,y) , adde(y,x);
			deg[x]++ , deg[y]++;
		}
		rep(i,1,n - 1){
			int x,y;
			scanf("%d %d",&x,&y);
			adde2(x,y) , adde2(y,x);
		}
		init();
		solve();
		if ( ans == inf ) printf("-1\n");
		else printf("%d\n",ans);
	}
}


Measured memory_bytes: 2304