#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = 55;
int n;
vector<int> g[2][maxn];

int read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  for (int t = 0; t < 2; t++) {
    for (int i = 0; i < n; i++) {
      g[t][i].clear();
    }
    for (int i = 0; i < n - 1; i++) {
      int v, u;
      scanf("%d%d", &v, &u);
      v--;
      u--;
      g[t][v].push_back(u);
      g[t][u].push_back(v);
    }
  }
  return true;
}

int ok[maxn];
int deg[2][maxn];
int p[2][maxn];

void dfs(int t, int v, int pr) {
  p[t][v] = pr;
  deg[t][v] = 0;
  for (int i = 0; i < sz(g[t][v]); i++) {
    int u = g[t][v][i];
    if (u == pr) {
      continue;
    }
    deg[t][v]++;
    dfs(t, u, v);
  }
}

void solve() {
  int res = inf;
  for (int s = 0; s < n; s++) {
    dfs(0, s, -1);
    dfs(1, s, -1);
    for (int i = 0; i < n; i++) {
      ok[i] = false;
    }
    ok[s] = true;
    while (true) {
      bool found = false;
      for (int v = 0; v < n; v++) {
        if (ok[v]) {
          continue;
        }
        if (p[0][v] == p[1][v] && ok[p[0][v]]) {
          ok[v] = true;
          found = true;
        }
      }
      if (!found) {
        break;
      }
    }
    int cur = 0;
    while (true) {
      bool found = false;
      for (int v = 0; v < n; v++) {
        if (ok[v]) {
          continue;
        }
        if (!deg[0][v] && ok[p[1][v]]) {
          ok[v] = true;
          deg[0][p[0][v]]--;
          cur++;
          found = true;
        }
      }
      if (!found) {
        break;
      }
    }
    bool allok = true;
    for (int i = 0; i < n; i++) {
      if (!ok[i]) {
        allok = false;
      }
    }
    if (allok) {
      res = min(res, cur);
    }
  }
  for (int s = 0; s < n; s++) {
    if (sz(g[0][s]) > 1) {
      continue;
    }
    for (int t = 0; t < n; t++) {
      if (s == t) {
        continue;
      }
      dfs(0, t, -1);
      dfs(1, s, -1);
      for (int i = 0; i < n; i++) {
        ok[i] = false;
      }
      ok[s] = true;
      deg[0][p[0][s]]--;
      int cur = 1;
      while (true) {
        bool found = false;
        for (int v = 0; v < n; v++) {
          if (ok[v]) {
            continue;
          }
          if (!deg[0][v] && ok[p[1][v]]) {
            ok[v] = true;
            deg[0][p[0][v]]--;
            cur++;
            found = true;
          }
        }
        if (!found) {
          break;
        }
      }
      bool allok = true;
      for (int i = 0; i < n; i++) {
        if (!ok[i]) {
          allok = false;
        }
      }
      if (allok) {
        res = min(res, cur);
      }
    }
  }
  if (res >= inf) {
    res = -1;
  }
  printf("%d\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  int t;
  scanf("%d", &t);
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 256