#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
const int N = 55;
int n;
namespace Gr {
vector<int> G[N];
queue<int> que;
int du[N];
void init() {
  for (int i = 1; i <= n; ++i) G[i].clear(), du[i] = 0;
}
void ad(int x, int y) {
  G[x].push_back(y);
  du[y]++;
}
bool sol() {
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (!du[i]) que.push(i);
  while (!que.empty()) {
    cnt++;
    int u = que.front();
    que.pop();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (!(--du[v])) que.push(v);
    }
  }
  return cnt == n;
}
}  // namespace Gr
vector<int> G[2][N];
int fa[2][N];
int mp[2][N][N];
int g[N], flg;
int dfs2(int u) {
  int tot = 1;
  for (int v = 1; v <= n; ++v)
    if (fa[0][v] == u && fa[1][v] == u) tot += dfs2(v);
  return tot;
}
void dfs(int p, int u, int last) {
  fa[p][u] = last;
  for (int i = 0; i < G[p][u].size(); ++i)
    if (G[p][u][i] != last) dfs(p, G[p][u][i], u);
}
int vis[N], tot_siz;
void check(int u) {
  if (vis[u]) return;
  tot_siz++, vis[u] = 1;
  for (int i = 0; i < G[0][u].size(); ++i) check(G[0][u][i]);
}
int sol(int rt) {
  for (int d = 0; d < 2; ++d)
    for (int u = 1; u <= n; ++u) {
      G[d][u].clear();
      for (int v = 1; v <= n; ++v)
        if (mp[d][u][v]) G[d][u].push_back(v);
    }
  {
    tot_siz = 0;
    for (int i = 1; i <= n; ++i) vis[i] = 0;
    check(1);
    if (tot_siz < n) return 0;
  }
  dfs(0, rt, 0);
  dfs(1, rt, 0);
  Gr::init();
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (fa[0][i] != fa[1][i]) {
      if (fa[1][fa[1][i]] != fa[0][fa[1][i]]) Gr::ad(fa[1][i], i);
      if (fa[1][fa[0][i]] != fa[0][fa[0][i]]) Gr::ad(i, fa[0][i]);
    } else
      cnt++;
  if (dfs2(rt) != cnt) return 0;
  if (Gr::sol()) {
    flg = 1;
    for (int i = 1; i <= n; ++i) cnt += fa[1][i] == fa[0][i];
    return cnt;
  }
}
int main() {
  int T;
  rd(T);
  while (T--) {
    flg = 0;
    rd(n);
    for (int i = 1, x, y; i < n; ++i)
      rd(x), rd(y), mp[0][x][y] = mp[0][y][x] = 1;
    for (int i = 1, x, y; i < n; ++i)
      rd(x), rd(y), mp[1][x][y] = mp[1][y][x] = 1;
    int ans = 0;
    for (int i = 1; i <= n; ++i) ans = max(ans, sol(i));
    for (int u = 1; u <= n; ++u)
      for (int v = 1; v <= n; ++v)
        if (mp[0][u][v])
          for (int w = 1; w <= n; ++w)
            if (!mp[0][u][w]) {
              mp[0][u][v] = mp[0][v][u] = 0;
              mp[0][u][w] = mp[0][w][u] = 1;
              ans = max(ans, sol(u) - 1);
              mp[0][u][v] = mp[0][v][u] = 1;
              mp[0][u][w] = mp[0][w][u] = 0;
            }
    if (!flg)
      printf("-1\n");
    else
      printf("%d\n", n - ans);
    for (int d = 0; d < 2; ++d)
      for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) mp[d][i][j] = 0;
  }
  return 0;
}
