#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#define int long long

using namespace std;

const int N=109,INF=1<<30;
int flag[N],head[N],cnt,deg[N],n;
struct Tree
{
	int head[N],cnt,F[N],x[N],y[N];
	struct Edge
	{
		int nxt,to;
	}g[N*2];
	
	void add(int from,int to)
	{
		g[++cnt].nxt=head[from];
		g[cnt].to=to;
		head[from]=cnt;
	}
	
	void clear()
	{
		cnt=0;
		memset(head,0,sizeof(head));
	}
	
	void dfs(int x,int fa)
	{
		for (int i=head[x];i;i=g[i].nxt)
		{
			int v=g[i].to;
			if(v==fa)
				continue;
			F[v]=x,dfs(v,x);
		}
	}
}A,B,C;

void DFS_A(int x,int fa)
{
	for (int i=A.head[x];i;i=A.g[i].nxt)
	{
		int v=A.g[i].to;
		if(v==fa)
			continue;
		if(flag[v]&&flag[x])
			C.add(v,x);
		DFS_A(v,x);
	}
}

void DFS_B(int x,int fa)
{
	for (int i=B.head[x];i;i=B.g[i].nxt)
	{
		int v=B.g[i].to;
		if(v==fa)
			continue;
		if(flag[x]&&flag[v])
			C.add(x,v);
		DFS_B(v,x);
	}
}

void init()
{
	B.clear();
	memset(deg,0,sizeof(deg));
	scanf("%lld",&n);
	for (int i=1;i<n;i++)
		scanf("%lld %lld",&A.x[i],&A.y[i]),deg[A.x[i]]++,deg[A.y[i]]++;
	for (int i=1,x,y;i<n;i++)
		scanf("%lld %lld",&x,&y),B.add(x,y),B.add(y,x);
}

bool TP_sort()
{
	int ans=n;
//	for (int i=1;i<=n;i++)
//		if(flag[i]) ans++;
	queue <int> q;
	static int Deg[N];
	memset(Deg,0,sizeof(Deg));
	for (int i=1;i<=n;i++)
		for (int j=C.head[i];j;j=C.g[j].nxt)
		{
			int v=C.g[j].to;
			Deg[v]++;
		}
//	puts("!!!");
//	for (int i=1;i<=n;i++)
//		for (int j=C.head[i];j;j=C.g[j].nxt)
//			printf("%lld %lld\n",i,C.g[j].to);
	for (int i=1;i<=n;i++)
		if(Deg[i]==0) ans--,q.push(i);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for (int i=C.head[x];i;i=C.g[i].nxt)
		{
			int v=C.g[i].to;
			Deg[v]--;
			if(Deg[v]==0)
				ans--,q.push(v);
		}
	}
//	printf("ans:%lld\n",ans);
	return !ans;
}

void work()
{
	int ans=INF;
	for (int i=1;i<=n;i++)
	{
		if(deg[i]!=1) continue;
		for (int j=1;j<=n;j++)
		{
			if(i==j) continue;
			int qwq=1;
			for (int k=1;k<n;k++)
				if((A.x[k]==i&&A.y[k]==j)||(A.x[k]==j&&A.y[k]==i))
				{
					qwq=0;
					break;
				}
			A.clear();
			for (int k=1;k<n;k++)
				if(A.x[k]!=i&&A.y[k]!=i)
					A.add(A.x[k],A.y[k]),A.add(A.y[k],A.x[k]);
			A.add(i,j),A.add(j,i);
			A.dfs(i,A.F[i]=-1),B.dfs(i,B.F[i]=-1);
			int tmp=0;
			for (int k=1;k<=n;k++)
				tmp+=(flag[k]=!(A.F[k]==B.F[k]));
			int qaq=1;
			for (int k=1;k<=n;k++)
				if(k!=i&&flag[B.F[k]]&&!flag[k])
				{
					qaq=0;
					break;
				}
			if(!qaq) continue;
			C.clear();
			DFS_A(i,-1),DFS_B(i,-1);
//			printf("i,j:%lld %lld\n",i,j);
//			for (int i=1;i<=n;i++)
//				printf("%lld ",flag[i]);puts("");
			ans=min(ans,TP_sort()?tmp+qwq:INF);
		}
	}
	if(ans==INF)
		printf("-1\n");
	else
		printf("%lld\n",ans);
}

signed main()
{
	int T;
	scanf("%lld",&T);
	while(T--)
	{
		init();
		work();
	}
	return 0;
}

### Prompt

In Cpp, your task is to solve 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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#define int long long

using namespace std;

const int N=109,INF=1<<30;
int flag[N],head[N],cnt,deg[N],n;
struct Tree
{
	int head[N],cnt,F[N],x[N],y[N];
	struct Edge
	{
		int nxt,to;
	}g[N*2];
	
	void add(int from,int to)
	{
		g[++cnt].nxt=head[from];
		g[cnt].to=to;
		head[from]=cnt;
	}
	
	void clear()
	{
		cnt=0;
		memset(head,0,sizeof(head));
	}
	
	void dfs(int x,int fa)
	{
		for (int i=head[x];i;i=g[i].nxt)
		{
			int v=g[i].to;
			if(v==fa)
				continue;
			F[v]=x,dfs(v,x);
		}
	}
}A,B,C;

void DFS_A(int x,int fa)
{
	for (int i=A.head[x];i;i=A.g[i].nxt)
	{
		int v=A.g[i].to;
		if(v==fa)
			continue;
		if(flag[v]&&flag[x])
			C.add(v,x);
		DFS_A(v,x);
	}
}

void DFS_B(int x,int fa)
{
	for (int i=B.head[x];i;i=B.g[i].nxt)
	{
		int v=B.g[i].to;
		if(v==fa)
			continue;
		if(flag[x]&&flag[v])
			C.add(x,v);
		DFS_B(v,x);
	}
}

void init()
{
	B.clear();
	memset(deg,0,sizeof(deg));
	scanf("%lld",&n);
	for (int i=1;i<n;i++)
		scanf("%lld %lld",&A.x[i],&A.y[i]),deg[A.x[i]]++,deg[A.y[i]]++;
	for (int i=1,x,y;i<n;i++)
		scanf("%lld %lld",&x,&y),B.add(x,y),B.add(y,x);
}

bool TP_sort()
{
	int ans=n;
//	for (int i=1;i<=n;i++)
//		if(flag[i]) ans++;
	queue <int> q;
	static int Deg[N];
	memset(Deg,0,sizeof(Deg));
	for (int i=1;i<=n;i++)
		for (int j=C.head[i];j;j=C.g[j].nxt)
		{
			int v=C.g[j].to;
			Deg[v]++;
		}
//	puts("!!!");
//	for (int i=1;i<=n;i++)
//		for (int j=C.head[i];j;j=C.g[j].nxt)
//			printf("%lld %lld\n",i,C.g[j].to);
	for (int i=1;i<=n;i++)
		if(Deg[i]==0) ans--,q.push(i);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for (int i=C.head[x];i;i=C.g[i].nxt)
		{
			int v=C.g[i].to;
			Deg[v]--;
			if(Deg[v]==0)
				ans--,q.push(v);
		}
	}
//	printf("ans:%lld\n",ans);
	return !ans;
}

void work()
{
	int ans=INF;
	for (int i=1;i<=n;i++)
	{
		if(deg[i]!=1) continue;
		for (int j=1;j<=n;j++)
		{
			if(i==j) continue;
			int qwq=1;
			for (int k=1;k<n;k++)
				if((A.x[k]==i&&A.y[k]==j)||(A.x[k]==j&&A.y[k]==i))
				{
					qwq=0;
					break;
				}
			A.clear();
			for (int k=1;k<n;k++)
				if(A.x[k]!=i&&A.y[k]!=i)
					A.add(A.x[k],A.y[k]),A.add(A.y[k],A.x[k]);
			A.add(i,j),A.add(j,i);
			A.dfs(i,A.F[i]=-1),B.dfs(i,B.F[i]=-1);
			int tmp=0;
			for (int k=1;k<=n;k++)
				tmp+=(flag[k]=!(A.F[k]==B.F[k]));
			int qaq=1;
			for (int k=1;k<=n;k++)
				if(k!=i&&flag[B.F[k]]&&!flag[k])
				{
					qaq=0;
					break;
				}
			if(!qaq) continue;
			C.clear();
			DFS_A(i,-1),DFS_B(i,-1);
//			printf("i,j:%lld %lld\n",i,j);
//			for (int i=1;i<=n;i++)
//				printf("%lld ",flag[i]);puts("");
			ans=min(ans,TP_sort()?tmp+qwq:INF);
		}
	}
	if(ans==INF)
		printf("-1\n");
	else
		printf("%lld\n",ans);
}

signed main()
{
	int T;
	scanf("%lld",&T);
	while(T--)
	{
		init();
		work();
	}
	return 0;
}

```