#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[200001];
int p1[200001];
int p2[200001];
int d1[200001];
int d2[200001];
void dfs1(int start) {
  for (auto u : adj[start]) {
    if (p1[u] == -1) {
      p1[u] = start;
      d1[u] = d1[start] + 1;
      dfs1(u);
    }
  }
}
void dfs2(int start) {
  for (auto u : adj[start]) {
    if (p2[u] == -1) {
      p2[u] = start;
      d2[u] = d2[start] + 1;
      dfs2(u);
    }
  }
}
void solve() {
  int n, u, v;
  cin >> n >> u >> v;
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    d1[i] = 0;
    d2[i] = 0;
    p1[i] = -1;
    p2[i] = -1;
  }
  p1[u] = 0;
  d1[u] = 0;
  dfs1(u);
  p2[v] = 0;
  d2[v] = 0;
  dfs2(v);
  int ans = 0;
  if (d2[u] == 1 && adj[v][0] == u) {
    cout << ans << endl;
    return;
  }
  for (int i = 1; i <= n; i++) {
    if (d2[i] <= d1[i]) {
      if (d1[i] % 2 == 0)
        ans = max(ans, d2[i] - 1);
      else {
        ans = max(ans, d2[i]);
      }
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
