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

Measured memory_bytes: 256