#if defined(_USE_PCH_)
#include "pch.hpp"
#else
#include <bits/stdc++.h>
#endif
#define RNG(V_, A_, B_, ...) for(int V_=(A_), V_##_END=(B_) __VA_OPT__(,) __VA_ARGS__; V_<=V_##_END; V_++)
#define IRNG(V_, A_, B_) for(int V_=(A_), V_##_END=(B_); V_>=V_##_END; V_--)
#ifdef _WIN32
#define long __int64
#endif
using namespace std;
const int MAXN=5e4+10;
struct Et{ int v,i; };
bool operator>(Et x,Et y){ return abs(x.i)>abs(y.i); }
int n,r; vector<Et> G[MAXN]; pair<int,int> E[MAXN];
Et mx[MAXN],sc[MAXN];
vector<pair<int,int>> G1[MAXN*2];
vector<int> buc[MAXN]; int dis[MAXN*2];
Et f(int u,int v){ return mx[u].v==v?(!sc[u].v?{u,0}:sc[u]):mx[u]; }
void case_main(){
    cin>>n>>r;
    RNG(i,0,n) G[i].clear(),buc[i].clear();
    RNG(i,0,n*2) G1[i].clear();
    fill_n(dis,n*2+1,0x3f3f3f3f);
    RNG(_,1,n-1,u,v) cin>>u>>v,G[u].push_back({v,i}),G[v].push_back({u,-i}),E[i]={u,v};
    RNG(u,1,n){
        Et mx_{0,0},sc_{0,0};
        for(auto [v,i]:G[u]){
            Et x{v,i};
            if(x>mx_) sc_=mx,mx_=x;
            else if(x>sc_) sc_=x;
        }
        mx[u]=mx_,sc[u]=sc_;
    }
    RNG(u,1,n){
        for(auto [v,i]:G[u]){
            auto [w,j]=f(v,u);
            if(j!=0) G1[n+j].push_back({i,1});
            else buc[0].push_back(i),dis[n+i]=0;
            if(w!=u&&f(w,w).v==v){
                if(f(v,v).v==w) G1[n-j].push_back({i,4});
                else G[n-i].push_back({i,3});
            }
        }
    }
    RNG(k,0,n){
        for(int i:buc[k]){
            for(int [j,w]:G1[n+i]){
                if(dis[n+j]>dis[n+i]+w) dis[n+j]=dis[n+i]+w,buc[dis[j]].push_back(j);
            }
        }
    }
    int ans=0;
    if(F(r,r).v==1) ans=dis[F(r,r).v+n];
    else{
        for(int u=r,v;;u=v){
        }
    }
}
int main(){
    int cas; cin>>cas;
    RNG(_,1,cas) case_main();
}