#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast <t> (x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef double db ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int maxn = 50 + 100 ;
const int inf = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const ll mod = 1e9 + 7 ;
const double eps = 1e-7 ;
template <class T = int> T read()
{
	T f = 1, a = 0;
	char ch = getchar() ;
	while (!isdigit(ch)) { if (ch == '-') f = -1 ; ch = getchar() ; }
	while (isdigit(ch)) { a =  (a << 3) + (a << 1) + ch - '0' ; ch = getchar() ; }
	return a * f ;
}

int t, n;

int ans, sum, flag;

vi e1[maxn], e2[maxn], e3[maxn];

void dfs_addlim_A(int now, int fa)
{
	// printf("dfs_addlim_A : %d %d\n", now, fa);
	for(int to : e1[now])
	{
		if(to == fa) continue;
		if(to == flag) continue;
		e3[to].pb(now);
		dfs_addlim_A(to, now);
	}
}

void dfs_addlim_B(int now, int fa)
{
	// printf("dfs_addlim_B : %d %d\n", now, fa);
	for(int to : e2[now])
	{
		if(to == fa) continue;
		if(to == flag) continue;
		e3[now].pb(to);
		dfs_addlim_B(to, now);
	}
}

void dfs_and(int now, int fa)
{
	// printf("dfs_and : %d %d\n", now, fa);
	int p1 = 0, p2 = 0;
	sum ++;
	while(p1 < siz(e1[now]) && p2 < siz(e2[now]))
	{
		// printf("now : %d %d\n", e1[now][p1], e2[now][p2]);
		if(e1[now][p1] == flag || e1[now][p1] == fa) p1 ++;
		else if(e2[now][p2] == fa) p2 ++;
		else if(e1[now][p1] < e2[now][p2]) dfs_addlim_A(e1[now][p1], now), p1 ++;
		else if(e1[now][p1] > e2[now][p2]) dfs_addlim_B(e2[now][p2], now), p2 ++;
		else dfs_and(e1[now][p1], now), p1 ++, p2 ++;
	}
	while(p1 < siz(e1[now]))
	{
		if(e1[now][p1] == flag || e1[now][p1] == fa) p1 ++;
		else dfs_addlim_A(e1[now][p1 ++], now);
	}
	while(p2 < siz(e2[now]))
	{
		if(e2[now][p2] == fa) p2 ++;
		else dfs_addlim_B(e2[now][p2 ++], now);
	}
}

int in[maxn];

void check()
{
	int ret = 0;
	// printf("A and B : %d\n", sum);
	clr(in);
	qi q;
	rep(i, 1, n) for(int to : e3[i]) ++ in[to];
	rep(i, 1, n) if(!in[i]) q.push(i);
	while(!q.empty())
	{
		int now = q.front();
		ret ++, q.pop();
		for(int to : e3[now]) if(-- in[to] == 0) q.push(to);
	}
	if(ret == n) ans = min(ans, n - sum);
}

void solve()
{
	ans = inf;
	scanf("%d", &n);
	rep(i, 1, n - 1)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		e1[u].pb(v), e1[v].pb(u);
	}
	rep(i, 1, n - 1)
	{
		int u, v;
		scanf("%d %d", &u, &v);
		e2[u].pb(v), e2[v].pb(u);
	}
	rep(i, 1, n) sort(all(e1[i])), sort(all(e2[i]));
	// puts("A : ");
	// rep(i, 1, n)
	// {
	// 	printf("now : %d\n", i);
	// 	for(int to : e1[i]) printf("to : %d\n", to);
	// }
	// puts("B : ");
	// rep(i, 1, n)
	// {
	// 	printf("now : %d\n", i);
	// 	for(int to : e2[i]) printf("to : %d\n", to);
	// }

	// puts("-----------------------------------------");
	// puts("input ok");

	rep(i, 1, n)
	{
		// puts("---------------------------");
		// printf("root : %d\n", i);
		flag = 0;
		rep(j, 1, n) vi().swap(e3[j]);
		sum = 0;
		dfs_and(i, 0);
		check();
		if(siz(e1[i]) > 1) continue;
		flag = i;
		rep(j, 1, n)
		{
			if(i == j) continue;
			// printf("link : %d\n", j);
			rep(k, 1, n) vi().swap(e3[k]);
			sum = 0;
			bool vis = 0;
			for(int to : e2[i])
			{
				if(to != j) dfs_addlim_B(to, i);
				else vis = 1, dfs_and(j, i);
			}
			if(!vis) dfs_addlim_A(j, i);
			check();
		}
	}
	if(ans > n) puts("-1");
	else printf("%d\n", ans);
	rep(i, 1, n) vi().swap(e1[i]), vi().swap(e2[i]);
}

signed main()
{
	scanf("%d", &t);
	while(t --) solve();
	return 0;
}



/*

1
5
1 2
2 3
3 4
4 5
3 1
1 2
2 4
4 5

*/

Measured memory_bytes: 256