#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

template <typename T>
class graph {
 public:
  struct edge {
    int from;
    int to;
    T cost;
  };

  vector<edge> edges;
  vector<vector<int>> g;
  int n;

  graph(int _n) : n(_n) {
    g.resize(n);
  }

  virtual int add(int from, int to, T cost) = 0;
};

template <typename T>
class digraph : public graph<T> {
 public:
  using graph<T>::edges;
  using graph<T>::g;
  using graph<T>::n;

  digraph(int _n) : graph<T>(_n) {
  }

  int add(int from, int to, T cost = 1) {
    assert(0 <= from && from < n && 0 <= to && to < n);
    int id = (int) edges.size();
    g[from].push_back(id);
    edges.push_back({from, to, cost});
    return id;
  }

  digraph<T> reverse() const {
    digraph<T> rev(n);
    for (auto &e : edges) {
      rev.add(e.to, e.from, e.cost);
    }
    return rev;
  }
};

template <typename T>
vector<int> find_scc(const digraph<T> &g, int &cnt) {
  digraph<T> g_rev = g.reverse();
  vector<int> order;
  vector<bool> was(g.n, false);
  function<void(int)> dfs1 = [&](int v) {
    was[v] = true;
    for (int id : g.g[v]) {
      auto &e = g.edges[id];
      int to = e.to;
      if (!was[to]) {
        dfs1(to);
      }
    }
    order.push_back(v);
  };
  for (int i = 0; i < g.n; i++) {
    if (!was[i]) {
      dfs1(i);
    }
  }
  vector<int> c(g.n, -1);
  function<void(int)> dfs2 = [&](int v) {
    for (int id : g_rev.g[v]) {
      auto &e = g_rev.edges[id];
      int to = e.to;
      if (c[to] == -1) {
        c[to] = c[v];
        dfs2(to);
      }
    }
  };
  cnt = 0;
  for (int id = g.n - 1; id >= 0; id--) {
    int i = order[id];
    if (c[i] != -1) {
      continue;
    }
    c[i] = cnt++;
    dfs2(i);
  }
  return c;
  // c[i] <= c[j] for every edge i -> j
}

class dsu {
 public:
  vector<int> p;
  int n;

  dsu(int _n) : n(_n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
  }

  inline int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      return true;
    }
    return false;
  }
};

template <typename T>
void debug_out(T t) {
  cerr << t;
}

template <typename A, typename B>
void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}

template <typename T>
void debug_out(vector<T> t) {
  int sz = t.size();
  for(int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if(i != sz - 1) cerr << ", ";
  }
}

template <typename T>
void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for(int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if(i != sz - 1) cerr << endl;
  }
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__), cerr << endl;


int tt;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> tt;
  vector<int> ans;
  while(tt--) {
    int n;
    cin >> n;
    vector<vector<int>> to(n);
    vector<vector<int>> from(n);
    set<pair<int, int>> edges;
    dsu cur(n);
    range(i, 0, n - 1) {
      int u, v;
      cin >> u >> v;
      u--; v--;
      from[u].push_back(v);
      from[v].push_back(u);
      edges.insert(make_pair(u, v));
      edges.insert(make_pair(v, u));
    }
    range(i, 0, n - 1) {
      int u, v;
      cin >> u >> v;
      u--; v--;
      to[u].push_back(v);
      to[v].push_back(u);
      if(edges.find(make_pair(u, v)) != edges.end()) cur.unite(u, v);
    }
    int res = -1;
    vector<int> bar(n);
    range(i, 0, n) bar[cur.get(i)]++;
    range(i, 0, n) {
      digraph<int> st(n);
      function<void(int, int, bool)> dfs = [&](int v, int p, bool rev) {
        if(rev) {
          if(p != -1 && cur.get(v) != cur.get(i) && cur.get(p) != cur.get(i)) st.add(v, p);
          for(int u : from[v]) if(u != p) dfs(u, v, rev);
        } else {
          if(p != -1 && cur.get(v) != cur.get(i) && cur.get(p) != cur.get(i)) st.add(p, v);
          for(int u : to[v]) if(u != p) dfs(u, v, rev);
        }
      };
      dfs(i, -1, true);
      dfs(i, -1, false);
      int cnt = 0;
      find_scc(st, cnt);
      if(cnt == n) {
        res = max(res, bar[cur.get(i)]);
      }
    }
    if(res == -1) {
      if(true) {
        range(i, 0, n) {
          range(j, 0, n) {
            if(i == j) continue;
            digraph<int> st(n);
            function<void(int, int, bool)> dfs = [&](int v, int p, bool rev) {
              if(rev) {
                if(p != -1) st.add(v, p);
                for(int u : from[v]) if(u != p) dfs(u, v, rev);
              } else {
                if(p != -1) st.add(p, v);
                for(int u : to[v]) if(u != p) dfs(u, v, rev);
              }
            };
            dfs(i, -1, false);
            dfs(j, -1, true);
            int cnt = 0;
            find_scc(st, cnt);
            if(cnt == n) {
              res = 0;
              break;
            }
          }
          if(res == 0) break;
        }
      }
    }
    if(res != -1) res = n - res;
    cout << res << endl;
  }
  return 0;
}


Measured memory_bytes: 256