#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <numeric>
using namespace std;

#define repeat(i, x) for (int64_t i = 0; (i) < (int64_t)(x); ++(i))

namespace TaskF {
    using Tree = vector<set<int>>;

    int n_;
    Tree aT_, bT_;
    vector<int> aParents_, bParents_;
    vector<int> deg_;
    vector<bool> use_;

    void DFSA(int u, int p) {
        aParents_[u] = p;
        for (int v : aT_[u]) {
            if (v != p) DFSA(v, u);
        }
    }

    void DFSB(int u, int p) {
        bParents_[u] = p;
        for (int v : bT_[u]) {
            if (v != p) DFSB(v, u);
        }
    }

    Tree inputTree() {
        Tree t(n_);
        repeat (i, n_ - 1) {
            int a, b;
            cin >> a >> b; a--; b--;
            t[a].insert(b);
            t[b].insert(a);
        }
        return t;
    }

    void solve(int testNumber) {
        cin >> n_;

        aParents_.resize(n_, 0);
        bParents_.resize(n_, 0);
        deg_.resize(n_, 0);
        use_.resize(n_, false);

        aT_ = inputTree();
        bT_ = inputTree();

        DFSA(0,-1);
        DFSB(0,-1);

        int ans = -1;
        repeat (s, n_) {
            if (aT_[s].size() == 1) {
                int t = *aT_[s].begin();
                aT_[s].erase(t);
                aT_[t].erase(s);

                repeat (i, n_) {
                    if (s == i) continue;

                    aT_[s].insert(i);
                    aT_[i].insert(s);

                    DFSA(s, -1);
                    DFSB(s, -1);

                    fill(use_.begin(), use_.end(), false);

                    queue <int> que;
                    int cnt = 0;
                    que.push(s);

                    while (!que.empty()) {
                        int v = que.front(); que.pop();
                        use_[v]=true;
                        cnt++;

                        for (int to : aT_[v]) {
                            if(to != aParents_[v] and bParents_[to] == v) que.push(to);
                        }
                    }
                    repeat (j, n_) {
                        deg_[j] = aT_[j].size() - 1;
                    }

                    while (true) {
                        int v = -1;
                        repeat (j, n_) {
                            if(!use_[j] and deg_[j]==0 and use_[bParents_[j]]) {
                                v = j;
                                break;
                            }
                        }
                        if(v == -1) break;
                        use_[v]=true;
                        deg_[aParents_[v]]--;
                    }

                    bool up=true;
                    repeat (j, n_) {
                        if(!use_[j]) {
                            up=false;
                        }
                    }

                    if(up) {
                        int vl=n_-cnt+(t != i);
                        if(ans==-1) ans=vl;
                        else ans=min(ans,vl);
                    }

                    aT_[s].erase(i);
                    aT_[i].erase(s);
                }
                aT_[s].insert(t);
                aT_[t].insert(s);
            }
        }
        cout << ans << endl;
    }
}

int main() {
    int T;
    cin >> T;

    repeat (_, T) {
        TaskF::solve(_);
    }

    return 0;
}


Measured memory_bytes: 256