#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define endl '\n'
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int INF=0x3f3f3f3f,N=2e5+10;
//求从根节点r遍历一个树的时间 2*(n-1)-d d为以r为根节点的树的深度(离r最远d点)
void solve()
{
int n;
cin>>n;
int a,b;
cin>>a>>b;
vector<vector<int>>g(n+1);
for(int i=0;i<n-1;i++)
{
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int>path,pp;
auto dfs=[&](auto &&dfs,int u,int p)->void
{
path.push_back(u);
if(u==b)
{
pp=path;
}
for(auto v:g[u])
{
if(v==p)continue;
dfs(dfs,v,u);
}
path.pop_back();
};
dfs(dfs,a,-1);
int p1=0,p2=pp.size()-1;
int ans=0;
while(p1<p2)//求a和b的中点
{
p1++,p2--;
ans++;
}
auto dfs1=[&](auto &&dfs1,int u,int p)->int
{
int mx=0;
for(auto v:g[u])
{
if(v==p)continue;
mx=max(mx,dfs1(dfs1,v,u)+1);
}
return mx;
};
cout<<ans+2*(n-1)-dfs1(dfs1,pp[p2],-1)<<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t=1;
cin>>t;
while(t--)
{
solve();
}
return 0;
}