#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#define LL long long
#define LD long double
using namespace std;
const int NN= +117;
const int MM= +117;
int read(){
	int fl=1,x;char c;
	for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
	if(c=='-'){fl=-1;c=getchar();}
	for(x=0;c>='0'&&c<='9';c=getchar())
		x=(x<<3)+(x<<1)+c-'0';
	return x*fl;
}
void open(){
	freopen("a.in","r",stdin);
	//freopen("a.out","w",stdout);
}
void close(){
	fclose(stdin);
	fclose(stdout);
}


int m,n;
struct edge{
	int x,y;
	
};
bool operator <(const edge &a, const edge &b){
	return a.x==b.x?a.y<b.y:a.x<b.x;
}
bool operator ==(const edge &a, const edge &b){
	return a.x==b.x&&a.y==b.y;
}
edge ae[NN]={},be[NN]={};
edge com[NN]={};
vector<int> a[NN]={};
int dega[NN]={};
int adj[NN]={};
vector<int> b[NN]={};
vector<int> sam[NN]={};
vector<int> p[NN]={};
bool vis[NN]={};
int tu[NN]={};
bool cir;
void dfs(int x,int fa){
	vis[x]=1;
	for(int i=0;i<sam[x].size();++i){
		int cur=sam[x][i];
		if(cur!=fa){
			dfs(cur,x);
		}
	}
}
void adda(int x,int fa){
	for(int i=0;i<a[x].size();++i){
		int cur=a[x][i];
		if(cur!=fa){
			p[cur].push_back(x);
			adda(cur,x);
		}
	}
}
void addb(int x,int fa){
	for(int i=0;i<b[x].size();++i){
		int cur=b[x][i];
		if(cur!=fa){
			p[x].push_back(cur);
			addb(cur,x);
		}
	}
}
void chk(int x){
	tu[x]=1;
	for(int i=0;i<p[x].size();++i){
		int cur=p[x][i];
		if(tu[cur]==1)cir=1;
		if(!tu[cur])chk(cur);
	}
	tu[x]=2;
}
int calc(int rt){
	for(int i=1;i<=n;++i){
		p[i].clear();
		vis[i]=0;
		tu[i]=0;
	}
	dfs(rt,0);
	int res=n;
	for(int i=1;i<=n;++i){
		if(vis[i]){
			--res;
			for(int j=0;j<a[i].size();++j){
				int cur=a[i][j];
				if(!vis[cur]){
					adda(cur,i);
				}
			}
			for(int j=0;j<b[i].size();++j){
				int cur=b[i][j];
				if(!vis[cur]){
					addb(cur,i);
				}
			}
		}
	}
	cir=0;
	for(int i=1;i<=n;++i){
		if(!tu[i])chk(i);
	}
	if(cir)res=n+1;
	//cerr<<res<<endl;
	return res;
}
void solve(){
	n=read();
	for(int i=1;i<=n;++i){
		a[i].clear();
		b[i].clear();
		sam[i].clear();
		dega[i]=0;
	}
	for(int i=1;i<n;++i){
		int x=read();
		int y=read();
		if(x>y)swap(x,y);
		ae[i].x=x,ae[i].y=y;
		dega[x]++;
		adj[x]=y;
		dega[y]++;
		adj[y]=x;
	}
	for(int i=1;i<n;++i){
		int x=read();
		int y=read();
		if(x>y)swap(x,y);
		be[i].x=x,be[i].y=y;
	}
	sort(ae+1,ae+n);
	sort(be+1,be+n);
	int pa=1,pb=1;
	int ccnt=0;
	while(pa!=n&&pb!=n){
		if(ae[pa]<be[pb])++pa;
		else if(be[pb]<ae[pa])++pb;
		else{
			com[++ccnt]=ae[pa];
			++pa,++pb;
		}
	}
	for(int i=1;i<n;++i){
		int x=ae[i].x,y=ae[i].y;
		a[x].push_back(y);
		a[y].push_back(x);
	}
	for(int i=1;i<n;++i){
		int x=be[i].x,y=be[i].y;
		b[x].push_back(y);
		b[y].push_back(x);
	}
	for(int i=1;i<=ccnt;++i){
		int x=com[i].x,y=com[i].y;
		sam[x].push_back(y);
		sam[y].push_back(x);
	}
	int ans=n+1;
	for(int i=1;i<=n;++i){
		ans=min(ans,calc(i));
	}
	for(int i=1;i<=n;++i){
		if(dega[i]==1){
			for(int j=1;j<=n;++j){
				if(i!=j){
					for(int k=1;k<=n;++k){
						a[k].clear();
						sam[k].clear();
					}
					edge now,ad;
					now.x=i,now.y=adj[i];
					ad.x=i,ad.y=j;
					if(now.x>now.y)swap(now.x,now.y);
					if(ad.x>ad.y)swap(ad.x,ad.y);
					for(int i=1;i<n;++i){
						if(ae[i]==now){
							continue;
						}
						int x=ae[i].x,y=ae[i].y;
						a[x].push_back(y);
						a[y].push_back(x);
						if(be[i]==ad){
							x=be[i].x,y=be[i].y;
							sam[x].push_back(y);
							sam[y].push_back(x);
						}
					}
					a[ad.x].push_back(ad.y);
					a[ad.y].push_back(ad.x);
					for(int i=1;i<=ccnt;++i){
						if(com[i]==now)continue;
						int x=com[i].x,y=com[i].y;
						sam[x].push_back(y);
						sam[y].push_back(x);
					}
					ans=min(ans,calc(i)+1);
				}
			}
		}
	}
	if(ans>n){
		printf("-1\n");
	}
	else printf("%d\n",ans);
}
int main(){
	//open();
	int T=read();
	while(T--){
		solve();
	}


	close();
	return 0;
}

Measured memory_bytes: 256