#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <map>
#include <set>
#include <list>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)

using namespace std;

int i,j,k,l,m,n;
/////////////////////////////////////////////////////
ll T;
ll N;
ll a[55],b[55],c[55],d[55];
set<ll> tree1[55];
set<ll> tree2[55];
vector<ll> ttree1[55];
vector<ll> ttree2[55];
int mk[55];


void dnffix(ll I,ll prevI){
    //printf("%lld ",I);
    mk[I]=-1;
    for(auto i=tree1[I].begin();i!=tree1[I].cend();i++){
        if(tree2[I].find(*i)!=tree2[I].cend()&&prevI!=(*i)){
            dnffix((*i),I);
        }
    }
}
void dnftree1(ll I,ll prevI){
    //printf("p1_%lld ",I);
    for(auto i=tree1[I].begin();i!=tree1[I].cend();i++){
        if(mk[*i]==-1||(*i)==prevI)
            continue;
        if(prevI!=0){
            mk[I]++;
            ttree1[(*i)].push_back(I);
        }
        dnftree1(*i,I);
    }
}
void dnftree2(ll I,ll prevI){
    //printf("p2_%lld ",I);
    for(auto i=tree2[I].begin();i!=tree2[I].cend();i++){
        if(mk[*i]==-1||(*i)==prevI)
            continue;
        if(prevI!=0){
            mk[(*i)]++;
            ttree2[I].push_back(*i);
        }
        dnftree2(*i,I);
    }
}


int main(){
    scanf("%lld",&T);
    for(int Q=0;Q<T;Q++){
        for(i=0;i<=50;i++){
            tree1[i].clear();
            tree2[i].clear();
        }

        scanf("%lld",&N);
        
        for(i=1;i<N;i++){
            scanf("%lld%lld",&a[i],&b[i]);
            tree1[a[i]].insert(b[i]);
            tree1[b[i]].insert(a[i]);
        }
        for(i=1;i<N;i++){
            scanf("%lld%lld",&c[i],&d[i]);
            tree2[c[i]].insert(d[i]);
            tree2[d[i]].insert(c[i]);
        }
        ll ans=999;
        for(i=1;i<=N;i++){
            //if(tree1[i].size()>=2)
            //    continue;
            for(j=1;j<=N;j++){
                if(i==j)
                    continue;
                for(k=0;k<=N;k++){
                    mk[k]=0;
                    ttree1[k].clear();
                    ttree2[k].clear();
                }
                
                int inst=(tree1[j].find(i)==tree1[j].cend()?1:0);
                ll refm=0;
                if(inst){
                    if(tree1[i].size()>=2||ans<=N)
                        continue;
                    //tree1[j].insert(i);
                    mk[i]=-1;
                    dnftree1(j,i);
                    dnftree2(i,0);
                }else{
                    //printf("\n%d:%d\n",i,j);
                    dnffix(i,0);
                    for(k=1;k<=N;k++){
                        if(mk[k]==-1){
                            refm++;
                            dnftree1(k,0);
                            dnftree2(k,0);
                        }
                    }
                }
                
                //printf("%d:%d-%lld %d\n",i,j,refm,inst);
                /*
                for(k=1;k<=N;k++){
                    printf("%d ",mk[k]);
                }
                printf("\n");
                */
                //printf("\n%d:%d-%d\n",i,j,inst);
                vector<ll> tp;
                for(k=1;k<=N;k++){
                    //printf("%d ",mk[k]);
                    if(mk[k]==0){
                        tp.push_back(k);
                    }
                }
                
                for(k=0;k<tp.size();k++){
                    for(l=0;l<ttree1[tp[k]].size();l++){
                        if(--mk[ttree1[tp[k]][l]]==0){
                            tp.push_back(ttree1[tp[k]][l]);
                        }
                    }
                    for(l=0;l<ttree2[tp[k]].size();l++){
                        if(--mk[ttree2[tp[k]][l]]==0){
                            tp.push_back(ttree2[tp[k]][l]);
                        }
                    }
                    //printf("\n%d:\n",k);
                    //for(l=0;l<tp.size();l++){
                    //    printf("%d ",tp[l]);
                    //}
                }
                /*
                for(k=1;k<=N;k++){
                    printf("%d ",mk[k]);
                }
                printf("\n");
                */
                /*
                printf("\n%d:%d-%lld %lld %d\n",i,j,refm,cnum,inst);
                for(k=1;k<=N;k++){
                    printf("%d ",mk[k]);
                }
                */
                bool flag=true;
                for(l=1;l<=N;l++){
                    if(mk[l]!=0&&mk[l]!=-1){
                        flag=false;
                        break;
                    }
                }
                if(flag&&N-refm<ans){
                //if(cnum==N&&N-refm<ans){
                    ans=N-refm;
                }
                
                //if(inst)
                    //tree1[j].erase(i);
            }
        }
        printf("%lld\n",ans==999?-1:ans);
    }
    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 <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <map>
#include <set>
#include <list>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)

using namespace std;

int i,j,k,l,m,n;
/////////////////////////////////////////////////////
ll T;
ll N;
ll a[55],b[55],c[55],d[55];
set<ll> tree1[55];
set<ll> tree2[55];
vector<ll> ttree1[55];
vector<ll> ttree2[55];
int mk[55];


void dnffix(ll I,ll prevI){
    //printf("%lld ",I);
    mk[I]=-1;
    for(auto i=tree1[I].begin();i!=tree1[I].cend();i++){
        if(tree2[I].find(*i)!=tree2[I].cend()&&prevI!=(*i)){
            dnffix((*i),I);
        }
    }
}
void dnftree1(ll I,ll prevI){
    //printf("p1_%lld ",I);
    for(auto i=tree1[I].begin();i!=tree1[I].cend();i++){
        if(mk[*i]==-1||(*i)==prevI)
            continue;
        if(prevI!=0){
            mk[I]++;
            ttree1[(*i)].push_back(I);
        }
        dnftree1(*i,I);
    }
}
void dnftree2(ll I,ll prevI){
    //printf("p2_%lld ",I);
    for(auto i=tree2[I].begin();i!=tree2[I].cend();i++){
        if(mk[*i]==-1||(*i)==prevI)
            continue;
        if(prevI!=0){
            mk[(*i)]++;
            ttree2[I].push_back(*i);
        }
        dnftree2(*i,I);
    }
}


int main(){
    scanf("%lld",&T);
    for(int Q=0;Q<T;Q++){
        for(i=0;i<=50;i++){
            tree1[i].clear();
            tree2[i].clear();
        }

        scanf("%lld",&N);
        
        for(i=1;i<N;i++){
            scanf("%lld%lld",&a[i],&b[i]);
            tree1[a[i]].insert(b[i]);
            tree1[b[i]].insert(a[i]);
        }
        for(i=1;i<N;i++){
            scanf("%lld%lld",&c[i],&d[i]);
            tree2[c[i]].insert(d[i]);
            tree2[d[i]].insert(c[i]);
        }
        ll ans=999;
        for(i=1;i<=N;i++){
            //if(tree1[i].size()>=2)
            //    continue;
            for(j=1;j<=N;j++){
                if(i==j)
                    continue;
                for(k=0;k<=N;k++){
                    mk[k]=0;
                    ttree1[k].clear();
                    ttree2[k].clear();
                }
                
                int inst=(tree1[j].find(i)==tree1[j].cend()?1:0);
                ll refm=0;
                if(inst){
                    if(tree1[i].size()>=2||ans<=N)
                        continue;
                    //tree1[j].insert(i);
                    mk[i]=-1;
                    dnftree1(j,i);
                    dnftree2(i,0);
                }else{
                    //printf("\n%d:%d\n",i,j);
                    dnffix(i,0);
                    for(k=1;k<=N;k++){
                        if(mk[k]==-1){
                            refm++;
                            dnftree1(k,0);
                            dnftree2(k,0);
                        }
                    }
                }
                
                //printf("%d:%d-%lld %d\n",i,j,refm,inst);
                /*
                for(k=1;k<=N;k++){
                    printf("%d ",mk[k]);
                }
                printf("\n");
                */
                //printf("\n%d:%d-%d\n",i,j,inst);
                vector<ll> tp;
                for(k=1;k<=N;k++){
                    //printf("%d ",mk[k]);
                    if(mk[k]==0){
                        tp.push_back(k);
                    }
                }
                
                for(k=0;k<tp.size();k++){
                    for(l=0;l<ttree1[tp[k]].size();l++){
                        if(--mk[ttree1[tp[k]][l]]==0){
                            tp.push_back(ttree1[tp[k]][l]);
                        }
                    }
                    for(l=0;l<ttree2[tp[k]].size();l++){
                        if(--mk[ttree2[tp[k]][l]]==0){
                            tp.push_back(ttree2[tp[k]][l]);
                        }
                    }
                    //printf("\n%d:\n",k);
                    //for(l=0;l<tp.size();l++){
                    //    printf("%d ",tp[l]);
                    //}
                }
                /*
                for(k=1;k<=N;k++){
                    printf("%d ",mk[k]);
                }
                printf("\n");
                */
                /*
                printf("\n%d:%d-%lld %lld %d\n",i,j,refm,cnum,inst);
                for(k=1;k<=N;k++){
                    printf("%d ",mk[k]);
                }
                */
                bool flag=true;
                for(l=1;l<=N;l++){
                    if(mk[l]!=0&&mk[l]!=-1){
                        flag=false;
                        break;
                    }
                }
                if(flag&&N-refm<ans){
                //if(cnum==N&&N-refm<ans){
                    ans=N-refm;
                }
                
                //if(inst)
                    //tree1[j].erase(i);
            }
        }
        printf("%lld\n",ans==999?-1:ans);
    }
    return 0;
}
```