/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 110;
VI e[2][SIZE];
set<PII>need;
void init(int N){
    FOR(i,1,N){
        REP(j,2)e[j][i].clear();
    }
    need.clear();
}
bool equal(int N){
    FOR(i,1,N){
        REP(j,2)sort(ALL(e[j][i]));
        if(e[0][i]!=e[1][i])return 0;
    }
    return 1;
}
int deg[SIZE];
bool is_leaf_in_tree0(int x){
    return deg[x]==1;
}
int stop[SIZE];
int dfs0(int x,int lt){
    stop[x]=1;
    int num=1;
    for(int y:e[1][x]){
        if(y==lt)continue;
        if(binary_search(ALL(e[0][x]),y)){
            num+=dfs0(y,x);
        }
    }
    return num;
}
int father[SIZE],father2[SIZE];
void build(int x,int lt){
    father[x]=lt;
    for(int y:e[1][x]){
        if(y==lt)continue;
        build(y,x);
    }
}
void build2(int x,int lt){
    father2[x]=lt;
    for(int y:e[0][x]){
        if(y==lt)continue;
        build2(y,x);
    }
}
bool valid(int N,int x,int y){
    stop[x]=1;
    FOR(i,1,N)deg[i]=SZ(e[0][i]);
    deg[e[0][x][0]]--;
    deg[y]++;
    set<int>AA;
    build(x,x);
    build2(y,y);
    FOR(i,1,N){
        if(deg[i]==1&&!stop[i]){
            //if(x==1&&y==2)W(i,"-");
            AA.insert(i);
        }
    }
    while(SZ(AA)){
        bool suc=0;
        for(int v:AA){
            if(stop[father[v]]){
                //if(x==1&&y==2)W(v,"ker");
                AA.erase(v);
                suc=1;
                stop[v]=1;
                int u=father2[v];
                deg[u]--;
                if(!stop[u]&&deg[u]==1){
                    AA.insert(u);
                }
                break;
            }
        }
        if(!suc)break;
    }
    FOR(i,1,N){
        if(!stop[i])return 0;
    }
    return 1;
}
int main(){
    CASET{
        int N; R(N);
        init(N);
        REPP(i,1,N){
            int x,y; R(x,y);
            e[0][x].PB(y);
            e[0][y].PB(x);
        }
        REPP(i,1,N){
            int x,y; R(x,y);
            need.insert({x,y});
            need.insert({y,x});
            e[1][x].PB(y);
            e[1][y].PB(x);
        }
        FOR(i,1,N)REP(j,2)sort(ALL(e[j][i]));
        if(equal(N))W(0);
        else{
            int an=N+1;
            FOR(x,1,N){
                if(SZ(e[0][x])==1){
                    FOR(y,1,N){
                        if(x==y)continue;
                        if(e[0][x][0]==y)continue;
                        int now=0;
                        MS0(stop);
                        if(need.count({x,y})){
                            now=dfs0(y,x);
                        }
                        if(valid(N,x,y)){
                            //W(x,y,now);
                            an=min(an,N-now);
                        }
                    }
                }
            }
            if(an==N+1)W(-1);
            else W(an);
        }
    }
    return 0;
}


Measured memory_bytes: 256