#include <bits/stdc++.h>
using namespace std;
struct edge {
  int u, v;
} e[52];
int head[52], ver[52 * 52], nxt[52 * 52], l;
int head1[52], ver1[52 * 2], nxt1[52 * 2], l1;
int t, n, i, j, k, fa[52], fb[52], d[52], d1[52], cnt;
bool vis[52];
int read() {
  char c = getchar();
  int w = 0;
  while (c < '0' || c > '9') c = getchar();
  while (c <= '9' && c >= '0') {
    w = w * 10 + c - '0';
    c = getchar();
  }
  return w;
}
void insert(int x, int y) {
  l++;
  ver[l] = y;
  nxt[l] = head[x];
  head[x] = l;
}
void insert1(int x, int y) {
  l1++;
  ver1[l1] = y;
  nxt1[l1] = head1[x];
  head1[x] = l1;
}
void dfsa(int x, int pre) {
  fa[x] = pre;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = ver[i];
    if (y != pre) dfsa(y, x);
  }
}
void dfsb(int x, int pre) {
  fb[x] = pre;
  vis[x] = vis[pre] & (fa[x] == fb[x]);
  for (int i = head1[x]; i; i = nxt1[i]) {
    int y = ver1[i];
    if (y != pre) dfsb(y, x);
  }
}
bool toposort() {
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (d1[i] == 0) {
      q.push(i);
    }
  }
  int cnt = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    cnt++;
    for (int i = head[x]; i; i = nxt[i]) {
      int y = ver[i];
      d1[y]--;
      if (d1[y] == 0) q.push(y);
    }
  }
  return (cnt == n);
}
bool check1() {
  for (int i = 1; i <= n; i++) {
    if (fa[i] != fb[i]) return 0;
  }
  return 1;
}
int check(int root) {
  memset(vis, 0, sizeof(vis));
  vis[0] = 1;
  for (int i = 1; i < n; i++) {
    if (e[i].u != root && e[i].v != root)
      insert(e[i].u, e[i].v), insert(e[i].v, e[i].u);
  }
  dfsa(root, 0);
  dfsb(root, 0);
  memset(head, 0, sizeof(head));
  l = 0;
  memset(d1, 0, sizeof(d1));
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      insert(i, fa[i]);
      d1[fa[i]]++;
      insert(fb[i], i);
      d1[i]++;
      ans++;
    }
  }
  if (toposort())
    return ans;
  else
    return 1 << 30;
}
int main() {
  t = read();
  while (t--) {
    cnt++;
    int ans = 1 << 30;
    memset(d, 0, sizeof(d));
    memset(head1, 0, sizeof(head1));
    l1 = 0;
    memset(head, 0, sizeof(head));
    l = 0;
    n = read();
    for (i = 1; i < n; i++) {
      e[i].u = read();
      e[i].v = read();
      d[e[i].u]++;
      d[e[i].v]++;
      insert(e[i].u, e[i].v);
      insert(e[i].v, e[i].u);
    }
    for (i = 1; i < n; i++) {
      int u = read(), v = read();
      insert1(u, v);
      insert1(v, u);
    }
    dfsa(1, 0);
    dfsb(1, 0);
    if (check1()) {
      puts("0");
      continue;
    }
    for (i = 1; i <= n; i++) {
      if (d[i] == 1) {
        for (j = 1; j <= n; j++) {
          if (i == j) continue;
          memset(head, 0, sizeof(head));
          l = 0;
          insert(i, j);
          insert(j, i);
          ans = min(ans, check(i));
        }
      }
    }
    if (ans != 1 << 30)
      printf("%d\n", ans + 1);
    else
      puts("-1");
  }
  return 0;
}
