#include <bits/stdc++.h>
using namespace std;
const int maxn = 50, inf = 1e9;
bool a[maxn + 10][maxn + 10], b[maxn + 10][maxn + 10];
int t, n, ans, fa[maxn + 10];
vector<int> g[maxn + 10];
int deg[maxn + 10];
bool vis[maxn + 10];

int dfssz(int p, int fa) {
	int ans = 1;
	for (int i = 1; i <= n; ++i)
		if (a[p][i] && i != fa)
			ans += dfssz(i, p);
	return ans;
}

int dfsc(int p, int fa) {
	int ans = 1; vis[p] = 1;
	for (int i = 1; i <= n; ++i)
		if (a[p][i] && b[p][i] && i != fa)
			ans += dfsc(i, p);
	return ans;
}

void dfsA(int p) {
	for (int i = 1; i <= n; ++i)
		if (a[p][i] && i != fa[p]) {
			fa[i] = p; dfsA(i);
		}
}

void dfsB(int p) {
	for (int i = 1; i <= n; ++i)
		if (b[p][i] && i != fa[p]) {
			fa[i] = p; dfsB(i);
		}
}

void init() {
	for (int i = 1; i <= n; ++i) {
		g[i].clear(); deg[i] = 0;
	}
}

void adde(int l, int r) {
	g[l].push_back(r); ++deg[r];
}

bool topo() {
	static queue<int> q;
	int cnt = 0;
	for (int i = 1; i <= n; ++i)
		if (!deg[i]) q.push(i);
	while (!q.empty()) {
		int p = q.front(); q.pop();
		++cnt;
		for (int i = 0; i < (int)g[p].size(); ++i) {
			int e = g[p][i];
			if (!--deg[e]) q.push(e);
		}
	}
	return cnt == n;
}

void chk(int x) {
	if (dfssz(x, 0) < n) return;
	memset(vis, 0, sizeof vis);
	int res = dfsc(x, 0);
	init();
	fa[x] = 0; dfsA(x);
	for (int i = 1; i <= n; ++i)
		if (!vis[i] && !vis[fa[i]]) adde(i, fa[i]);
	fa[x] = 0; dfsB(x);
	for (int i = 1; i <= n; ++i)
		if (!vis[i] && !vis[fa[i]]) adde(fa[i], i);
	if (topo()) ans = min(ans, n - res);
}

int main() {
	scanf("%d", &t);
	while (t--) {
		ans = inf;
		memset(a, 0, sizeof a);
		memset(b, 0, sizeof b);
		scanf("%d", &n);
		for (int i = 1; i < n; ++i) {
			int l, r; scanf("%d%d", &l, &r);
			a[l][r] = a[r][l] = 1;
		}
		for (int i = 1; i < n; ++i) {
			int l, r; scanf("%d%d", &l, &r);
			b[l][r] = b[r][l] = 1;
		}
		for (int i = 1; i <= n; ++i) {
			int to = 0;
			for (int j = 1; j <= n; ++j)
				if (a[i][j]) {
					if (!to) to = j;
					else to = -1;
				}
			if (to == -1) continue;
			a[i][to] = a[to][i] = 0;
			for (int j = 1; j <= n; ++j)
				if (j != i && j != to) {
					a[i][j] = a[j][i] = 1;
					chk(i);
					a[i][j] = a[j][i] = 0;
				}
			a[i][to] = a[to][i] = 1;
		}
		++ans;
		for (int i = 1; i <= n; ++i) chk(i);
		printf("%d\n", ans < inf ? ans : -1);
	}
}

### Prompt

Please provide a cpp coded solution to the problem described below:

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>
using namespace std;
const int maxn = 50, inf = 1e9;
bool a[maxn + 10][maxn + 10], b[maxn + 10][maxn + 10];
int t, n, ans, fa[maxn + 10];
vector<int> g[maxn + 10];
int deg[maxn + 10];
bool vis[maxn + 10];

int dfssz(int p, int fa) {
	int ans = 1;
	for (int i = 1; i <= n; ++i)
		if (a[p][i] && i != fa)
			ans += dfssz(i, p);
	return ans;
}

int dfsc(int p, int fa) {
	int ans = 1; vis[p] = 1;
	for (int i = 1; i <= n; ++i)
		if (a[p][i] && b[p][i] && i != fa)
			ans += dfsc(i, p);
	return ans;
}

void dfsA(int p) {
	for (int i = 1; i <= n; ++i)
		if (a[p][i] && i != fa[p]) {
			fa[i] = p; dfsA(i);
		}
}

void dfsB(int p) {
	for (int i = 1; i <= n; ++i)
		if (b[p][i] && i != fa[p]) {
			fa[i] = p; dfsB(i);
		}
}

void init() {
	for (int i = 1; i <= n; ++i) {
		g[i].clear(); deg[i] = 0;
	}
}

void adde(int l, int r) {
	g[l].push_back(r); ++deg[r];
}

bool topo() {
	static queue<int> q;
	int cnt = 0;
	for (int i = 1; i <= n; ++i)
		if (!deg[i]) q.push(i);
	while (!q.empty()) {
		int p = q.front(); q.pop();
		++cnt;
		for (int i = 0; i < (int)g[p].size(); ++i) {
			int e = g[p][i];
			if (!--deg[e]) q.push(e);
		}
	}
	return cnt == n;
}

void chk(int x) {
	if (dfssz(x, 0) < n) return;
	memset(vis, 0, sizeof vis);
	int res = dfsc(x, 0);
	init();
	fa[x] = 0; dfsA(x);
	for (int i = 1; i <= n; ++i)
		if (!vis[i] && !vis[fa[i]]) adde(i, fa[i]);
	fa[x] = 0; dfsB(x);
	for (int i = 1; i <= n; ++i)
		if (!vis[i] && !vis[fa[i]]) adde(fa[i], i);
	if (topo()) ans = min(ans, n - res);
}

int main() {
	scanf("%d", &t);
	while (t--) {
		ans = inf;
		memset(a, 0, sizeof a);
		memset(b, 0, sizeof b);
		scanf("%d", &n);
		for (int i = 1; i < n; ++i) {
			int l, r; scanf("%d%d", &l, &r);
			a[l][r] = a[r][l] = 1;
		}
		for (int i = 1; i < n; ++i) {
			int l, r; scanf("%d%d", &l, &r);
			b[l][r] = b[r][l] = 1;
		}
		for (int i = 1; i <= n; ++i) {
			int to = 0;
			for (int j = 1; j <= n; ++j)
				if (a[i][j]) {
					if (!to) to = j;
					else to = -1;
				}
			if (to == -1) continue;
			a[i][to] = a[to][i] = 0;
			for (int j = 1; j <= n; ++j)
				if (j != i && j != to) {
					a[i][j] = a[j][i] = 1;
					chk(i);
					a[i][j] = a[j][i] = 0;
				}
			a[i][to] = a[to][i] = 1;
		}
		++ans;
		for (int i = 1; i <= n; ++i) chk(i);
		printf("%d\n", ans < inf ? ans : -1);
	}
}

```