#include <bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
#define rll register ll
namespace io {
	const int SIZE = (1 << 21) + 1;
	char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
	// getchar
	#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
	// print the remaining part
	inline void flush () {
		fwrite (obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	// putchar
	inline void putc (char x) {
		*oS ++ = x;
		if (oS == oT) flush ();
	}
	// input a signed integer
	template <class I>
	inline void gi (I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = (x << 1) + (x << 3) + (c & 15); x *= f;
	}
	// input a letter
	template <class I>
	inline void get (I &x) {
		for (c = gc(); c < 'A' || c > 'Z'; c = gc()) ;
		x = c;
	}
	// print a signed integer
	template <class I>
	inline void print (I x) {
		if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
		while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
		while (qr) putc (qu[qr --]);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: get;
using io :: putc;
using io :: print;
const int N=55,inf=2e9;
int n;
struct tree{
	int G[N][N],fa[N],v[N];
	inline void clear(){
		ri i,j;
		for(i=1;i<=n;++i)
			for(j=1;j<=n;++j)
				G[i][j]=0;
	}
	inline void addedge(ri x,ri y){
		G[x][y]=G[y][x]=1;
	}
	inline void dfs(ri x){
		for(ri i=1;i<=n;++i)
			if(G[x][i]&&i!=fa[x])
				fa[i]=x,
				dfs(i);
	}
}ta,tb;
int G[N][N],deg[N],q[N],link[N];
inline bool check(){
	ri l,r,i,j,x;
	l=1;r=0;
	for(i=1;i<=n;++i) deg[i]=0;
	for(i=1;i<=n;++i)
		for(j=1;j<=n;++j)
			if(G[i][j])
				++deg[j];
	for(i=1;i<=n;++i)
		if(!deg[i]) q[++r]=i;
	while(l<=r){
		x=q[l++];
		for(i=1;i<=n;++i)
			if(G[x][i]&&!--deg[i])
				q[++r]=i;
	}
	return r==n;
}
int degree[N];
int main(){
	ri t,i,j,x,y,rt,flag,res,ans,o;
	gi(t);
	while(t--){
		gi(n);
		ta.clear();
		for(i=1;i<=n;++i) degree[i]=0;
		for(i=1;i^n;++i) gi(x),gi(y),ta.addedge(x,y),++degree[x],++degree[y],link[x]=y,link[y]=x;
		tb.clear();
		for(i=1;i^n;++i) gi(x),gi(y),tb.addedge(x,y);
		ta.fa[1]=0;
		ta.dfs(1);
		tb.fa[1]=0;
		tb.dfs(1);
		flag=1;
		for(i=1;i<=n;++i)
			if(ta.fa[i]!=tb.fa[i]){
				flag=0;
				break;
			}
		if(flag){
			print(0);putc('\n');
			continue;
		}
		ans=inf;
		for(rt=1;rt<=n;++rt)
			if(degree[rt]==1){
				ta.G[rt][link[rt]]=ta.G[link[rt]][rt]=0;
				tb.fa[rt]=0;
				tb.dfs(rt);
				for(o=1;o<=n;++o){
					ta.G[rt][o]=ta.G[o][rt]=1;
					ta.fa[rt]=0;
					ta.dfs(rt);
					res=0;
					for(i=1;i<=n;++i)
						ta.v[i]=ta.fa[i]!=tb.fa[i],
						res+=ta.v[i];
					flag=1;
					for(i=1;i<=n;++i)
						if(!ta.v[i]&&ta.v[ta.fa[i]]){
							flag=0;
							break;
						}
					if(!flag){
						ta.G[rt][o]=ta.G[o][rt]=0;
						continue;
					}
					for(i=1;i<=n;++i)
						for(j=1;j<=n;++j)
							G[i][j]=0;
					for(i=1;i<=n;++i)
						if(ta.v[i]){
							if(tb.fa[i]) G[i][tb.fa[i]]=1;
							for(j=1;j<=n;++j)
								if(ta.fa[j]==i)
									G[i][j]=1;
						}
					if(check())
						ans=min(ans,res+1);
					ta.G[rt][o]=ta.G[o][rt]=0;
				}
				ta.G[rt][link[rt]]=ta.G[link[rt]][rt]=1;
			}
		ans=ans==inf?-1:ans;
		print(ans);putc('\n');
	}
	return 0;
}

Measured memory_bytes: 2304