#include <iostream>
#include<vector>
#Include<cstring>
using namespace std;
vector<int> m[100005];
int a[100005];
void f(int k)
{ for(int i=0;i<m[k].size();i++)
{
if(a[m[k][i]]==0)
{ a[m[k][i]]=k;
f(m[k][i]);
}
}
}
int main(void)
{
int t,n,k,i,x,y;
cin>>t;
while(t--)
{ memset(a,0,sizeof(a));
memset(m,0,sizeof(m));
i=1; cin>>n>>k;
while(i<n)
{
cin>>x>>y;
m[x].push_back(y);
m[y].push_back(x);
i++;
}
a[k]=-1;
f(k);
for(i=1;i<n;i++)
cout<<a[i]<<" ";
cout<<a[i]<<endl;
}
}