#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include //#include //using Modint = atcoder::modint998244353; using namespace std; typedef long long ll; void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) { if (!b) { d = a; x = 1; y = 0; } else { ex_gcd(b, a % b, d, y, x); y -= a / b * x; } } ll inv(ll a, ll n) { ll d, x, y; ex_gcd(a, n, d, x, y); return d == 1 ? (x % n + n) % (n / d) : -1; } ll gcd(ll x, ll y) { if (y == 0) return x; return gcd(y, x % y); } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } inline void scan_d(int &ret) { char c; ret=0; while((c=getchar())<'0'||c>'9'); while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar(); } const int maxn = 100005; const int mod = 998244353; int ans[maxn]; int main() { #ifdef suiyuan2009 freopen("/Users/suiyuan2009/CLionProjects/icpc/input.cpp", "r", stdin); freopen("/Users/suiyuan2009/CLionProjects/icpc/output.cpp", "w", stdout); #endif int T; scan_d(T); while(T--){ int n,m,K; scan_d(n); scan_d(m); scan_d(K); memset(ans, -1, sizeof(ans)); ans[K] = 0; while(m--){ int x,y; scan_d(x); scan_d(y); if(x==y)continue; if(ans[x]==-1&&ans[y]==-1){ continue; } else if(ans[x]==-1){ ans[x] = ans[y]; ans[y]++; } else if(ans[y]==-1){ ans[y] = ans[x]; ans[x]++; } else { int tx = min(ans[x]+1, ans[y]); int ty = min(ans[y]+1, ans[x]); ans[x] = tx; ans[y] = ty; } } for(int i=1;i<=n;i++){ printf("%d%c", ans[i], i==n?'\n': ' '); } } return 0; }