#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<int,int>
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
	ll x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(ll a){
    if(a<0){
    	a=-a; putchar('-');
	}
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
inline ull rnd(){
	return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=55,inf=1e9;
int n,ans,sum,rd[N],a[N],b[N],d[N],A[N],B[N],ti;
vector<int> f[N],g[N],v[N];
void ins(int a,int b){
	v[a].pb(b); rd[b]++; 
}
void dfsa(int p,int fa){
	sum++;
	for(auto i:f[p])if(i!=fa){dfsa(i,p); ins(i,p);}
}
void dfsb(int p,int fa){
	for(auto i:g[p])if(i!=fa){dfsb(i,p); ins(p,i);}
}
void dfs(int p,int fa){
	ti++;
	vector<int> zs,a,b;
	for(auto i:f[p])if(i!=fa)A[i]=ti;
	for(auto i:g[p])if(i!=fa){
		B[i]=ti;
		if(A[i]==ti)zs.pb(i); else b.pb(i);
	}
	for(auto i:f[p])if(i!=fa&&B[i]!=ti)a.pb(i);
	for(auto i:zs)dfs(i,p);
	for(auto i:a)dfsa(i,p);
	for(auto i:b)dfsb(i,p);
}
void solve(vector<int> A,vector<int> B,vector<int> C,vector<int> D,int rt){
	For(i,1,n){
		f[i].clear(); g[i].clear(); v[i].clear(); rd[i]=0;
	}sum=0;
	For(i,0,n-2){
		f[A[i]].pb(B[i]);
		f[B[i]].pb(A[i]);
		g[C[i]].pb(D[i]);
		g[D[i]].pb(C[i]);
	}
	dfs(rt,0);
	queue<int> q;
	For(i,1,n)if(!rd[i])q.push(i);
	while(q.size()){
		int t=q.front(); q.pop();
		for(auto i:v[t])if(--rd[i]==0)q.push(i);
	}
	For(i,1,n)if(rd[i])return;
	ans=min(ans,sum);
}
int main(){
	int T=read();
	while(T--){
		n=read(); ans=inf;
		memset(d,0,sizeof(d));
		set<PI> s1,s2;
		For(i,1,n-1){
			a[i]=read(); b[i]=read();
			if(a[i]>b[i])swap(a[i],b[i]);
			s1.insert(mp(a[i],b[i]));
			d[a[i]]++; d[b[i]]++;
		}
		vector<int> C,D;
		For(i,1,n-1){
			int x=read(),y=read();
			if(x>y)swap(x,y);
			C.pb(x); D.pb(y);
			s2.insert(mp(x,y));
		}
		if(s1==s2){puts("0"); continue;}
		For(i,1,n)if(d[i]==1){
			For(j,1,n)if(i!=j){
				vector<int> A,B;
				For(k,1,n-1){
					if(b[k]==i)A.pb(j); else A.pb(a[k]);
					if(a[k]==i)B.pb(j); else B.pb(b[k]);
				}
				solve(A,B,C,D,i);
			}
		}
		writeln(ans==inf?-1:ans+1);
	}
}