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

Solution (in CPP):

Solution:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 55;
int n;
int ANS;
int edA[N][2], edB[N][2];
int parA[N];
vector<int> gA[N];
vector<int> gB[N];
vector<int> sonsA[N], sonsB[N];
bool good[N][3];
bool GB[N][N];

void dfsA(int v, int p) {
	for (int u : gA[v]) {
		if (u == p) continue;
		sonsA[v].push_back(u);
		dfsA(u, v);
	}
}

void dfsB(int v, int p) {
	for (int u : gB[v]) {
		if (u == p) continue;
		sonsB[v].push_back(u);
		dfsB(u, v);
	}
}

void solve(int V, int curAns) {
	for (int i = 0; i < n; i++) {
		gA[i].clear();
		gB[i].clear();
		sonsA[i].clear();
		sonsB[i].clear();
		good[i][0] = good[i][1] = good[i][2] = false;
	}
	for (int i = 1; i < n; i++) {
		gA[edA[i][0]].push_back(edA[i][1]);
		gA[edA[i][1]].push_back(edA[i][0]);
		gB[edB[i][0]].push_back(edB[i][1]);
		gB[edB[i][1]].push_back(edB[i][0]);
	}
	dfsA(V, -1);
	dfsB(V, -1);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			GB[i][j] = 0;
	for (int i = 0; i < n; i++)
		for (int j : sonsB[i])
			GB[i][j] = 1;
	good[V][0] = 1;
	vector<int> q;
	q.push_back(V);
	for (int i = 0; i < (int)q.size(); i++) {
		int v = q[i];
		for (int u : sonsB[v])
			good[u][2] = 1;
		for (int u : sonsA[v]) {
			if (GB[v][u]) {
				good[u][0] = 1;
				q.push_back(u);
			}
		}
	}
	q.clear();
	int deg[N];
	for (int v = 0; v < n; v++)
		for (int u : sonsA[v])
			parA[u] = v;
	for (int v = 0; v < n; v++) {
		deg[v] = (int)sonsA[v].size();
		good[v][1] = deg[v] == 0;
	}
	for (int v = 0; v < n; v++)
		if (!good[v][0] && good[v][1] && good[v][2])
			q.push_back(v);
	for (int i = 0; i < (int)q.size(); i++) {
		int v = q[i];
		good[v][0] = true;
		curAns++;
		int u = parA[v];
		deg[u]--;
		if (deg[u] == 0) {
			good[u][1] = 1;
			if (!good[u][0] && good[u][1] && good[u][2])
				q.push_back(u);
		}
		for (int w : sonsB[v]) {
			good[w][2] = 1;
			if (!good[w][0] && good[w][1] && good[w][2])
				q.push_back(w);
		}
	}
	for (int v = 0; v < n; v++)
		if (!good[v][0])
			return;
	ANS = min(ANS, curAns);
}

void solve() {
	scanf("%d", &n);
	for (int i = 1; i < n; i++)
		for (int j = 0; j < 2; j++) {
			scanf("%d", &edA[i][j]);
			edA[i][j]--;
		}
	for (int i = 1; i < n; i++)
		for (int j = 0; j < 2; j++) {
			scanf("%d", &edB[i][j]);
			edB[i][j]--;
		}
	ANS = N;
	int deg[N];
	for (int i = 0; i < n; i++)
		deg[i] = 0;
	for (int i = 1; i < n; i++)
		for (int j = 0; j < 2; j++)
			deg[edA[i][j]]++;
	for (int v = 0; v < n; v++) {
		solve(v, 0);
		if (deg[v] != 1) continue;
		int id = 1;
		while(edA[id][0] != v && edA[id][1] != v) id++;
		int k = 0;
		if (edA[id][k] == v) k = 1;
		int old = edA[id][k];
		for (int u = 0; u < n; u++) {
			if (u == v || u == old) continue;
			edA[id][k] = u;
			solve(v, 1);
		}
		edA[id][k] = old;
	}
	if (ANS == N) ANS = -1;
	printf("%d\n", ANS);
}

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int t;
	scanf("%d", &t);
	while(t--) solve();

	return 0;
}