Problem:
Score : 1900 points

Problem Statement

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.

Sample Input 1

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

Sample Output 1

1
-1

The graph in each case is shown below.

In case 1, A can be made to coincide with B by choosing Vertex 1, removing the edge connecting 1 and 2, and adding an edge connecting 1 and 3. Note that Vertex 2 is not a leaf vertex and thus cannot be chosen.

Sample Input 2

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

Sample Output 2

6
0
7

A may coincide with B from the beginning.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int T,n,f1[51],f2[51],d1[51],d2[51],ans,d[51];
bool vis[51],cur[51];
struct tree
{
	bool mp[51][51];
	struct bian{int nxt,to;}bi[110];
	int head[51],num;
	inline void add(int from,int to)
	{
		mp[from][to]=1;
		bi[++num]=bian{head[from],to};
		head[from]=num;
	}
	void clear()
	{
		memset(mp,0,sizeof(mp));
		memset(head,0,sizeof(head));
		num=0;
	}
}t1,t2;
void dfs1(int v,int fa)
{
	f1[v]=fa;
	for(int i=t1.head[v];i;i=t1.bi[i].nxt)
		if(t1.bi[i].to!=fa)++d1[v],dfs1(t1.bi[i].to,v);
}
void dfs2(int v,int fa)
{
	f2[v]=fa;
	for(int i=t2.head[v];i;i=t2.bi[i].nxt)
		if(t2.bi[i].to!=fa)++d2[v],dfs2(t2.bi[i].to,v);
}
void work(int x)
{
	queue<int>q;
	int y,cnt=0;
	memset(cur+1,0,n);
	q.push(x),vis[x]=1,cur[x]=1,++cnt;
	while(!q.empty())
	{
		x=q.front();q.pop();//printf("%d ",x);
		for(int i=t1.head[x];i;i=t1.bi[i].nxt)if(t2.mp[x][y=t1.bi[i].to]&&!vis[y])q.push(y),cur[y]=1,vis[y]=1,++cnt;
	}
//	printf("\n");
	memset(f1+1,0,n<<2);memset(d1+1,0,n<<2);
	memset(f2+1,0,n<<2);memset(d2+1,0,n<<2);
	for(int i=1;i<=n;++i)
		if(cur[i])
		{
			for(int j=t1.head[i];j;j=t1.bi[j].nxt)if(!cur[y=t1.bi[j].to])dfs1(y,i);
			for(int j=t2.head[i];j;j=t2.bi[j].nxt)if(!cur[y=t2.bi[j].to])dfs2(y,i);
		}
	int tot=cnt;
	for(int i=1;i<=n;++i)if(!cur[i]&&!d1[i]&&cur[f2[i]])q.push(i);
	while(!q.empty())
	{
		x=q.front();q.pop();
	//	printf("%d ",x);
		--d1[f1[x]],cur[x]=1,++tot;
		y=f1[x];
		if(!cur[y]&&!d1[y]&&cur[f2[y]])q.push(y);
		for(int i=t2.head[x];i;i=t2.bi[i].nxt)
		{
			y=t2.bi[i].to;
			if(f2[y]==x&&!cur[y]&&!d1[y])q.push(y);
		}
	}
//	printf("\n");
//	printf("%d\n\n",tot);
	if(tot==n)ans=min(ans,n-cnt);
}
bool work1(int x,int y)
{
	queue<int>q;
	memset(f1+1,0,n<<2);memset(d1+1,0,n<<2);
	memset(f2+1,0,n<<2);memset(d2+1,0,n<<2);
	memset(cur+1,0,n);
	dfs1(y,0);
	dfs2(x,0);
	--d1[f1[x]];
	int tot=0;
	cur[x]=1;
	for(int i=1;i<=n;++i)if(!cur[i]&&!d1[i]&&cur[f2[i]])q.push(i);
	while(!q.empty())
	{
		x=q.front();q.pop();
		--d1[f1[x]],cur[x]=1,++tot;
		y=f1[x];
		if(!cur[y]&&!d1[y]&&cur[f2[y]])q.push(y);
		for(int i=t2.head[x];i;i=t2.bi[i].nxt)
		{
			y=t2.bi[i].to;
			if(f2[y]==x&&!cur[y]&&!d1[y])q.push(y);
		}
	}
	return tot==n;	
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		memset(vis+1,0,n);memset(d+1,0,n<<2);
		t1.clear(),t2.clear();
		ans=0x3f3f3f3f;
		int x,y;
		for(int i=1;i<n;++i)
		{
			scanf("%d %d",&x,&y);
			t1.add(x,y),t1.add(y,x);
			++d[x],++d[y];
		}
		for(int i=1;i<n;++i)
		{
			scanf("%d %d",&x,&y);
			t2.add(x,y),t2.add(y,x);
		}
		for(int i=1;i<=n;++i)if(!vis[i])work(i);
		if(ans!=0x3f3f3f3f)
		{
			printf("%d\n",ans);
			continue;
		}
		bool flag=0;
		for(int i=1;i<=n&&!flag;++i)
			if(d[i]==1)
				for(int j=1;j<=n&&!flag;++j)
					if(j!=i&&work1(i,j))flag=1;
		if(flag)printf("%d\n",n);
		else printf("-1\n");
	}
	return 0;
}