#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

void DFS(int R, vector< vector<int> > & G, vector<int> & par, vector<int> & seq) {
	seq.push_back(R);
	ALL_THE(G[R], it) if(par[*it] == -1) {
		par[*it] = R;
		DFS(*it, G, par, seq);
	}
}

int min_ops_test(vector< vector<int> > & Gi, vector< vector<int> > & Gt, int R) {
	int N = Gi.size();
	vector<int> pari(N, -1), part(N, -1);
	vector<int> seqi, seqt;
	pari[R] = R;
	DFS(R, Gi, pari, seqi);
	part[R] = R;
	DFS(R, Gt, part, seqt);
	vector<bool> match(N, false); // vertices with already matching path from R
	match[R] = true;
	for(int i = 1; i < N; i++)
		match[seqi[i]] = (pari[seqi[i]] == part[seqi[i]] && match[pari[seqi[i]]]);
	vector<int> deg(N);
	vector<bool> top(N, false), leaf(N, false);
	for(int i = 0; i < N; i++) if(!match[i] && match[part[i]]) top[i] = true;
	for(int i = 0; i < N; i++) deg[i] = Gi[i].size();
	for(int i = 0; i < N; i++) if(deg[i] == 1) leaf[i] = true;
	int cur = 0, req = N;
	for(int i = 0; i < N; i++) req -= match[i];
	for(int i = 0; i < N; i++) {
		bool done = true;
		for(int j = 0; j < N; j++) if(leaf[j] && top[j]) {
			leaf[j] = false;
			deg[pari[j]]--;
			deg[j] = -1;
			if(deg[pari[j]] == 1 && !match[pari[j]]) leaf[pari[j]] = true;
			top[j] = false;
			ALL_THE(Gt[j], it) if(!match[*it]) top[*it] = true;
			match[j] = true;
			done = false;
			cur++;
			break;
		}
		if(done) break;
	}
	if(cur == req) return req;
	return N+1;
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int T;
	cin >> T;
	for(int t = 0; t < T; t++) {
		int N;
		cin >> N;
		vector< vector<int> > Gi(N), Gt(N);
		for(int i = 0; i < N-1; i++) {
			int u, v;
			cin >> u >> v;
			Gi[--u].push_back(--v);
			Gi[v].push_back(u);
		}
		for(int i = 0; i < N-1; i++) {
			int u, v;
			cin >> u >> v;
			Gt[--u].push_back(--v);
			Gt[v].push_back(u);
		}
		int ans = N+1;
		for(int r = 0; r < N; r++) ans = min(ans, min_ops_test(Gi, Gt, r));
		if(ans == N+1)
		for(int r = 0; r < N; r++) if((int)Gi[r].size() == 1)
			for(int v = 0; v < N; v++) if(v != r) {
				vector< vector<int> > Gi_moved = Gi;
				int u = Gi[r][0];
				for(int i = 0; i < (int)Gi[u].size(); i++) if(Gi[u][i] == r)
					Gi_moved[u].erase(begin(Gi_moved[u])+i);
				Gi_moved[r][0] = v;
				Gi_moved[v].push_back(r);
				ans = min(ans, min_ops_test(Gi_moved, Gt, r) + 1);
			}
		cout << ((ans == N+1) ? -1 : ans) << "\n";
	}
	return 0;
}

// look at my code
// my code is amazing


Measured memory_bytes: 256