#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;

struct SCC
{
	const int INF = int(1e9);
	vector<vector<int> > vec;
	int index;
	vector<int> idx;
	vector<int> lowlink;
	vector<bool> onstack;
	stack<int> s;
	vector<int> sccidx;
	int scccnt;
	vi topo;
	
	//lower sccidx means appear later
	void init(int n)
	{
		idx.assign(n,-1);
		index = 0;
		onstack.assign(n,0);
		lowlink.assign(n,INF);
		while(!s.empty()) s.pop();
		sccidx.assign(n,-1);
		scccnt = 0;
		vec.clear();
		topo.clear();
		vec.resize(n);
	}
	
	void addedge(int u, int v) //u -> v
	{
		vec[u].pb(v);
	}
	
	void connect(int u)
	{
		idx[u] = index;
		lowlink[u] = index;
		index++;
		s.push(u);
		onstack[u] = true;
		for(int i = 0; i < vec[u].size(); i++)
		{
			int v = vec[u][i];
			if(idx[v] == -1)
			{
				connect(v);
				lowlink[u] = min(lowlink[u], lowlink[v]);
			}
			else if(onstack[v])
			{
				lowlink[u] = min(lowlink[u], idx[v]);
			}
		}
		if(lowlink[u] == idx[u])
		{
			while(1)
			{
				int v = s.top();
				s.pop();
				onstack[v] = false;
				sccidx[v] = scccnt;
				if(v == u) break;
			}
			scccnt++;
		}
	}
	
	void tarjan()
	{
		for(int i = 0; i < vec.size(); i++)
		{
			if(idx[i] == -1)
			{
				connect(i);
			}
		}
	}
	
	void toposort() //if graph is a DAG and i just want to toposort
	{
		tarjan();
		int n = vec.size();
		topo.resize(n);
		vector<ii> tmp;
		for(int i = 0; i < n; i++)
		{
			tmp.pb(mp(sccidx[i],i));
		}
		sort(tmp.begin(),tmp.end());
		reverse(tmp.begin(),tmp.end());
		for(int i = 0; i < n; i++)
		{
			topo[i]=tmp[i].se;
			if(i>0) assert(tmp[i].fi!=tmp[i-1].fi);
		}
	}
};

int par[111];
vi adj[111];
set<ii> E;
int marked[111];

void dfs(int u, int p, int x=1)
{
	par[u]=p;
	for(int v:adj[u])
	{
		if(v==p) continue;
		if(x&&E.find(mp(min(u,v),max(u,v)))!=E.end())
		{
			marked[v]=1;
			dfs(v,u,1);
		}
		else
		{
			dfs(v,u,0);
		}
	}
}

int solve(int n, set<ii> e1, set<ii> e2, int rt)
{
	/*
	cerr<<'\n';
	for(ii x:e1)
	{
		cerr<<x.fi<<' '<<x.se<<'\n';
	}
	for(ii x:e2)
	{
		cerr<<x.fi<<' '<<x.se<<'\n';
	}
	cerr<<'\n';
	*/
	if(e1==e2) return 0;
	for(int i=0;i<n;i++)
	{
		adj[i].clear(); marked[i]=0;
	}
	E=e2;
	for(ii x:e1)
	{
		adj[x.fi].pb(x.se); adj[x.se].pb(x.fi);
	}
	dfs(rt,-1);
	int ans=0;
	for(int i=0;i<n;i++)
	{
		if(i==rt) continue;
		int p=par[i]; int u=i;
		/*
		if(!marked[u]&&e2.find(mp(min(p,u),max(p,u)))!=e2.end()) 
		{
			return int(1e9);
		}
		*/
		if(!marked[u]) ans++;
	}
	marked[rt]=1;
	SCC scc; scc.init(n);
	for(int i=0;i<n;i++)
	{
		if(i==rt||marked[i]) continue;
		int p=par[i]; int u=i;
		if(!marked[p]) scc.addedge(u,p);
	}
	E=e1; 
	for(int i=0;i<n;i++)
	{
		adj[i].clear(); marked[i]=0;
	}
	for(ii x:e2)
	{
		adj[x.fi].pb(x.se); adj[x.se].pb(x.fi);
	}
	dfs(rt,-1);
	marked[rt]=1;
	for(int i=0;i<n;i++)
	{
		if(i==rt||marked[i]) continue;
		int p=par[i]; int u=i;
		if(!marked[p]) scc.addedge(p,u);
	}
	scc.tarjan();
	/*
	for(int i=0;i<n;i++)
	{
		cerr<<"SCC "<<i<<" : "<<scc.sccidx[i]<<'\n';
	}
	*/
	if(scc.scccnt!=n) return int(1e9);
	//cerr<<"ANS : "<<ans<<'\n';
	return ans;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t; cin>>t;
	while(t--)
	{
		int n; cin>>n;
		set<ii> e1,e2;
		for(int i=0;i<n-1;i++)
		{
			int u,v; cin>>u>>v; u--; v--;
			e1.insert(mp(min(u,v),max(u,v)));
		}
		for(int i=0;i<n-1;i++)
		{
			int u,v; cin>>u>>v; u--; v--;
			e2.insert(mp(min(u,v),max(u,v)));
		}
		if(e1==e2)
		{
			cout<<0<<'\n'; continue;
		}
		int ans=int(1e9);
		vector<vi> miniadj(n);
		for(ii x:e1)
		{
			miniadj[x.fi].pb(x.se); miniadj[x.se].pb(x.fi);
		}
		for(int i=0;i<n;i++)
		{
			if(miniadj[i].size()==1)
			{
				int j = miniadj[i][0];
				for(int k=0;k<n;k++)
				{
					if(k==j||k==i) continue;
					set<ii> E1, E2;
					E1 = e1; E2 = e2;
					//cerr<<"DELETE "<<i<<' '<<j<<" INSERT "<<i<<' '<<k<<'\n';
					E1.erase(mp(min(i,j),max(i,j))); E1.insert(mp(min(i,k),max(i,k)));
					ans = min(ans, solve(n, E1, E2, i)+1);
				}
			}
		}
		if(ans>int(1e7)) ans=-1;
		cout<<ans<<'\n';
	}
}


Measured memory_bytes: 256