#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 55;
int n;
int yal[N][N];
vector<int> y[N];
int par[N];
int par2[N];
int ans;
void dfsfindpar(int v, int p) {
  par[v] = p;
  for (auto u : y[v]) {
    if (u == p) continue;
    dfsfindpar(u, v);
  }
  return;
}
vector<int> g[N];
vector<int> out[N];
void dfsfind(int v, int p) {
  par2[v] = p;
  for (auto u : g[v]) {
    if (u == p) continue;
    dfsfind(u, v);
  }
  return;
}
int in[N];
int jav;
int vis2[N];
void dddfs(int v, int p, int k) {
  if (k != v) {
    out[k].push_back(v);
    in[v]++;
  }
  for (auto u : g[v]) {
    if (u == p) continue;
    dddfs(u, v, k);
  }
  return;
}
void ddfs(int v, int p) {
  if (!vis2[v]) dddfs(v, p, v);
  for (auto u : g[v]) {
    if (u == p) continue;
    ddfs(u, v);
  }
  return;
}
void dfs(int v, int p) {
  if (par[v] != p) {
    jav++;
    ddfs(v, p);
  }
  for (auto u : g[v]) {
    if (u == p) continue;
    dfs(u, v);
  }
  return;
}
int vis[N];
void check() {
  memset(vis2, 0, sizeof vis2);
  memset(vis, 0, sizeof vis);
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (!in[i]) q.push(i);
  }
  while (q.size()) {
    int v = q.front();
    q.pop();
    vis[v] = 1;
    for (auto u : out[v]) {
      in[u]--;
      if (in[u] == 0) q.push(u);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) jav = 1e9;
  }
  return;
}
int solve(int v) {
  jav = 0;
  for (int i = 1; i <= n; i++) {
    out[i].clear();
    in[i] = 0;
    g[i].clear();
    for (int j = 1; j <= n; j++) {
      if (yal[i][j]) g[i].push_back(j);
    }
  }
  dfsfind(v, v);
  dfsfindpar(v, v);
  dfs(v, v);
  for (int t = 1; t <= n; t++) {
    for (int i = 1; i <= n; i++) {
      if (in[par[i]] || par2[par[i]] != par[par[i]] || out[par[i]].size()) {
        out[i].push_back(par[i]);
        in[par[i]]++;
      }
    }
  }
  check();
  return jav;
}
int Main() {
  ans = 1e9;
  cin >> n;
  memset(yal, 0, sizeof yal);
  for (int i = 1; i <= n; i++) y[i].clear();
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    yal[u][v] = 1;
    yal[v][u] = 1;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    y[u].push_back(v);
    y[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    int deg = 0;
    int id = 0;
    for (int j = 1; j <= n; j++) {
      if (yal[i][j]) {
        deg++;
        id = j;
      }
    }
    if (deg > 1) continue;
    yal[i][id] = 0;
    yal[id][i] = 0;
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      yal[i][j] = 1;
      yal[j][i] = 1;
      ans = min(ans, solve(i) + (j != id));
      yal[i][j] = 0;
      yal[j][i] = 0;
    }
    yal[i][id] = 1;
    yal[id][i] = 1;
  }
  if (ans > 2 * n) return cout << -1 << '\n', 0;
  ;
  return cout << ans << '\n', 0;
  ;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) Main();
  return 0;
}
