#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;


/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/


struct graph {
	int n;
	vector<pair<int, int>> ed0;
	vector<pair<int, int>> ed1;
	vector<int> eds2[55];
	vector<int> eds1[55];
	vector<int> eds[55];
	int en[55];
	int cc[55];
	int en2[55];
	void dfs1(int v) {
		en[v] = 1;
		for (int u: eds[v])
			if (!en[u])
				dfs1(u);
	}
	int get(int x) {
		set<pair<int, int>> ss;
		fill(cc, cc + n, 0);
		for (int i = 0; i < ed0.size(); ++i) {
			if (ed0[i].first > ed0[i].second)
				swap(ed0[i].first, ed0[i].second);
			ss.insert(ed0[i]);
			int a = ed0[i].first;
			int b = ed0[i].second;
			++cc[a], ++cc[b];
			eds1[a].push_back(b);
			eds1[b].push_back(a);
		}
		for (int i = 0; i < ed0.size(); ++i) {
			if (ed1[i].first > ed1[i].second)
				swap(ed1[i].first, ed1[i].second);
			if (ss.count(ed1[i])) {
				int a = ed1[i].first;
				int b = ed1[i].second;
				eds[a].push_back(b);
				eds[b].push_back(a);
			}
			int a = ed1[i].first;
			int b = ed1[i].second;
			eds2[a].push_back(b);
			eds2[b].push_back(a);
		}
		fill(en, en + n, 0);
		fill(en2, en2 + n, 0);
		dfs1(x);
		int lf = n;
		for (int i = 0; i < n; ++i) {
			lf -= en[i];
			if (en[i]) {
				for (int u: eds2[i])
					en2[u] = 1;
			}
		}
		int ans = 0;
		while (lf) {
			int fl = 0;
			for (int i = 0; i < n; ++i)
				if (cc[i] == 1 && !en[i] && en2[i]) {
					--lf;
					++ans;
					en[i] = 1;
					fl = 1;
					for (int u: eds2[i])
						en2[u] = 1;
					for (int u: eds1[i])
						if (!en[u])
							--cc[u];
				}
			if (!fl)
				return n + 1;
		}
		return ans;
	}
};
int cc0[100], cc1[100];


void solve() {
	graph now;
	int n;
	cin >> n;
	now.n = n;
	for (int i = 0; i < n; ++i)
		cc0[i] = cc1[i] = 0;
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		++cc0[a];
		++cc0[b];
		now.ed0.emplace_back(a, b);
	}
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		++cc1[a], ++cc1[b];
		now.ed1.emplace_back(a, b);
	}
	int ans = n + 1;
	for (int i = 0; i < n; ++i) {
		graph tmp = now;
		ans = min(ans, tmp.get(i));
		if (cc0[i] == 1) {
			int k = 0;
			while (now.ed0[k].first != i && now.ed0[k].second != i)
				++k;
			for (int j = 0; j < n; ++j) 
				if (j != i) {
					tmp = now;
					tmp.ed0[k] = {i, j};
					ans = min(ans, tmp.get(i) + 1);
				}
		}
	}
	if (ans == n + 1)
		cout << -1 << "\n";
	else
		cout << ans << "\n";
}

int main() {
#ifdef FASTIO
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
	int tt;
	cin >> tt;
	for (int i = 0; i < tt; ++i)
		solve();
	return 0;
}



### Prompt

Your task is to create 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
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;


/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/


struct graph {
	int n;
	vector<pair<int, int>> ed0;
	vector<pair<int, int>> ed1;
	vector<int> eds2[55];
	vector<int> eds1[55];
	vector<int> eds[55];
	int en[55];
	int cc[55];
	int en2[55];
	void dfs1(int v) {
		en[v] = 1;
		for (int u: eds[v])
			if (!en[u])
				dfs1(u);
	}
	int get(int x) {
		set<pair<int, int>> ss;
		fill(cc, cc + n, 0);
		for (int i = 0; i < ed0.size(); ++i) {
			if (ed0[i].first > ed0[i].second)
				swap(ed0[i].first, ed0[i].second);
			ss.insert(ed0[i]);
			int a = ed0[i].first;
			int b = ed0[i].second;
			++cc[a], ++cc[b];
			eds1[a].push_back(b);
			eds1[b].push_back(a);
		}
		for (int i = 0; i < ed0.size(); ++i) {
			if (ed1[i].first > ed1[i].second)
				swap(ed1[i].first, ed1[i].second);
			if (ss.count(ed1[i])) {
				int a = ed1[i].first;
				int b = ed1[i].second;
				eds[a].push_back(b);
				eds[b].push_back(a);
			}
			int a = ed1[i].first;
			int b = ed1[i].second;
			eds2[a].push_back(b);
			eds2[b].push_back(a);
		}
		fill(en, en + n, 0);
		fill(en2, en2 + n, 0);
		dfs1(x);
		int lf = n;
		for (int i = 0; i < n; ++i) {
			lf -= en[i];
			if (en[i]) {
				for (int u: eds2[i])
					en2[u] = 1;
			}
		}
		int ans = 0;
		while (lf) {
			int fl = 0;
			for (int i = 0; i < n; ++i)
				if (cc[i] == 1 && !en[i] && en2[i]) {
					--lf;
					++ans;
					en[i] = 1;
					fl = 1;
					for (int u: eds2[i])
						en2[u] = 1;
					for (int u: eds1[i])
						if (!en[u])
							--cc[u];
				}
			if (!fl)
				return n + 1;
		}
		return ans;
	}
};
int cc0[100], cc1[100];


void solve() {
	graph now;
	int n;
	cin >> n;
	now.n = n;
	for (int i = 0; i < n; ++i)
		cc0[i] = cc1[i] = 0;
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		++cc0[a];
		++cc0[b];
		now.ed0.emplace_back(a, b);
	}
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		cin >> a >> b;
		--a, --b;
		++cc1[a], ++cc1[b];
		now.ed1.emplace_back(a, b);
	}
	int ans = n + 1;
	for (int i = 0; i < n; ++i) {
		graph tmp = now;
		ans = min(ans, tmp.get(i));
		if (cc0[i] == 1) {
			int k = 0;
			while (now.ed0[k].first != i && now.ed0[k].second != i)
				++k;
			for (int j = 0; j < n; ++j) 
				if (j != i) {
					tmp = now;
					tmp.ed0[k] = {i, j};
					ans = min(ans, tmp.get(i) + 1);
				}
		}
	}
	if (ans == n + 1)
		cout << -1 << "\n";
	else
		cout << ans << "\n";
}

int main() {
#ifdef FASTIO
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
	int tt;
	cin >> tt;
	for (int i = 0; i < tt; ++i)
		solve();
	return 0;
}



```