#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=55;
vector<int> e1[N],e2[N],e3[N];
int q[N],deg[N],n,ans,sum;
int vis[N][N],banx,bany;
int fa1[N],fa2[N],spe[N];
bool topo(){
	int h=0,t=0;
	For(i,1,n) if (!deg[i]) q[++t]=i;
	while (h!=t){
		int x=q[++h];
		For(i,0,e3[x].size()-1)
			if (!--deg[e3[x][i]])
				q[++t]=e3[x][i];
	}
	return t==n;
}
void dfs1(int x,int fl){
	sum-=fl; spe[x]=fl;
	For(i,0,e1[x].size()-1)
		if (e1[x][i]!=fa1[x]&&vis[x][e1[x][i]]!=-1){	
			fa1[e1[x][i]]=x;
			dfs1(e1[x][i],vis[x][e1[x][i]]&fl);
		}
}
void dfs2(int x){
	For(i,0,e2[x].size()-1)
		if (e2[x][i]!=fa2[x]){	
			fa2[e2[x][i]]=x;
			dfs2(e2[x][i]);
		}
}
int check(int id){
	memset(fa1,0,sizeof(fa1));
	memset(fa2,0,sizeof(fa2));
	memset(deg,0,sizeof(deg));
	int fl=vis[banx][bany]; sum=n;
	vis[banx][bany]=vis[bany][banx]=-1;
	dfs1(id,1); dfs2(id);
	For(i,1,n) e3[i].clear();
	For(i,1,n) if (!spe[i]){
		if (!spe[fa1[i]]){
			e3[i].PB(fa1[i]);
			++deg[fa1[i]];
		}
		if (!spe[fa2[i]]){
			e3[fa2[i]].PB(i);	
			++deg[i];
		}
	}
	if (!topo()) sum=1<<30;
	vis[banx][bany]=vis[bany][banx]=fl;
	//printf("QUERY %d %d\n",id,sum);
	return sum;
}
void solve(){
	scanf("%d",&n);
	For(i,1,n) e1[i].clear();
	For(i,1,n) e2[i].clear();
	memset(vis,0,sizeof(vis));
	For(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		e1[x].PB(y); e1[y].PB(x);
	}
	For(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		e2[x].PB(y); e2[y].PB(x);
		vis[x][y]=vis[y][x]=1;
	}
	int ans=1<<30;
	banx=bany=0;
	For(i,1,n) ans=min(ans,check(i));
	For(i,1,n) if (e1[i].size()==1){
		int opp=e1[i][0];
		For(j,1,n) if (j!=i&&j!=opp){
			banx=i,bany=opp;
			e1[i].PB(j); e1[j].PB(i);
			ans=min(ans,check(i)+1);
			e1[i].pop_back();
			e1[j].pop_back();
		}
	}
	if (ans>1<<29)
		ans=-1;
	printf("%d\n",ans);
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}
### Prompt

Generate a cpp solution 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
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=55;
vector<int> e1[N],e2[N],e3[N];
int q[N],deg[N],n,ans,sum;
int vis[N][N],banx,bany;
int fa1[N],fa2[N],spe[N];
bool topo(){
	int h=0,t=0;
	For(i,1,n) if (!deg[i]) q[++t]=i;
	while (h!=t){
		int x=q[++h];
		For(i,0,e3[x].size()-1)
			if (!--deg[e3[x][i]])
				q[++t]=e3[x][i];
	}
	return t==n;
}
void dfs1(int x,int fl){
	sum-=fl; spe[x]=fl;
	For(i,0,e1[x].size()-1)
		if (e1[x][i]!=fa1[x]&&vis[x][e1[x][i]]!=-1){	
			fa1[e1[x][i]]=x;
			dfs1(e1[x][i],vis[x][e1[x][i]]&fl);
		}
}
void dfs2(int x){
	For(i,0,e2[x].size()-1)
		if (e2[x][i]!=fa2[x]){	
			fa2[e2[x][i]]=x;
			dfs2(e2[x][i]);
		}
}
int check(int id){
	memset(fa1,0,sizeof(fa1));
	memset(fa2,0,sizeof(fa2));
	memset(deg,0,sizeof(deg));
	int fl=vis[banx][bany]; sum=n;
	vis[banx][bany]=vis[bany][banx]=-1;
	dfs1(id,1); dfs2(id);
	For(i,1,n) e3[i].clear();
	For(i,1,n) if (!spe[i]){
		if (!spe[fa1[i]]){
			e3[i].PB(fa1[i]);
			++deg[fa1[i]];
		}
		if (!spe[fa2[i]]){
			e3[fa2[i]].PB(i);	
			++deg[i];
		}
	}
	if (!topo()) sum=1<<30;
	vis[banx][bany]=vis[bany][banx]=fl;
	//printf("QUERY %d %d\n",id,sum);
	return sum;
}
void solve(){
	scanf("%d",&n);
	For(i,1,n) e1[i].clear();
	For(i,1,n) e2[i].clear();
	memset(vis,0,sizeof(vis));
	For(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		e1[x].PB(y); e1[y].PB(x);
	}
	For(i,1,n-1){
		int x,y;
		scanf("%d%d",&x,&y);
		e2[x].PB(y); e2[y].PB(x);
		vis[x][y]=vis[y][x]=1;
	}
	int ans=1<<30;
	banx=bany=0;
	For(i,1,n) ans=min(ans,check(i));
	For(i,1,n) if (e1[i].size()==1){
		int opp=e1[i][0];
		For(j,1,n) if (j!=i&&j!=opp){
			banx=i,bany=opp;
			e1[i].PB(j); e1[j].PB(i);
			ans=min(ans,check(i)+1);
			e1[i].pop_back();
			e1[j].pop_back();
		}
	}
	if (ans>1<<29)
		ans=-1;
	printf("%d\n",ans);
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}
```