#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>inline ostream& operator<<(ostream&o,const v(T)&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>inline ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return;}
#define yn {puts("YES");}else{puts("NO");}
const int MX = 200005;

// SCC
struct scc {
  int n, k;
  vvi to, ot, d, gt; // to, rev_to, groups, group_to
  vi g, used, kv; // group, gomi, topo_ord
  scc(int n=0):n(n),to(n),ot(n){}
  int inc() { to.pb(vi()); ot.pb(vi()); return n++;}
  void add(int a, int b) { to[a].pb(b); ot[b].pb(a);}
  void dfs(int v) {
    if (used[v]) return;
    used[v] = 1;
    rep(i,sz(to[v])) dfs(to[v][i]);
    kv[--k] = v;
  }
  void rfs(int v) {
    if (g[v] != -1) return;
    g[v] = k; d[k].pb(v);
    rep(i,sz(ot[v])) rfs(ot[v][i]);
  }
  void init() {
    k = n;
    used = kv = vi(n);
    g = vi(n,-1);
    rep(i,n) dfs(i);
    rep(i,n) if (g[kv[i]] == -1)  {
      d.pb(vi());
      rfs(kv[i]);
      k++;
    }
    gt = vvi(k);
    rep(i,n)rep(j,sz(ot[i])) {
      int v = g[ot[i][j]], u = g[i];
      if (v != u) gt[v].pb(u);
    }
    rep(i,k) {
      sort(rng(gt[i]));
      gt[i].erase(unique(rng(gt[i])),gt[i].end());
    }
  }
};
//
struct Solver {
  int n;
  vvi to, g;
  scc s;
  void tfs(int v, int p=-1) {
    for (int u : to[v]) {
      if (u == p) continue;
      s.add(v,u);
      tfs(u,v);
    }
  }
  void gfs(int v, int p=-1) {
    for (int u : g[v]) {
      if (u == p) continue;
      s.add(u,v);
      gfs(u,v);
    }
  }
  int dfs(int v, int p=-1) {
    int res = 1;
    rep(i,n) {
      if (i == v || i == p) continue;
      int fl = 0;
      if (binary_search(rng(to[v]),i)) fl |= 1;
      if (binary_search(rng(g[v]),i)) fl |= 2;
      if (fl == 1) tfs(i,v);
      if (fl == 2) gfs(i,v);
      if (fl == 3) res += dfs(i,v);
    }
    return res;
  }
  int calc(int root) {
    s = scc(n);
    int res = n-dfs(root);
    s.init();
    if (s.k != n) return INF;
    return res;
  }
  void solve() {
    scanf("%d",&n);
    to = g = vvi(n);
    rep(i,n-1) {
      int a,b;
      scanf("%d%d",&a,&b);
      --a; --b;
      to[a].pb(b);
      to[b].pb(a);
    }
    rep(i,n-1) {
      int a,b;
      scanf("%d%d",&a,&b);
      --a; --b;
      g[a].pb(b);
      g[b].pb(a);
    }
    rep(i,n) sort(rng(to[i]));
    rep(i,n) sort(rng(g[i]));
    int ans = INF;
    rep(i,n) {
      int now = calc(i);
      mins(ans,now);
      // cerr<<i<<": "<<now<<endl;
      if (now == INF && sz(to[i]) == 1) {
        int p = to[i][0];
        rep(j,n) if (i != j && j != p) {
          vvi to_ = to;
          to = vvi(n);
          rep(v,n) for (int u : to_[v]) {
            if (P(v,u) == P(i,p)) continue;
            if (P(v,u) == P(p,i)) continue;
            to[v].pb(u);
          }
          to[i].pb(j);
          to[j].pb(i);
          rep(k,n) sort(rng(to[k]));
          int nw = calc(i);
          mins(ans,nw+1);
          to = to_;
        }
      }
    }
    if (ans == INF) ans = -1;
    cout<<ans<<endl;
    // if (ans == -1) {
    //   cout<<n<<endl;
    //   rep(i,n)for (int j : to[i]) if (i < j) cout<<i+1<<" "<<j+1<<endl;
    //   rep(i,n)for (int j : g[i]) if (i < j) cout<<i+1<<" "<<j+1<<endl;
    // }
  }
};

int main() {
  int ts;
  scanf("%d",&ts);
  rrep(ti,ts) {
    Solver solver;
    solver.solve();
    // cerr<<ti<<endl;
  }
  return 0;
}






















Measured memory_bytes: 256