#include <bits/stdc++.h>

const int MAXN = 60;
std::vector<int> A[MAXN], B[MAXN], C[MAXN], D[MAXN];
int fa[MAXN], n;
void dfs(int u) { for (int i : A[u]) if (i != fa[u]) fa[i] = u, dfs(i); }
int mark[MAXN];
void dfs1(int u, int fa = 0) {
	for (int i : C[u]) if (i != fa)
		mark[u] || mark[i] ? 0 : (D[i].push_back(u), 0), dfs1(i, u);
}
void dfs2(int u, int fa = 0) {
	for (int i : B[u]) if (i != fa)
		mark[u] || mark[i] ? 0 : (D[u].push_back(i), 0), dfs2(i, u);
}
inline bool lb(const std::vector<int> & x, int v) {
	auto at = std::lower_bound(x.begin(), x.end(), v);
	return at != x.end() && *at == v;
}
void dfs3(int u, int fa = 0) {
	mark[u] = true; for (int i : B[u]) if (lb(C[u], i) && i != fa) dfs3(i, u);
}
int ind[MAXN];
std::queue<int> q;
int solve(int rt) {
	for (int i = 1; i <= n; ++i) C[i].clear(), D[i].clear();
	std::fill(ind + 1, ind + 1 + n, 0);
	std::fill(mark + 1, mark + 1 + n, 0);
	for (int i = 1; i <= n; ++i) if (fa[i]) {
		C[fa[i]].push_back(i);
		C[i].push_back(fa[i]);
	}
	for (int i = 1; i <= n; ++i) std::sort(C[i].begin(), C[i].end());
	dfs3(rt); dfs1(rt); dfs2(rt);
	for (int i = 1; i <= n; ++i) for (auto j : D[i]) ++ind[j];
	for (int i = 1; i <= n; ++i) if (ind[i] == 0) q.push(i);
	while (!q.empty()) {
		int t = q.front(); q.pop();
		for (auto i : D[t]) if (!--ind[i]) q.push(i);
	}
	int res = n - std::accumulate(mark + 1, mark + 1 + n, 0);
	return *std::max_element(ind + 1, ind + 1 + n) ? n + 1 : res;
}
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	int T; std::cin >> T;
	while (T --> 0) {
		std::cin >> n;
		for (int i = 1, t1, t2; i < n; ++i) {
			std::cin >> t1 >> t2;
			A[t1].push_back(t2);
			A[t2].push_back(t1);
		}
		for (int i = 1, t1, t2; i < n; ++i) {
			std::cin >> t1 >> t2;
			B[t1].push_back(t2);
			B[t2].push_back(t1);
		}
		for (int i = 1; i <= n; ++i) std::sort(A[i].begin(), A[i].end());
		for (int i = 1; i <= n; ++i) std::sort(B[i].begin(), B[i].end());
		bool can = true;
		for (int i = 1; i <= n; ++i) can &= A[i] == B[i];
		int ans = n + 1;
		if (can) ans = 0;
		else {
			int rt = 0;
			for (int i = 1; i <= n; ++i) if (A[i].size() > 1) rt = i;
			assert(rt);
			fa[rt] = 0; dfs(rt);
			for (int i = 1; i <= n; ++i) 
				ans = std::min(ans, solve(i));
			for (int i = 1; i <= n; ++i) if (A[i].size() == 1) {
				int s = fa[i];
				for (int j = 1; j <= n; ++j) if (j != i)
					fa[i] = j, ans = std::min(ans, solve(i) + 1);
				fa[i] = s;
			}
		}
		std::cout << (ans == n + 1 ? -1 : ans) << '\n';
		for (int i = 1; i <= n; ++i) A[i].clear(), B[i].clear();
	}
	return 0;
}

### 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 <bits/stdc++.h>

const int MAXN = 60;
std::vector<int> A[MAXN], B[MAXN], C[MAXN], D[MAXN];
int fa[MAXN], n;
void dfs(int u) { for (int i : A[u]) if (i != fa[u]) fa[i] = u, dfs(i); }
int mark[MAXN];
void dfs1(int u, int fa = 0) {
	for (int i : C[u]) if (i != fa)
		mark[u] || mark[i] ? 0 : (D[i].push_back(u), 0), dfs1(i, u);
}
void dfs2(int u, int fa = 0) {
	for (int i : B[u]) if (i != fa)
		mark[u] || mark[i] ? 0 : (D[u].push_back(i), 0), dfs2(i, u);
}
inline bool lb(const std::vector<int> & x, int v) {
	auto at = std::lower_bound(x.begin(), x.end(), v);
	return at != x.end() && *at == v;
}
void dfs3(int u, int fa = 0) {
	mark[u] = true; for (int i : B[u]) if (lb(C[u], i) && i != fa) dfs3(i, u);
}
int ind[MAXN];
std::queue<int> q;
int solve(int rt) {
	for (int i = 1; i <= n; ++i) C[i].clear(), D[i].clear();
	std::fill(ind + 1, ind + 1 + n, 0);
	std::fill(mark + 1, mark + 1 + n, 0);
	for (int i = 1; i <= n; ++i) if (fa[i]) {
		C[fa[i]].push_back(i);
		C[i].push_back(fa[i]);
	}
	for (int i = 1; i <= n; ++i) std::sort(C[i].begin(), C[i].end());
	dfs3(rt); dfs1(rt); dfs2(rt);
	for (int i = 1; i <= n; ++i) for (auto j : D[i]) ++ind[j];
	for (int i = 1; i <= n; ++i) if (ind[i] == 0) q.push(i);
	while (!q.empty()) {
		int t = q.front(); q.pop();
		for (auto i : D[t]) if (!--ind[i]) q.push(i);
	}
	int res = n - std::accumulate(mark + 1, mark + 1 + n, 0);
	return *std::max_element(ind + 1, ind + 1 + n) ? n + 1 : res;
}
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	int T; std::cin >> T;
	while (T --> 0) {
		std::cin >> n;
		for (int i = 1, t1, t2; i < n; ++i) {
			std::cin >> t1 >> t2;
			A[t1].push_back(t2);
			A[t2].push_back(t1);
		}
		for (int i = 1, t1, t2; i < n; ++i) {
			std::cin >> t1 >> t2;
			B[t1].push_back(t2);
			B[t2].push_back(t1);
		}
		for (int i = 1; i <= n; ++i) std::sort(A[i].begin(), A[i].end());
		for (int i = 1; i <= n; ++i) std::sort(B[i].begin(), B[i].end());
		bool can = true;
		for (int i = 1; i <= n; ++i) can &= A[i] == B[i];
		int ans = n + 1;
		if (can) ans = 0;
		else {
			int rt = 0;
			for (int i = 1; i <= n; ++i) if (A[i].size() > 1) rt = i;
			assert(rt);
			fa[rt] = 0; dfs(rt);
			for (int i = 1; i <= n; ++i) 
				ans = std::min(ans, solve(i));
			for (int i = 1; i <= n; ++i) if (A[i].size() == 1) {
				int s = fa[i];
				for (int j = 1; j <= n; ++j) if (j != i)
					fa[i] = j, ans = std::min(ans, solve(i) + 1);
				fa[i] = s;
			}
		}
		std::cout << (ans == n + 1 ? -1 : ans) << '\n';
		for (int i = 1; i <= n; ++i) A[i].clear(), B[i].clear();
	}
	return 0;
}

```