/*
    _____
  .'     '.
 /  0   0  \
|     ^     |
|  \     /  |
 \  '---'  /
  '._____.'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

struct chash
{
	int operator()(int x) const
	{
		x ^= (x >> 20) ^ (x >> 12);
		return x ^ (x >> 7) ^ (x >> 4);
	}
};

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using hashtable = gp_hash_table<T, U, chash>;
random_device(rd);
mt19937 rng(rd());

template<class T>
void readi(T &x)
{
	T input = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		input = input * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		input = -input;
	}
	x = input;
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int aout[20];
	int ilen = 0;
	while(output)
	{
		aout[ilen] = ((output % 10));
		output /= 10;
		ilen++;
	}
	for (int i = ilen - 1; i >= 0; i--)
	{
		putchar(aout[i] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
template<class T, class U>
T nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-10;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 53

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

int TC;
int N;
set<int> edge[2][MAXN];
int parent[2][MAXN];
int ans;
set<int> eds[MAXN];
int disc[MAXN], low[MAXN];
int T, K;
vector<int> scc[MAXN], nodes;
bitset<MAXN> vis, friendly;

void dfs(bool f, int u)
{
	for (int v : edge[f][u])
	{
		if (v == parent[f][u])
		{
			continue;
		}
		parent[f][v] = u;
		dfs(f, v);
	}
	return;
}
void work(int u)
{
	for (int v : edge[0][u])
	{
		if (v == parent[0][u]) continue;
		if (parent[1][v] == u)
		{
			friendly[v] = true;
			work(v);
		}
	}
}
void dfs(int u)
{
	disc[u] = low[u] = T; vis[u] = true;
	T++;
	nodes.PB(u);
	for (int v : eds[u])
	{
		if (vis[v])
		{
			ckmin(low[u], low[v]);
			continue;
		}
		dfs(v);
	}
	if (low[u] == disc[u])
	{
		while(nodes.back() != u)
		{
			int v = nodes.back();
			nodes.pop_back();
			scc[K].PB(v);
		}
		nodes.pop_back();
		scc[K].PB(u);
		low[u] = INF;
		K++;
	}
	return;
}
int solve(int u)
{
	//you know u cannot move!
	int res = 0;
	T = 0; K = 0;
	for (int i = 0; i < N; i++)
	{
		parent[0][i] = N; parent[1][i] = N;
		eds[i].clear(); scc[i].clear();
		friendly[i] = false; vis[i] = false;
		disc[i] = 0; low[i] = 0;
	}
	dfs(0, u);
	dfs(1, u);
	friendly[u] = true;
	work(u);
	for (int i = 0; i < N; i++)
	{
		if (friendly[i]) continue;
		res++;
		if (!friendly[parent[0][i]])
		eds[i].insert(parent[0][i]);
		if (!friendly[parent[1][i]])
		eds[parent[1][i]].insert(i);
	}
	// cerr << "solve " << u << endl;
	// for (int i = 0; i < N; i++)
	// {
	// 	for (int v : eds[i])
	// 	{
	// 		cerr << i << "->" << v << endl;
	// 	}
	// }
	for (int i = 0; i < N; i++)
	{
		if (!vis[i]) dfs(i);
	}
	//now we check for a cycle!
	for (int i = 0; i < K; i++)
	{
		if (scc[i].size() > 1)
		{
			return INF;
		}
	}
	// cerr << "works!\n";
	return res;
	//u is stuck!
	//first find all the guys who don't need to move:
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	// cout << fixed << setprecision(10);
	// cerr << fixed << setprecision(10);
	// freopen ("file.in", "r", stdin);
	// freopen ("file.out", "w", stdout);
	cin >> TC;
	while(TC--)
	{
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			edge[0][i].clear();
			edge[1][i].clear();
		}
		ans = INF;
		for (int i = 1; i < N; i++)
		{
			int u, v;
			cin >> u >> v;
			u--; v--;
			edge[0][u].insert(v);
			edge[0][v].insert(u);
		}
		for (int i = 1; i < N; i++)
		{
			int u, v;
			cin >> u >> v;
			u--; v--;
			edge[1][u].insert(v);
			edge[1][v].insert(u);
		}
		for (int i = 0; i < N; i++)
		{
			ckmin(ans, solve(i));
		}
		for (int i = 0; i < N; i++)
		{
			auto blah = edge[0][i];
			for (int v : blah)
			{
				if (edge[0][v].size() > 1) continue;
				edge[0][v].erase(i);
				edge[0][i].erase(v);
				for (int j = 0; j < N; j++)
				{
					if (j == i || j == v) continue;\
					edge[0][v].insert(j);
					edge[0][j].insert(v);
					ckmin(ans, 1 + solve(v));
					edge[0][v].erase(j);
					edge[0][j].erase(v);
				}
				edge[0][v].insert(i);
				edge[0][i].insert(v);
			}
		}
		cout << (ans == INF ? -1 : ans) << '\n';
		//ok lol let's see
		//what the fuck.
		//once you know a fixed node, everything else is forced
	}
	// cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}


Measured memory_bytes: 900