#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<bitset>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

typedef bitset<51> B;

struct UF{
	int par[100];
	void init(){
		rep(i,100)par[i] = i;
	}
	int find(int x){
		if(par[x] == x)return x;
		return par[x] = find(par[x]);
	}
	bool same(int x,int y){
		return find(x) == find(y);
	}
	void unit(int x,int y){
		if(same(x,y))return;
		x = find(x);
		y = find(y);
		par[x] = y;
	}
}uf;

int N;
struct Tree{
	int d[52];
	vector<int> G[52];
	void clear(){
		rep(i,52){
			G[i].clear();
			d[i] = 0;
		}
	}
}X,Y;

bool f(Tree X_,B b){
	int cnt = b.count();
	set<int> S;
	for(int i = 1 ; i <= N ; i ++){
		if(X_.d[i] == 1 && !b[i])S.insert(i);
	}
	while(cnt < N){
		bool update = false;
		for(int i = 1 ; i <= N ; i ++){
			if(b[i])continue;
			bool s = false;
			for(int j: Y.G[i]){
				if(b[j]){
					s = true;
					break;
				}
			}
			if(!s)continue;
			if(S.find(i) != S.end()){
				S.erase(i);
				for(int j: X_.G[i]){
					X_.d[j] --;
					if(X_.d[j] == 1 && !b[j])S.insert(j);
				}
				b[i] = true;
				update = true;
				cnt ++;
			}
		}
		if(!update)return false;
	}
	return true;
}

bool g(Tree X_, int v){
	for(int u: X_.G[v]){
		X_.d[u] --;
	}
	B b;
	b[v] = true;
	return f(X_,b);
}

int main(){
	int T;
	scanf("%d",&T);
	rep(ppp,T){
		uf.init();
		int a[50],b[50],c[50],d[50];
		scanf("%d",&N);
		for(int i = 0 ; i < N-1 ; i ++){
			scanf("%d%d",&a[i],&b[i]);
			if(a[i]>b[i])swap(a[i],b[i]);
		}
		for(int i = 0 ; i < N-1 ; i ++){
			scanf("%d%d",&c[i],&d[i]);
			if(c[i]>d[i])swap(c[i],d[i]);
		}
		set<P> S;
		rep(i,N-1)S.insert(P(a[i],b[i]));
		rep(i,N-1){
			if(S.find(P(c[i],d[i])) != S.end()){
				uf.unit(c[i],d[i]);
			}
		}
		
		X.clear();
		Y.clear();
		for(int i = 0 ; i <N-1 ; i ++){
			X.G[a[i]].push_back(b[i]);
			X.G[b[i]].push_back(a[i]);
			X.d[a[i]] ++;
			X.d[b[i]] ++;
		}
		for(int i = 0 ; i <N-1 ; i ++){
			Y.G[c[i]].push_back(d[i]);
			Y.G[d[i]].push_back(c[i]);
			Y.d[c[i]] ++;
			Y.d[d[i]] ++;
		}
		
		int ret = 10000;
		for(int i = 1 ; i <= N ; i ++){
			if(uf.find(i) != i)continue;
			B b;
			for(int j = 1 ; j <= N ; j ++){
				if(uf.find(j) == i)b[j] = true;
			}
			int t = b.count();
			//cout << b << endl;
			if(f(X,b))ret = min( ret , N-t );
		}
		for(int i = 1 ; i <= N ; i ++){
			if(X.d[i] == 1){
				if(g(X,i))ret = min( ret , N );
			}
		}
		if(ret == 10000)cout << -1 << endl;
		else cout << ret << endl;
	}
}
