#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 55;
int n;
int ANS;
int edA[N][2], edB[N][2];
int parA[N];
vector<int> gA[N];
vector<int> gB[N];
vector<int> sonsA[N], sonsB[N];
bool good[N][3];
bool GB[N][N];

void dfsA(int v, int p) {
	for (int u : gA[v]) {
		if (u == p) continue;
		sonsA[v].push_back(u);
		dfsA(u, v);
	}
}

void dfsB(int v, int p) {
	for (int u : gB[v]) {
		if (u == p) continue;
		sonsB[v].push_back(u);
		dfsB(u, v);
	}
}

void solve(int V, int curAns) {
	for (int i = 0; i < n; i++) {
		gA[i].clear();
		gB[i].clear();
		sonsA[i].clear();
		sonsB[i].clear();
		good[i][0] = good[i][1] = good[i][2] = false;
	}
	for (int i = 1; i < n; i++) {
		gA[edA[i][0]].push_back(edA[i][1]);
		gA[edA[i][1]].push_back(edA[i][0]);
		gB[edB[i][0]].push_back(edB[i][1]);
		gB[edB[i][1]].push_back(edB[i][0]);
	}
	dfsA(V, -1);
	dfsB(V, -1);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			GB[i][j] = 0;
	for (int i = 0; i < n; i++)
		for (int j : sonsB[i])
			GB[i][j] = 1;
	good[V][0] = 1;
	vector<int> q;
	q.push_back(V);
	for (int i = 0; i < (int)q.size(); i++) {
		int v = q[i];
		for (int u : sonsB[v])
			good[u][2] = 1;
		for (int u : sonsA[v]) {
			if (GB[v][u]) {
				good[u][0] = 1;
				q.push_back(u);
			}
		}
	}
	q.clear();
	int deg[N];
	for (int v = 0; v < n; v++)
		for (int u : sonsA[v])
			parA[u] = v;
	for (int v = 0; v < n; v++) {
		deg[v] = (int)sonsA[v].size();
		good[v][1] = deg[v] == 0;
	}
	for (int v = 0; v < n; v++)
		if (!good[v][0] && good[v][1] && good[v][2])
			q.push_back(v);
	for (int i = 0; i < (int)q.size(); i++) {
		int v = q[i];
		good[v][0] = true;
		curAns++;
		int u = parA[v];
		deg[u]--;
		if (deg[u] == 0) {
			good[u][1] = 1;
			if (!good[u][0] && good[u][1] && good[u][2])
				q.push_back(u);
		}
		for (int w : sonsB[v]) {
			good[w][2] = 1;
			if (!good[w][0] && good[w][1] && good[w][2])
				q.push_back(w);
		}
	}
	for (int v = 0; v < n; v++)
		if (!good[v][0])
			return;
	ANS = min(ANS, curAns);
}

void solve() {
	scanf("%d", &n);
	for (int i = 1; i < n; i++)
		for (int j = 0; j < 2; j++) {
			scanf("%d", &edA[i][j]);
			edA[i][j]--;
		}
	for (int i = 1; i < n; i++)
		for (int j = 0; j < 2; j++) {
			scanf("%d", &edB[i][j]);
			edB[i][j]--;
		}
	ANS = N;
	int deg[N];
	for (int i = 0; i < n; i++)
		deg[i] = 0;
	for (int i = 1; i < n; i++)
		for (int j = 0; j < 2; j++)
			deg[edA[i][j]]++;
	for (int v = 0; v < n; v++) {
		solve(v, 0);
		if (deg[v] != 1) continue;
		int id = 1;
		while(edA[id][0] != v && edA[id][1] != v) id++;
		int k = 0;
		if (edA[id][k] == v) k = 1;
		int old = edA[id][k];
		for (int u = 0; u < n; u++) {
			if (u == v || u == old) continue;
			edA[id][k] = u;
			solve(v, 1);
		}
		edA[id][k] = old;
	}
	if (ANS == N) ANS = -1;
	printf("%d\n", ANS);
}

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	int t;
	scanf("%d", &t);
	while(t--) solve();

	return 0;
}


Measured memory_bytes: 256