/**
 *    author:  tourist
 *    created: 15.09.2018 16:12:22       
**/
#include <bits/stdc++.h>

using namespace std;

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

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

  function<bool(int)> ignore;

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

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

  virtual void set_ignore_edge_rule(const function<bool(int)> &f) {
    ignore = f;
  }

  virtual void reset_ignore_edge_rule() {
    ignore = nullptr;
  }
};

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

  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);
    }
    if (ignore != nullptr) {
      rev.set_ignore_edge_rule([&](int id) {
        return ignore(id);
      });
    }
    return rev;
  }
};

template <typename T>
vector<int> find_topsort(const digraph<T> &g) {
  vector<int> deg(g.n, 0);
  for (int id = 0; id < (int) g.edges.size(); id++) {
    if (g.ignore != nullptr && g.ignore(id)) {
      continue;
    }
    deg[g.edges[id].to]++;
  }
  vector<int> x;
  for (int i = 0; i < g.n; i++) {
    if (deg[i] == 0) {
      x.push_back(i);
    }
  }
  for (int ptr = 0; ptr < (int) x.size(); ptr++) {
    int i = x[ptr];
    for (int id : g.g[i]) {
      if (g.ignore != nullptr && g.ignore(id)) {
        continue;
      }
      auto &e = g.edges[id];
      int to = e.to;
      if (--deg[to] == 0) {
        x.push_back(to);
      }
    }
  }
  if ((int) x.size() != g.n) {
    return vector<int>();
  }
  return x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<vector<vector<int>>> g(2, vector<vector<int>>(n));
    for (int rot = 0; rot < 2; rot++) {
      for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        g[rot][x].push_back(y);
        g[rot][y].push_back(x);
      } 
    }
    int ans = n + 1;
    for (int root = 0; root < n; root++) {
      vector<int> was(n, 0);
      vector<int> que(1, root);
      was[root] = 1;
      for (int b = 0; b < (int) que.size(); b++) {
        map<int,int> go;
        for (int rot = 0; rot < 2; rot++) {
          for (int j : g[rot][que[b]]) {
            if (was[j] == 0) {
              go[j]++;
            }
          }
        }
        for (auto &p : go) {
          if (p.second == 2) {
            que.push_back(p.first);
            was[p.first] = 1;
          }
        }
      }
      int cur = n - (int) que.size();
      vector<int> old_que = que;
      vector<int> old_was = was;
      digraph<int> gr(n);
      for (int rot = 0; rot < 2; rot++) {
        que = old_que;
        was = old_was;
        for (int b = 0; b < (int) que.size(); b++) {
          for (int j : g[rot][que[b]]) {
            if (was[j] == 0) {
              que.push_back(j);
              was[j] = 1;
              if (b >= (int) old_que.size()) {
                if (rot == 0) {
                  gr.add(que[b], j);
                } else {
                  gr.add(j, que[b]);
                }
              }
            }
          }
        }
      }
      vector<int> topsort = find_topsort(gr);
      if (!topsort.empty()) {
        ans = min(ans, cur);
      }
    }
    if (ans == n + 1) {
      for (int root = 0; root < n; root++) {
        if (g[1][root].size() != 1) {
          continue;
        }
        int old_nei = g[1][root][0];
        g[1][old_nei].erase(find(g[1][old_nei].begin(), g[1][old_nei].end(), root));
        g[1][root].clear();
        for (int nei = 0; nei < n; nei++) {
          if (root == nei || old_nei == nei) {
            continue;
          }
          g[1][root].push_back(nei);
          g[1][nei].push_back(root);
          vector<int> was(n, 0);
          vector<int> que(1, root);
          was[root] = 1;
          int cur = n;
          vector<int> old_que = que;
          vector<int> old_was = was;
          digraph<int> gr(n);
          for (int rot = 0; rot < 2; rot++) {
            que = old_que;
            was = old_was;
            for (int b = 0; b < (int) que.size(); b++) {
              for (int j : g[rot][que[b]]) {
                if (was[j] == 0) {
                  que.push_back(j);
                  was[j] = 1;
                  if (b >= (int) old_que.size()) {
                    if (rot == 0) {
                      gr.add(que[b], j);
                    } else {
                      gr.add(j, que[b]);
                    }
                  }
                }
              }
            }
          }
          vector<int> topsort = find_topsort(gr);
          if (!topsort.empty()) {
            ans = min(ans, cur);
          }
          g[1][root].pop_back();
          g[1][nei].pop_back();
        }
        g[1][old_nei].push_back(root);
        g[1][root].push_back(old_nei);
      }
    }
    cout << (ans == n + 1 ? -1 : ans) << '\n';
  }
  return 0;
}


Measured memory_bytes: 256