#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<int> adj[N];
bool vis[N];
int level[N];
queue<int> que;
void bfs(int source){
que.push(source);
vis[source] = true;
while(!que.empty()){
int par = que.front();
que.pop();
for(auto x : adj[par]){
if(vis[x]) continue;
que.push(x);
vis[x] = true;
level[x]=level[par]+1;
}
}
}
int main(){
int t;
cin>>t;
while(t--){
int v,e;
cin >> v >> e;
for(int i=1;i<=e;i++){
int v1,v2;
cin >> v1 >> v2;
adj[v1].push_back(v2);
adj[v2].push_back(v1);
}
int src;
cin>>src;
bfs(src);
for(int i=1;i<=v;i++){
if(i==src)continue;
if(level[i]==0)cout<< "-1"<< " ";
else cout<<level[i]*6<<" ";
}
cout<<endl;
}
}