// Tenshi ni Fureta yo!

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int) (u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status()
{
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

namespace fastIO
{
	const UI BUFFER_SIZE = (UI) 1 << 15;
	char buffer[BUFFER_SIZE], *cur, *ed;

	inline char getc() { return *(cur == ed ? ed = buffer + fread(buffer, 1, BUFFER_SIZE, stdin), cur = buffer : cur)++; }
}
using fastIO::getc;

template<typename T> inline T read()
{
	register T sum(0), fg(1);
	register char ch(getc());
	for(; !isdigit(ch); ch = getc()) if(ch == '-') fg = -1;
	for(;  isdigit(ch); ch = getc()) sum = sum * 10 - '0' + ch;
	return sum * fg;
}

template<typename T> inline T sqr(const T &val) { return val * val; }
template<typename T> inline int chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline int chkmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

// END tpl.

const int MAXN = 50;

int n;

struct TREE
{
	vector<int> adj[MAXN + 5];

	int fa[MAXN + 5], dep[MAXN + 5];
	bitset<MAXN + 5> sub[MAXN + 5];

	inline void build(int N) { for(int i = 1; i <= N; ++i) adj[i].clear(); }
}G, T;

inline void input()
{
	n = read<int>();
	G.build(n);
	for(int i = 1; i < n; ++i)
	{
		int u = read<int>(), v = read<int>();
		G.adj[u].push_back(v);
		G.adj[v].push_back(u);
	}
	T.build(n);
	for(int i = 1; i < n; ++i)
	{
		int u = read<int>(), v = read<int>();
		T.adj[u].push_back(v);
		T.adj[v].push_back(u);
	}
}

inline void dfs(int u, int fa, TREE &t)
{
	t.dep[u] = t.dep[t.fa[u] = fa] + 1;
	t.sub[u].reset(), t.sub[u].set(u);
	for(auto v : t.adj[u]) if(v != fa)
	{
		dfs(v, u, t);
		t.sub[u] |= t.sub[v];
	}
}

int same[MAXN + 5];

inline int dfs_blk(int u, TREE &t)
{
	if(!same[u]) return 0;

	int sum = 1;
	for(auto v : t.adj[u]) if(v != t.fa[u]) sum += dfs_blk(v, t);
	return sum;
}

namespace LIM
{
	vector<int> lim[MAXN + 5];

	inline void clear() { for(int i = 1; i <= n; ++i) lim[i].clear(); }
	inline void add_lim(int u, int v) { lim[u].push_back(v); }

	inline int check()
	{
		static int indeg[MAXN + 5];

		for(int i = 1; i <= n; ++i) indeg[i] = 0;
		for(int u = 1; u <= n; ++u) for(auto v : lim[u]) ++indeg[v];

		static queue<int> q;

		for(int i = 1; i <= n; ++i) if(indeg[i] == 0) q.push(i);
		while(!q.empty())
		{
			int u = q.front();
			q.pop();

			for(auto v : lim[u]) if(--indeg[v] == 0) q.push(v);
		}

		for(int i = 1; i <= n; ++i) if(indeg[i] > 0) return 0;
		return 1;
	}
}

inline int calc(int rt)
{
	dfs(rt, 0, G);
	dfs(rt, 0, T);

	int cnt = 0;
	for(int i = 1; i <= n; ++i) cnt += same[i] = G.fa[i] == T.fa[i];
	if(dfs_blk(rt, G) != cnt || dfs_blk(rt, T) != cnt) return INT_MAX / 2;

	LIM::clear();
	for(int u = 1; u <= n; ++u) if(!same[u])
		for(int v = G.sub[u]._Find_first(); v <= n; v = G.sub[u]._Find_next(v)) if(u != v && !same[v])
			LIM::add_lim(v, u);

	for(int u = 1; u <= n; ++u) if(!same[u])
		for(int v = T.sub[u]._Find_first(); v <= n; v = T.sub[u]._Find_next(v)) if(u != v && !same[v])
			LIM::add_lim(u, v);

	return LIM::check() ? n - cnt : INT_MAX / 2;
}

inline void solve()
{
	int ans = INT_MAX / 2;
	for(int i = 1; i <= n; ++i) chkmin(ans, calc(i));
	for(int i = 1; i <= n; ++i) if(SZ(G.adj[i]) == 1)
		for(int j = 1; j <= n; ++j) if(j != i)
		{
			static auto erase_adj = [] (vector<int> &adjs, int v) { adjs.erase(find(ALL(adjs), v)); };
			int t = G.adj[i][0];

			erase_adj(G.adj[i], t);
			erase_adj(G.adj[t], i);
			G.adj[i].push_back(j);
			G.adj[j].push_back(i);

			chkmin(ans, calc(i) + 1);

			erase_adj(G.adj[i], j);
			erase_adj(G.adj[j], i);
			G.adj[i].push_back(t);
			G.adj[t].push_back(i);
		}
	printf("%d\n", ans == INT_MAX / 2 ? -1 : ans);
}

int main()
{
#ifdef K_ON // K-ON!
	freopen("F.in", "r", stdin);
	freopen("F.out", "w", stdout);
#endif

	for(int cases = read<int>(); cases--; )
	{
		input();
		solve();
	}

	return 0;
}




Measured memory_bytes: 384