#include <bits/stdc++.h>
#define PB push_back
#define MP std::make_pair
#define FI first
#define SE second
#define M 1000005
#define INF 1000000000
int t,n,faA[M],faB[M];
std::vector <int> EdgeA[M],EdgeB[M],tmp[M],G[M];
bool flag[M];
int read(){
	char c=getchar();int ans=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans;
}
void Write(int x){
	if (x<0) putchar('-'),x=-x;
	if (x<10) putchar(x+'0');
	else Write(x/10),putchar(x%10+'0');
	return;
}
int min(int x,int y){return x<y?x:y;}
int max(int x,int y){return x>y?x:y;}
void DFS_A(int x,int f){
	faA[x]=f;
	for (auto i:tmp[x])
		if (i!=f) DFS_A(i,x);
	return;
}
void DFS_B(int x,int f){
	faB[x]=f;
	for (auto i:EdgeB[x])
		if (i!=f) DFS_B(i,x);
	return;
}
bool topo_sort(){
	std::queue <int> Q;
	int cnt=0;static int deg[M];
	for (int i=1;i<=n;i++) deg[i]=0,cnt+=flag[i];
	for (int i=1;i<=n;i++)
		for (auto j:G[i]) deg[j]++;
	for (int i=1;i<=n;i++)
		if (flag[i]&&!deg[i]) Q.push(i),--cnt;
	while (!Q.empty()){
		int now=Q.front();Q.pop();
		for (auto j:G[now])
			if (!--deg[j]) Q.push(j),--cnt;
	}
	return !cnt;
}
int check(int x,int y){
	if (EdgeA[x][0]==y) return INF;
	int ans=0;
	for (int i=1;i<=n;i++)
		for (auto j:EdgeA[i])
			if (!(i==x&&j==EdgeA[x][0])&&!(i==EdgeA[x][0]&&j==x)) tmp[i].PB(j),tmp[j].PB(i);
	tmp[x].PB(y),tmp[y].PB(x),DFS_A(x,0),DFS_B(x,0);
	for (int i=1;i<=n;i++) ans+=(flag[i]=(faA[i]!=faB[i]));
	for (int i=1;i<=n;i++)
		if (faA[i]&&flag[faA[i]]&&!flag[i]) return INF;
	for (int i=1;i<=n;i++)
		if (faA[i]&&flag[i]&&flag[faA[i]]) G[i].PB(faA[i]);
	for (int i=1;i<=n;i++)
		if (faB[i]&&flag[i]&&flag[faB[i]]) G[faB[i]].PB(i);
	return topo_sort()?ans:INF;
}
int main(){
	t=read();
	while (t--){
		n=read();int ans=INF;bool flag=1;
		for (int i=1,u,v;i<n;i++) u=read(),v=read(),EdgeA[u].PB(v),EdgeA[v].PB(u);
		for (int i=1,u,v;i<n;i++) u=read(),v=read(),EdgeB[u].PB(v),EdgeB[v].PB(u);
		for (int i=1;i<=n;i++){
			std::sort(EdgeA[i].begin(),EdgeA[i].end());
			std::sort(EdgeB[i].begin(),EdgeB[i].end());
			flag&=(EdgeA[i]==EdgeB[i]);
		}
		if (flag) printf("0\n");
		else {
			for (int i=1;i<=n;i++)
				for (int j=1;j<=n;j++)
					if (EdgeA[i].size()==1){
						ans=min(ans,check(i,j)+1);
						for (int i=1;i<=n;i++) G[i].clear(),tmp[i].clear();
					}
			Write(ans==INF?-1:ans),putchar('\n');
		}
		for (int i=1;i<=n;i++) EdgeA[i].clear(),EdgeB[i].clear();
	}
	return 0;
}
