#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#include<iostream>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
#define VI vector<int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
	LL f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=55;
struct Graph
{
	vector<int> nxt[MAXN];
	void ins(int x,int y){nxt[x].emplace_back(y);}
	void addedge(int x,int y){ins(x,y);ins(y,x);}
	void clr(){for(int i=1;i<MAXN;i++)nxt[i].clear();}
}G[2],T;
int fa[2][MAXN];
map<int,int> mp[MAXN];
int n,RT;
void init(Graph &a,int x,int o)
{
	for(auto y:a.nxt[x])if(y^fa[o][x])
		fa[o][y]=x,init(a,y,o);
}
bool flag[MAXN];
int du[MAXN],vis[MAXN];
bool isleaf[MAXN],nochange[MAXN];
void init_graph1(Graph &a,int x)
{
	if(fa[1][x])nochange[x]=nochange[fa[1][x]];
	if(fa[0][x]!=fa[1][x])nochange[x]=0;
	for(auto y:a.nxt[x])if(y^fa[1][x])
		init_graph1(a,y); 
}
void init_graph(Graph &a,int x)
{
	if(fa[1][x])flag[x]=flag[fa[1][x]];
	if(fa[0][x]!=fa[1][x])flag[x]=0;
	if(!flag[x]&&fa[0][x]==fa[1][x]){flag[0]=1;}
	if(fa[0][x]!=fa[1][x])
	{
		if(!nochange[fa[0][x]])T.ins(x,fa[0][x]),++du[fa[0][x]];
		if(!nochange[fa[1][x]])T.ins(fa[1][x],x),++du[x];
	}
	for(auto y:a.nxt[x])if(y^fa[1][x])
		init_graph(a,y);
}
queue<int> li; 
int query(int rt)
{
	fa[0][rt]=fa[1][rt]=0;RT=rt;
	init(G[0],rt,0);init(G[1],rt,1);T.clr();memset(du,0,sizeof(du));
//	puts("YES");
	flag[rt]=1;flag[0]=0;nochange[rt]=1;
	init_graph1(G[1],rt);
	init_graph(G[1],rt);
	if(flag[0])return 1e9;
	int ret=0;
	for(int i=1;i<=n;i++)if(fa[0][i]!=fa[1][i]&&i!=rt)++ret;
	int sz=0;
	for(int i=1;i<=n;i++)if(!du[i])li.push(i);
	while(!li.empty())
	{
		int x=li.front();li.pop();++sz;
		for(auto y:T.nxt[x])if(!(--du[y]))li.push(y);
	}
	if(sz!=n)return 1e9;
	return ret;
}
struct edge{int x,y;}E[MAXN];
void solve()
{
	G[0].clr();G[1].clr();
	n=read();
	for(int i=1;i<MAXN;i++)mp[i].clear();
	for(int i=1;i<n;i++)
	{
		int x=read(),y=read();
		G[0].addedge(x,y);
	}int rt=-1;
	for(int i=1;i<n;i++)
	{
		int x=read(),y=read();E[i].x=x;E[i].y=y;
		G[1].addedge(x,y);
	}int ans=1e9;
//	chkmin(ans,query(3));
	for(int i=1;i<=n;i++)
		chkmin(ans,query(i));
	memset(du,0,sizeof(du));memset(isleaf,0,sizeof(isleaf));
	for(int i=1;i<n;i++)++du[E[i].x],++du[E[i].y];
	for(int i=1;i<=n;i++)if(du[i]==1)isleaf[i]=1;
	for(int i=1;i<=n;i++)if(isleaf[i])
	{
		for(int j=1;j<=n;j++)if(i!=j)
		{
			G[1].clr();
			for(int k=1;k<n;k++)if(E[k].x!=i&&E[k].y!=i)
				G[1].addedge(E[k].x,E[k].y);
			G[1].addedge(i,j);
			chkmin(ans,query(i)+1);
		}
	}
//	}
	if(ans==1e9)pr2(-1);
	else pr2(ans);
}
int main()
{
//	freopen("b.in","r",stdin);
	
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	#ifdef Rose
		double BeginJudgeTime=clock();
	#endif
	for(int __=read();__;--__)solve();
	#ifdef Rose
		double EndJudgeTime=clock();
		cerr<<"JudgeTime is"<<" ";
		cerr<<(EndJudgeTime-BeginJudgeTime)/CLOCKS_PER_SEC<<endl;
	#endif
	return 0;
}


Measured memory_bytes: 256