#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct Node
{
int from, to, next;
} node[100010*2];
int head[100010], cnt;
void Add(int a, int b)
{
Node E = {a, b, head[a]};
node[cnt] = E;
head[a] = cnt++;
}
int father[100010], src;
void Deal()
{
queue<int> Q;
Q.push(src);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -1; i = node[i].next)
{
int v = node[i].to;
if(!father[v])
{
father[v] = u;
Q.push(v);
}
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int K; cnt = 0;
scanf("%d %d", &K, &src);
memset(head, -1, sizeof(head));
memset(father, 0, sizeof(father));
for(int i = 0; i < K - 1; i++)
{
int a, b;
scanf("%d %d", &a, &b);
Add(a, b);
Add(b, a);
}
Deal();
for(int i = 1; i <= K; i++)
{
if(i == src)
printf("-1 ");
else
printf("%d ", father[i]);
}
printf("\n");
}
return 0;
}