/**
 *    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;
}

### Prompt

Please formulate a Cpp solution to the following problem:

Snuke has found two trees A and B, each with N vertices numbered 1 to N. The i-th edge of A connects Vertex a_i and b_i, and the j-th edge of B connects Vertex c_j and d_j. Also, all vertices of A are initially painted white.

Snuke would like to perform the following operation on A zero or more times so that A coincides with B:

* Choose a leaf vertex that is painted white. (Let this vertex be v.)
* Remove the edge incident to v, and add a new edge that connects v to another vertex.
* Paint v black.



Determine if A can be made to coincide with B, ignoring color. If the answer is yes, find the minimum number of operations required.

You are given T cases of this kind. Find the answer for each of them.

Constraints

* 1 \leq T \leq 20
* 3 \leq N \leq 50
* 1 \leq a_i,b_i,c_i,d_i \leq N
* All given graphs are trees.

Input

Input is given from Standard Input in the following format:


T
case_1
:
case_{T}


Each case is given in the following format:


N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}


Output

For each case, if A can be made to coincide with B ignoring color, print the minimum number of operations required, and print `-1` if it cannot.

Examples

Input

2
3
1 2
2 3
1 3
3 2
6
1 2
2 3
3 4
4 5
5 6
1 2
2 4
4 3
3 5
5 6


Output

1
-1


Input

3
8
2 7
4 8
8 6
7 1
7 3
5 7
7 8
4 2
5 2
1 2
8 1
3 2
2 6
2 7
4
1 2
2 3
3 4
3 4
2 1
3 2
9
5 3
4 3
9 3
6 8
2 3
1 3
3 8
1 7
4 1
2 8
9 6
3 6
3 5
1 8
9 7
1 6


Output

6
0
7

### Response

```cpp
/**
 *    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;
}

```