#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int t;
vector<int>edge[55],edge2[55];
int n;
int up[55],up2[55];
int beg[55],en[55],beg2[55],en2[55],nxt,nxt2;
void dfs(int v,int u){
	up[v] = u; beg[v] = nxt++;
	for(int i=0;i<edge[v].size();i++){
		if(edge[v][i] == u) continue;
		dfs(edge[v][i],v);
	}
	en[v] = nxt-1;
}
void dfs2(int v,int u){
	up2[v] = u; beg2[v] = nxt2++;
	for(int i=0;i<edge2[v].size();i++){
		if(edge2[v][i] == u) continue;
		dfs2(edge2[v][i],v);
	}
	en2[v] = nxt2-1;
}

bool need[55];
bool used[55];
void go(int v,int u){
	used[v] = 1;
	for(int i=0;i<edge[v].size();i++){
	    if(edge[v][i] == u) continue;
		int d = POSL(edge2[v],edge[v][i]);
		if(d == edge2[v].size() || edge2[v][d] != edge[v][i]) continue;
		go(edge[v][i],v);
	}
}
vector<int>edge_tp[55];
void _dfs(int v,int u){
	for(int i=0;i<edge[v].size();i++){
		if(edge[v][i] == u) continue;
		if(!used[v] && !used[edge[v][i]]){
			edge_tp[v].pb(edge[v][i]);
		}
		_dfs(edge[v][i],v);
	}
}
void _dfs2(int v,int u){
	for(int i=0;i<edge2[v].size();i++){
		if(edge2[v][i] == u) continue;
		if(!used[v] && !used[edge2[v][i]]){
			edge_tp[edge2[v][i]].pb(v);
		}
		_dfs2(edge2[v][i],v);
	}
}
bool find_cycle(){
	int in[55]={};
	for(int i=1;i<=n;i++){
		for(int j=0;j<edge_tp[i].size();j++){
			in[edge_tp[i][j]]++;
		}
	}
	queue<int>que;
	for(int i=1;i<=n;i++){
		if(!in[i]) que.push(i);
	}
	while(!que.empty()){
		int q = que.front(); que.pop();
		for(int i=0;i<edge_tp[q].size();i++){
			in[edge_tp[q][i]]--;
			if(in[edge_tp[q][i]] == 0){
				que.push(edge_tp[q][i]);
			}
		}
	}
	for(int i=1;i<=n;i++) if(in[i]) return true;
	return false;
}
int sol2(int v){
	nxt = 0; nxt2 = 0;
	for(int i=1;i<=n;i++){
		SORT(edge[i]);
		SORT(edge2[i]);
	}
	dfs(v,-1);
	dfs2(v,-1);
	for(int i=1;i<=n;i++){
		used[i] = 0;
	}
	go(v,-1);
	for(int i=1;i<=n;i++){
		edge_tp[i].clear();
	}
	_dfs(v,-1);
	_dfs2(v,-1);
	if(find_cycle()) return INF;
	int ret = 0;
	for(int i=1;i<=n;i++){
		ret += (!used[i]);
	}
	return ret;
}
void sol(){
	cin>>n;
	for(int i=0;i<n-1;i++){
		int a,b; cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	for(int i=0;i<n-1;i++){
		int a,b; cin>>a>>b;
		edge2[a].pb(b);
		edge2[b].pb(a);
	}
	int ans = INF;
	for(int v=1;v<=n;v++){
		if(edge[v].size() > 1) continue;
		ans = min(ans,sol2(v));
		if(ans <= 1) continue;
		int to = edge[v][0];
		for(int u=1;u<=n;u++){
			if(v == u) continue;
			if(to == u) continue;
			edge[v].clear();
			for(int j=0;j<edge[to].size();j++){
				if(edge[to][j] == v){
					swap(edge[to][j],edge[to][edge[to].size()-1]);
					edge[to].pop_back();
					break;
				}
			}
			edge[v].pb(u);
			edge[u].pb(v);
			ans = min(ans,sol2(v)+1);
			
			edge[v].clear();
			for(int j=0;j<edge[u].size();j++){
				if(edge[u][j] == v){
					swap(edge[u][j],edge[u][edge[u].size()-1]);
					edge[u].pop_back();
					break;
				}
			}
			edge[v].pb(to);
			edge[to].pb(v);
		}
	}
	if(ans > 1e7) ans = -1;
	cout << ans << endl;
}
int main(){
	cin>>t;
	rep(i,t){
		rep(j,55) edge[j].clear();
		rep(j,55) edge2[j].clear();
		sol();
	}
}

Measured memory_bytes: 256