#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using ld = long double;

const int MAXN = 120;

vector<int> ea[MAXN];
vector<int> eb[MAXN];
vector<int> e[MAXN];

vector<int> eacopy[MAXN];
vector<int> ebcopy[MAXN];
vector<int> ecopy[MAXN];

int n;

void clear() {
    for (int i = 0; i <= n; ++i) {
        ea[i].clear();
        eb[i].clear();
        e[i].clear();
    }
}

vector<int> ecycle[MAXN];

void dfsa(int v, int p) {
    ecycle[v].push_back(p);

    for (int u : ea[v]) {
        if (u != p) {
            dfsa(u, v);
        }
    }
}

void dfsb(int v, int p) {
    ecycle[p].push_back(v);

    for (int u : eb[v]) {
        if (u != p) {
            dfsb(u, v);
        }
    }
}

vector<int> mainComp;

void dfs(int v, int p) {
    mainComp.push_back(v);

    for (int u: e[v]) {
        if (u != p) {
            dfs(u, v);
        }
    }
}

int state[MAXN];

void dfsCycle(int v) {
    if (state[v] == 2) {
        return;
    }

    if (state[v] == 1) {
        throw 0;
    }

    state[v] = 1;

    for (int u: ecycle[v]) {
        dfsCycle(u);
    }

    state[v] = 2;
}

int go(int i) {
    for (int j = 1; j <= n; ++j) {
        ecycle[j].clear();
    }

    mainComp.clear();
    dfs(i, i);
    dfsa(i, i);
    dfsb(i, i);

    for (int v : mainComp) {
        ecycle[v].clear();
    }

    fill(state, state + n + 1, 0);

    try {
        for (int j = 1; j <= n; ++j) {
            dfsCycle(j);
        }
    } catch (int) {
        return n + 1;
    }

    return n - mainComp.size();
}

void solve() {
    cin >> n;

    clear();

    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        ea[a].push_back(b);
        ea[b].push_back(a);
    }

    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        eb[a].push_back(b);
        eb[b].push_back(a);
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            auto ita = find(ea[i].begin(), ea[i].end(), j);
            auto itb = find(eb[i].begin(), eb[i].end(), j);
            if (ita != ea[i].end() && itb != eb[i].end()) {
                e[i].push_back(j);
            } 
        }
    }

    int ans = n + 1;

    for (int i = 1; i <= n; ++i) {
        ans = min(ans, go(i));
        copy(ea + 1, ea + n + 1, eacopy + 1);
        copy(e + 1, e + n + 1, ecopy + 1);

        if (ea[i].size() == 1) {
            int st = ea[i][0];

            ea[i].clear();
            auto it = find(ea[st].begin(), ea[st].end(), i);
            ea[st].erase(it);

            if (!e[i].empty()) {
                e[i].clear();

                auto it2 = find(e[st].begin(), e[st].end(), i);
                e[st].erase(it2);
            }

            for (int dest = 1; dest <= n; ++dest) {
                if (dest == st || dest == i) {
                    continue;
                }

                auto itb = find(eb[i].begin(), eb[i].end(), dest);
                if (itb != eb[i].end()) {
                    continue;
                }

                ea[i].push_back(dest);
                ea[dest].push_back(i);
                ans = min(ans, go(i) + 1);
                ea[i].clear();
                ea[dest].pop_back();
            }
        }

        copy(eacopy + 1, eacopy + n + 1, ea + 1);
        copy(ecopy + 1, ecopy + n + 1, e + 1);
    }

    if (ans > n) {
        cout << -1 << "\n";
    } else {
        cout << ans << "\n";
    }
}

int main() {
#ifdef BZ
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20);
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }
}



Measured memory_bytes: 256