/*
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;
}


### Prompt

Please create a solution in Cpp to the following problem:

Snuke has found two trees A and B, each with N vertices numbered 1 to N. The i-th edge of A connects Vertex a_i and b_i, and the j-th edge of B connects Vertex c_j and d_j. Also, all vertices of A are initially painted white.

Snuke would like to perform the following operation on A zero or more times so that A coincides with B:

* Choose a leaf vertex that is painted white. (Let this vertex be v.)
* Remove the edge incident to v, and add a new edge that connects v to another vertex.
* Paint v black.



Determine if A can be made to coincide with B, ignoring color. If the answer is yes, find the minimum number of operations required.

You are given T cases of this kind. Find the answer for each of them.

Constraints

* 1 \leq T \leq 20
* 3 \leq N \leq 50
* 1 \leq a_i,b_i,c_i,d_i \leq N
* All given graphs are trees.

Input

Input is given from Standard Input in the following format:


T
case_1
:
case_{T}


Each case is given in the following format:


N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}


Output

For each case, if A can be made to coincide with B ignoring color, print the minimum number of operations required, and print `-1` if it cannot.

Examples

Input

2
3
1 2
2 3
1 3
3 2
6
1 2
2 3
3 4
4 5
5 6
1 2
2 4
4 3
3 5
5 6


Output

1
-1


Input

3
8
2 7
4 8
8 6
7 1
7 3
5 7
7 8
4 2
5 2
1 2
8 1
3 2
2 6
2 7
4
1 2
2 3
3 4
3 4
2 1
3 2
9
5 3
4 3
9 3
6 8
2 3
1 3
3 8
1 7
4 1
2 8
9 6
3 6
3 5
1 8
9 7
1 6


Output

6
0
7

### Response

```cpp
/*
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;
}


```