#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
typedef pair<int, int>pii;
vector<int>pat[2][55];
bool flag[55];
set<pii>se[2];
vector<int>zp[55];
int cnt;
void dfs(int node, int t, bool f)
{
	flag[node] = true;
	for (int i = 0; i < pat[t][node].size(); i++)
	{
		int v = pat[t][node][i];
		if (!flag[v])
		{
			if (f&&se[1 - t].find(make_pair(node, v)) != se[1 - t].end())dfs(v, t, true);
			else
			{
				dfs(v, t, false);
				if (t == 0)cnt++;
				if (!f)
				{
					if (t == 0)zp[v].push_back(node);
					else zp[node].push_back(v);
				}
			}
		}
	}
}
int fin[55];
int pt;
void dfs2(int node)
{
	if (flag[node])return;
	flag[node] = true;
	for (int i = 0; i < zp[node].size(); i++)dfs2(zp[node][i]);
	fin[node] = pt++;
}
int get(int num, vector<pii>ea, vector<pii>eb, int r)
{
	for (int i = 0; i < num; i++)pat[0][i].clear(), pat[1][i].clear(), zp[i].clear();
	se[0].clear(), se[1].clear();
	for (int i = 0; i < ea.size(); i++)pat[0][ea[i].first].push_back(ea[i].second), pat[0][ea[i].second].push_back(ea[i].first);
	for (int i = 0; i < eb.size(); i++)pat[1][eb[i].first].push_back(eb[i].second), pat[1][eb[i].second].push_back(eb[i].first);
	for (int i = 0; i < ea.size(); i++)se[0].insert(ea[i]), se[0].insert(make_pair(ea[i].second, ea[i].first));
	for (int i = 0; i < eb.size(); i++)se[1].insert(eb[i]), se[1].insert(make_pair(eb[i].second, eb[i].first));
	cnt = 0;
	fill(flag, flag + num, false);
	dfs(r, 0, true);
	fill(flag, flag + num, false);
	dfs(r, 1, true);
	pt = 0;
	fill(flag, flag + num, false);
	for (int i = 0; i < num; i++)dfs2(i);
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < zp[i].size(); j++)
		{
			if (fin[i] < fin[zp[i][j]])return 1000000000;
		}
	}
	return cnt;
}
int deg[55];
int main()
{
	int data;
	scanf("%d", &data);
	for (int p = 0; p < data; p++)
	{
		int num;
		scanf("%d", &num);
		fill(deg, deg + num, 0);
		vector<pii>v1, v2;
		for (int i = 0; i < num - 1; i++)
		{
			int za, zb;
			scanf("%d%d", &za, &zb);
			za--, zb--;
			v1.push_back(make_pair(za, zb));
		}
		for (int i = 0; i < num - 1; i++)
		{
			int za, zb;
			scanf("%d%d", &za, &zb);
			za--, zb--;
			v2.push_back(make_pair(za, zb));
			deg[za]++, deg[zb]++;
		}
		int mini = 1000000000;
		for (int i = 0; i < num; i++)
		{
			int z = get(num, v1, v2, i);
			mini = min(mini, z);
			//printf("   %d %d\n", i + 1, z);
		}
		for (int i = 0; i < num; i++)
		{
			if (deg[i] != 1)continue;
			for (int j = 0; j < num; j++)
			{
				if (i == j)continue;
				vector<pii>zv;
				for (int k = 0; k < v2.size(); k++)
				{
					if (v2[k].first != i&&v2[k].second != i)zv.push_back(v2[k]);
					else zv.push_back(make_pair(i, j));
				}
				int z = get(num, v1, zv, i);
				mini = min(mini, z + 1);
				//printf("   %d %d %d\n", i + 1, j + 1, z);
			}
		}
		if (mini == 1000000000)printf("-1\n");
		else printf("%d\n", mini);
	}
}
### Prompt

Your challenge is to write 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<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
typedef pair<int, int>pii;
vector<int>pat[2][55];
bool flag[55];
set<pii>se[2];
vector<int>zp[55];
int cnt;
void dfs(int node, int t, bool f)
{
	flag[node] = true;
	for (int i = 0; i < pat[t][node].size(); i++)
	{
		int v = pat[t][node][i];
		if (!flag[v])
		{
			if (f&&se[1 - t].find(make_pair(node, v)) != se[1 - t].end())dfs(v, t, true);
			else
			{
				dfs(v, t, false);
				if (t == 0)cnt++;
				if (!f)
				{
					if (t == 0)zp[v].push_back(node);
					else zp[node].push_back(v);
				}
			}
		}
	}
}
int fin[55];
int pt;
void dfs2(int node)
{
	if (flag[node])return;
	flag[node] = true;
	for (int i = 0; i < zp[node].size(); i++)dfs2(zp[node][i]);
	fin[node] = pt++;
}
int get(int num, vector<pii>ea, vector<pii>eb, int r)
{
	for (int i = 0; i < num; i++)pat[0][i].clear(), pat[1][i].clear(), zp[i].clear();
	se[0].clear(), se[1].clear();
	for (int i = 0; i < ea.size(); i++)pat[0][ea[i].first].push_back(ea[i].second), pat[0][ea[i].second].push_back(ea[i].first);
	for (int i = 0; i < eb.size(); i++)pat[1][eb[i].first].push_back(eb[i].second), pat[1][eb[i].second].push_back(eb[i].first);
	for (int i = 0; i < ea.size(); i++)se[0].insert(ea[i]), se[0].insert(make_pair(ea[i].second, ea[i].first));
	for (int i = 0; i < eb.size(); i++)se[1].insert(eb[i]), se[1].insert(make_pair(eb[i].second, eb[i].first));
	cnt = 0;
	fill(flag, flag + num, false);
	dfs(r, 0, true);
	fill(flag, flag + num, false);
	dfs(r, 1, true);
	pt = 0;
	fill(flag, flag + num, false);
	for (int i = 0; i < num; i++)dfs2(i);
	for (int i = 0; i < num; i++)
	{
		for (int j = 0; j < zp[i].size(); j++)
		{
			if (fin[i] < fin[zp[i][j]])return 1000000000;
		}
	}
	return cnt;
}
int deg[55];
int main()
{
	int data;
	scanf("%d", &data);
	for (int p = 0; p < data; p++)
	{
		int num;
		scanf("%d", &num);
		fill(deg, deg + num, 0);
		vector<pii>v1, v2;
		for (int i = 0; i < num - 1; i++)
		{
			int za, zb;
			scanf("%d%d", &za, &zb);
			za--, zb--;
			v1.push_back(make_pair(za, zb));
		}
		for (int i = 0; i < num - 1; i++)
		{
			int za, zb;
			scanf("%d%d", &za, &zb);
			za--, zb--;
			v2.push_back(make_pair(za, zb));
			deg[za]++, deg[zb]++;
		}
		int mini = 1000000000;
		for (int i = 0; i < num; i++)
		{
			int z = get(num, v1, v2, i);
			mini = min(mini, z);
			//printf("   %d %d\n", i + 1, z);
		}
		for (int i = 0; i < num; i++)
		{
			if (deg[i] != 1)continue;
			for (int j = 0; j < num; j++)
			{
				if (i == j)continue;
				vector<pii>zv;
				for (int k = 0; k < v2.size(); k++)
				{
					if (v2[k].first != i&&v2[k].second != i)zv.push_back(v2[k]);
					else zv.push_back(make_pair(i, j));
				}
				int z = get(num, v1, zv, i);
				mini = min(mini, z + 1);
				//printf("   %d %d %d\n", i + 1, j + 1, z);
			}
		}
		if (mini == 1000000000)printf("-1\n");
		else printf("%d\n", mini);
	}
}
```