/*
Author: QAQAutomaton
Lang: C++
Code: F.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
	__INIT__(){
		memset(&inf,0x3f,sizeof(inf));
	}
}__INIT___;
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 ();
	}
	template<typename A>
	inline bool read (A &x) {
		for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
		for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
		return 1;
	}
	inline bool read (char &x) {
		while((x=gc())==' '||x=='\n' || x=='\r');
		return x!=EOF;
	}
	inline bool read(char *x){
		while((*x=gc())=='\n' || *x==' '||*x=='\r');
		if(*x==EOF)return 0;
		while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
		*x=0;
		return 1;
	}
	template<typename A,typename ...B>
	inline bool read(A &x,B &...y){
		return read(x)&&read(y...);
	}
	template<typename A>
	inline bool write (A 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 --]);
		return 0;
	}
	inline bool write (char x) {
		putc(x);
		return 0;
	}
	inline bool write(const char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	inline bool write(char *x){
		while(*x){putc(*x);++x;}
		return 0;
	}
	template<typename A,typename ...B>
	inline bool write(A x,B ...y){
		return write(x)||write(y...);
	}
	//no need to call flush at the end manually!
	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
vector<int> at[55],bt[55];
int t,n,u,v;
int fa[55],fb[55];
void dfs1(int x,int f){
	fa[x]=f;
	for(auto i:at[x])if(i!=f)dfs1(i,x);
}
void dfs2(int x,int f){
	fb[x]=f;
	for(auto i:bt[x])if(i!=f)dfs2(i,x);
}
vector<int> to[55];
int ind[55];
void add(int x,int y){
	to[x].push_back(y);
	++ind[y];
}
void top(int x){
	--ind[x];
	for(auto i:to[x])if(!--ind[i])top(i);
}
int chk(int x){
	dfs1(x,0);
	dfs2(x,0);
	for(int i=1;i<=n;++i){to[i].clear();ind[i]=0;}
	int cnt=0;
	for(int i=1;i<=n;++i){
		if(fa[i]!=fb[i]){
			++cnt;			
			for(auto j:at[i])if(j!=fa[i] && fb[j]==i){cnt=inf;break;}
			for(auto j:at[i])if(j!=fa[i])add(j,i);
		//	nx[j]|=1ll<<i;
			for(auto j:bt[i])if(j!=fb[i])add(i,j);
		//	nx[i]|=1ll<<j;
		}	
	}
	for(int i=1;i<=n;++i)if(!ind[i])top(i);
	for(int i=1;i<=n;++i)if(ind[i]>0)cnt=inf;
	return cnt;
}
signed main(){
#ifdef QAQAutoMaton 
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
#endif
	read(t);
	for(;t;--t){
		read(n);
		for(int i=1;i<=n;++i){	
			at[i].clear();
			bt[i].clear();
		}
		for(int i=1;i<n;++i){
			read(u,v);	
			at[u].push_back(v);
			at[v].push_back(u);
		}
		for(int i=1;i<n;++i){
			read(u,v);	
			bt[u].push_back(v);
			bt[v].push_back(u);
		}
		int ans=inf;
		for(int i=1;i<=n;++i){
			chkmin(ans,chk(i));
			if(at[i].size()==1){
				int nx=at[i][0];	
				at[i].pop_back();
				for(auto &j:at[nx])if(j==i){swap(j,at[nx].back());at[nx].pop_back();break;}
				for(int j=1;j<=n;++j)if(i!=j){
					at[i].push_back(j);at[j].push_back(i);	
					chkmin(ans,chk(i)+1);
					at[i].pop_back();at[j].pop_back();
				}
				at[i].push_back(nx);
				at[nx].push_back(i);
			}
		}	
		write(ans==inf?-1:ans,'\n');
	}
	return 0;
}



Measured memory_bytes: 2304