#include using namespace std; const int maxn=1e5+100; const int mode=1e9+7; int read(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){ if(ch=='-') f=-1; ch=getchar(); } while(isdigit(ch)){ x=x*10+ch-'0'; ch=getchar(); } return x*f; } int t,n,m,k,dp[maxn],u[maxn],v[maxn]; int main(){ t=read(); while(t--){ memset(dp,0x3f,sizeof(dp)); n=read(); m=read(); k=read(); int now=k; dp[k]=0; for(int i=1;i<=m;++i){ u[i]=read(); v[i]=read(); if(dp[u[i]]dp[v[i]]){ dp[u[i]]=dp[v[i]]; dp[v[i]]++; } } for(int i=1;i<=n;++i){ if(dp[i]>m) cout<<-1; else cout<