#include <cstdio>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cassert>

#define MIN_INT -2147483648
#define MAX_INT 2147483647
#define MIN_LONG -9223372036854775808L
#define MAX_LONG 9223372036854775807L
#define PI 3.141592653589793238462643383279502884L

#define long long long int

using std::vector;
using std::map;
using std::set;
using std::string;
using std::pair;
using std::cin;
using std::cout;
using std::cerr;

// @author: pashka

vector<vector<bool>> a;
vector<vector<bool>> b;
vector<int> d;
vector<int> xxx;
vector<int> parent;
vector<bool> used;
vector<bool> good;

void add_edge(int x, int y) {
//    cout << "add " << x << " " << y << "\n";
    assert(!a[x][y]);
    a[x][y] = a[y][x] = 1;
    xxx[x] ^= y;
    xxx[y] ^= x;
    d[x]++;
    d[y]++;
}

vector<pair<int, int>> queue;
vector<bool> in_queue;

void update_d(int x) {
//    cout << "ud " << x << "\n";
    if (d[x] == 1 && !used[x] && !good[x] && !in_queue[x]) {
        for (int y = 0; y < d.size(); y++) {
            if (b[x][y] && good[y]) {
                in_queue[x] = true;
                queue.push_back({x, y});
                break;
            }
        }
    }
}

void update_g(int y) {
//    cout << "ug " << y << "\n";
    for (int x = 0; x < d.size(); x++) {
        if (b[x][y]) {
            if (d[x] == 1 && !used[x] && !good[x] && !in_queue[x]) {
                in_queue[x] = true;
                queue.push_back({x, y});
            }
        }
    }
}

void remove_edge(int x, int y) {
//    cout << "rem " << x << " " << y << "\n";
    assert(a[x][y]);
    a[x][y] = a[y][x] = 0;
    xxx[x] ^= y;
    xxx[y] ^= x;
    d[x]--;
    d[y]--;
    update_d(x);
    update_d(y);
}


void dfs(int x, int p) {
    parent[x] = p;
    if (p == -1) {
        good[x] = true;
    } else {
        good[x] = good[p] && b[x][p];
    }
    for (int y = 0; y < d.size(); y++) {
        if (a[x][y] && y != p) {
            dfs(y, x);
        }
    }
}

void solve_test() {
    int n;
    cin >> n;
    a.assign(n, vector<bool>(n));
    b.assign(n, vector<bool>(n));
    d.assign(n, 0);
    xxx.assign(n, 0);
    for (int i = 0; i < 2 * (n - 1); i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        if (i < n - 1) {
            add_edge(x, y);
//            a[x][y] = a[y][x] = 1;
        } else {
            b[x][y] = b[y][x] = 1;
        }
    }
    if (a == b) {
        cout << 0 << "\n";
        return;
    }
    auto a_save = a;
    auto d_save = d;
    auto xxx_save = xxx;

    int res = 1000;
    for (int x = 0; x < n; x++) {
        a = a_save;
        d = d_save;
        xxx = xxx_save;
        if (d[x] != 1) continue;
        for (int y = 0; y < n; y++) {
            if (y == x) continue;

//            cout << x << " " << y << std::endl;

            a = a_save;
            d = d_save;
            xxx = xxx_save;
            parent.assign(n, -1);
            used.assign(n, 0);
            good.assign(n, 0);
            queue.resize(0);
            in_queue.assign(n, 0);

            remove_edge(x, xxx[x]);
            add_edge(x, y);

            int s = 1;

            used[x] = true;

            dfs(x, -1);

            for (int i = 0; i < n; i++) {
                if (good[i]) update_g(i);
            }

            for (int i = 0; i < queue.size(); i++) {
                int xx = queue[i].first;
                int yy = queue[i].second;
                remove_edge(xx, xxx[xx]);
                add_edge(xx, yy);
                s++;
                good[xx] = true;
                update_g(xx);
            }

            if (a == b) {
                res = std::min(res, s);
            }
        }
    }
    if (res == 1000) {
        cout << -1 << "\n";
    } else {
        cout << res << "\n";
    }
}

int main() {
    std::ios::sync_with_stdio(false);

    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        solve_test();
    }

    return 0;
}

Measured memory_bytes: 256