#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
//#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=55;
ll n;
ll yal[N][N];
vector <int> y[N];
ll par[N];
ll par2[N];
ll ans;
void dfsfindpar(ll v,ll p){
    par[v]=p;
    for (auto u : y[v]){
        if (u==p) continue;
        dfsfindpar(u,v);
    }
    return ;
}
vector <int> g[N];
vector <int> out[N];
void dfsfind(ll v,ll p){
    par2[v]=p;
    for (auto u : g[v]){
        if (u==p) continue;
        dfsfind(u,v);
    }
    return;
}
ll in[N];
ll jav;
ll vis2[N];
void dddfs(ll v,ll p,ll k){
    if (k!=v){
        out[k].pb(v);
        in[v]++;
    }
    for (auto u : g[v]){
        if (u==p) continue;
        dddfs(u,v,k);
    }
    return ;
}
void ddfs(ll v,ll p){
    if (!vis2[v]) dddfs(v,p,v);
    for (auto u : g[v]){
        if (u==p) continue;
        ddfs(u,v);
    }
    return ;
}
void dfs(ll v,ll p){
    if (par[v]!=p){
        jav++;
        ddfs(v,p);

    }
    for (auto u : g[v]){
        if (u==p) continue;
        dfs(u,v);
    }
    return ;
}
ll vis[N];
void check(){
    memset(vis2,0,sizeof vis2);
    memset(vis,0,sizeof vis);
    queue <int> q;
    for (int i=1;i<=n;i++){
        if (!in[i]) q.push(i);
    }
    while(q.size()){
        ll v=q.front();
        q.pop();
        vis[v]=1;
        for (auto u : out[v]){
            in[u]--;
            if (in[u]==0) q.push(u);
        }
    }
    for (int i=1;i<=n;i++){
        if (!vis[i]) jav=1e9;
    }
    return ;
}
ll solve(ll v){
    jav=0;
    for (int i=1;i<=n;i++){
        out[i].clear();
        in[i]=0;
        g[i].clear();
        for (int j=1;j<=n;j++){
            if (yal[i][j]) g[i].pb(j);
        }
    }
    dfsfind(v,v);
    dfsfindpar(v,v);
    dfs(v,v);
    for (int t=1;t<=n;t++){
        for (int i=1;i<=n;i++){
            if (in[par[i]] || par2[par[i]]!=par[par[i]] || out[par[i]].size()){
                out[i].pb(par[i]);
                in[par[i]]++;
            }
        }
    }
   // cout << par2[par[4]] << " par " << par[par[4]] << endl;
    check();
    return jav;
}
ll Main(){
    ans=1e9;
    cin >> n;
    memset(yal,0,sizeof yal);
    for (int i=1;i<=n;i++) y[i].clear();
    for (int i=1;i<n;i++){
        ll u,v;
        cin >> u >> v;
        yal[u][v]=1;
        yal[v][u]=1;
    }
    for (int i=0;i<n-1;i++){
        ll u,v;
        cin >> u >> v;
        y[u].pb(v);
        y[v].pb(u);
    }
    for (int i=1;i<=n;i++){
        ll deg=0;
        ll id=0;
        for (int j=1;j<=n;j++){
            if (yal[i][j]){
                deg++;
                id=j;
            }
        }
        if (deg>1) continue;
        yal[i][id]=0;
        yal[id][i]=0;
        for (int j=1;j<=n;j++){
            if (i==j) continue;
            /// case ans=0 ;
            yal[i][j]=1;
            yal[j][i]=1;
            ans=min(ans,solve(i)+(j!=id));
       //     cout << i << " " << j << " " << ans << endl;
            yal[i][j]=0;
            yal[j][i]=0;
        }
        yal[i][id]=1;
        yal[id][i]=1;
    }
    if (ans>2*n) kill(-1);
    kill(ans);

}
int32_t main(){
    sync;
    ll t;
    cin >> t;
    while(t--) Main();
    return 0;
}





### Prompt

Construct a CPP code solution to the problem outlined:

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>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
//#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=55;
ll n;
ll yal[N][N];
vector <int> y[N];
ll par[N];
ll par2[N];
ll ans;
void dfsfindpar(ll v,ll p){
    par[v]=p;
    for (auto u : y[v]){
        if (u==p) continue;
        dfsfindpar(u,v);
    }
    return ;
}
vector <int> g[N];
vector <int> out[N];
void dfsfind(ll v,ll p){
    par2[v]=p;
    for (auto u : g[v]){
        if (u==p) continue;
        dfsfind(u,v);
    }
    return;
}
ll in[N];
ll jav;
ll vis2[N];
void dddfs(ll v,ll p,ll k){
    if (k!=v){
        out[k].pb(v);
        in[v]++;
    }
    for (auto u : g[v]){
        if (u==p) continue;
        dddfs(u,v,k);
    }
    return ;
}
void ddfs(ll v,ll p){
    if (!vis2[v]) dddfs(v,p,v);
    for (auto u : g[v]){
        if (u==p) continue;
        ddfs(u,v);
    }
    return ;
}
void dfs(ll v,ll p){
    if (par[v]!=p){
        jav++;
        ddfs(v,p);

    }
    for (auto u : g[v]){
        if (u==p) continue;
        dfs(u,v);
    }
    return ;
}
ll vis[N];
void check(){
    memset(vis2,0,sizeof vis2);
    memset(vis,0,sizeof vis);
    queue <int> q;
    for (int i=1;i<=n;i++){
        if (!in[i]) q.push(i);
    }
    while(q.size()){
        ll v=q.front();
        q.pop();
        vis[v]=1;
        for (auto u : out[v]){
            in[u]--;
            if (in[u]==0) q.push(u);
        }
    }
    for (int i=1;i<=n;i++){
        if (!vis[i]) jav=1e9;
    }
    return ;
}
ll solve(ll v){
    jav=0;
    for (int i=1;i<=n;i++){
        out[i].clear();
        in[i]=0;
        g[i].clear();
        for (int j=1;j<=n;j++){
            if (yal[i][j]) g[i].pb(j);
        }
    }
    dfsfind(v,v);
    dfsfindpar(v,v);
    dfs(v,v);
    for (int t=1;t<=n;t++){
        for (int i=1;i<=n;i++){
            if (in[par[i]] || par2[par[i]]!=par[par[i]] || out[par[i]].size()){
                out[i].pb(par[i]);
                in[par[i]]++;
            }
        }
    }
   // cout << par2[par[4]] << " par " << par[par[4]] << endl;
    check();
    return jav;
}
ll Main(){
    ans=1e9;
    cin >> n;
    memset(yal,0,sizeof yal);
    for (int i=1;i<=n;i++) y[i].clear();
    for (int i=1;i<n;i++){
        ll u,v;
        cin >> u >> v;
        yal[u][v]=1;
        yal[v][u]=1;
    }
    for (int i=0;i<n-1;i++){
        ll u,v;
        cin >> u >> v;
        y[u].pb(v);
        y[v].pb(u);
    }
    for (int i=1;i<=n;i++){
        ll deg=0;
        ll id=0;
        for (int j=1;j<=n;j++){
            if (yal[i][j]){
                deg++;
                id=j;
            }
        }
        if (deg>1) continue;
        yal[i][id]=0;
        yal[id][i]=0;
        for (int j=1;j<=n;j++){
            if (i==j) continue;
            /// case ans=0 ;
            yal[i][j]=1;
            yal[j][i]=1;
            ans=min(ans,solve(i)+(j!=id));
       //     cout << i << " " << j << " " << ans << endl;
            yal[i][j]=0;
            yal[j][i]=0;
        }
        yal[i][id]=1;
        yal[id][i]=1;
    }
    if (ans>2*n) kill(-1);
    kill(ans);

}
int32_t main(){
    sync;
    ll t;
    cin >> t;
    while(t--) Main();
    return 0;
}





```