#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();
    }
}


### 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
#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();
    }
}


```