#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

const int N = 50 + 5;

int test, n;

set<int> graph[N], gr[N], graph1[N], gr1[N];
int par[N], lvl[N], sum[N], used[N], ans;
set<int>::iterator it;
bool vis[N];
queue<int> q;

void bfs(int p) {
    q.push(p);
    lvl[p] = 0;
    used[p] = 1;
    memset(vis, 0, sizeof(vis));
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 1;
        for(it = graph1[u].begin(); it != graph1[u].end(); it++) {
            int v = *it;
            //cout << v << "\n";
            if(vis[v]) continue;
            vis[v] = 1;
            par[v] = u;
            lvl[v] = lvl[u] + 1;
            q.push(v);
        }
    }
}

int lmao() {
    int cnt = 0, amount = 0;
    for(int i = 1; i <= n; i++)
        sum[lvl[i]]++;
    while(1) {
        int ok = 0;
        for(int i = 1; i <= n; i++) {
            if(used[par[i]] && gr[i].find(par[i]) != gr[i].end() && !used[i]) {
                sum[lvl[i]]--;
                ok = 1;
                used[i] = 1;
            }
        }
        if(!ok)
            break;
    }
    while(1){
        int ok = 0;
        for(int i = 1; i <= n; i++) {
            if(!used[i] && gr[i].size() == 1 && used[par[i]]) {
                int u = *(gr[i].begin());
                gr[u].erase(i);
                gr[i].erase(u);
                gr[par[i]].insert(i);
                gr[i].insert(par[i]);
                ok = 1;
                amount++;
                used[i] = 1;
                sum[lvl[i]]--;
                break;
            }
        }
        if(!ok) break;
    }
    for(int i = 1; i <= n; i++) {
        if(sum[i])
            return -2;
    }
    return amount;
}

int main() {
    cin.tie(0), ios::sync_with_stdio(0);
    //freopen("test.inp", "r", stdin);
    cin >> test;
    while(test--) {
        cin >> n;
        for(int i = 1; i <= n; i++)
            graph[i].clear(), gr[i].clear(), graph1[i].clear();
        ans = n + 1;
        for(int i = 1; i <= n - 1; i++) {
            int u, v;
            cin >> u >> v;
            graph[u].insert(v);
            graph[v].insert(u);
        }
        for(int i = 1; i <= n - 1; i++) {
            int u, v;
            cin >> u >> v;
            graph1[u].insert(v);
            graph1[v].insert(u);
        }
        int ck = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++)
                sum[j] = 0, lvl[j] = 0, used[j] = 0;
            for(int j = 1; j <= n; j++)
                gr[j] = graph[j];
            bfs(i);
            int s = lmao();
            if(s >= 0)
                ans = min(ans, s);
        }
        for(int j = 1; j <= n; j++)
            gr1[j] = graph1[j];
        for(int i = 1; i <= n; i++) {
            if(graph[i].size() != 1) continue;
            for(int j = 1; j <= n; j++) {
                if(i == j) continue;
                for(int j = 1; j <= n; j++)
                    gr[j] = graph[j];
                for(int j = 1; j <= n; j++)
                    sum[j] = 0, lvl[j] = 0, used[j] = 0;
                if(gr[i].find(j) != gr[i].end()) continue;
                int u = *(gr[i].begin());
                gr[i].erase(u);
                gr[u].erase(i);
                gr[j].insert(i);
                gr[i].insert(j);
                bfs(i);
                int s = lmao() + 1;
                if(s >= 0)
                    ans = min(ans, s);
            }
        }
        if(ans == n + 1)
            cout << -1 << "\n";
        else
            cout << ans << "\n";
    }
}


Measured memory_bytes: 256