#include const int Inf = 0x3f3f3f3f; const int MN = 100005; const int MM = 100005; int N, M, K; int dis[MN]; inline void Solve() { scanf("%d%d%d", &N, &M, &K); for (int i = 1; i <= N; ++i) dis[i] = Inf; dis[K] = 0; for (int i = 1; i <= M; ++i) { int u, v; scanf("%d%d", &u, &v); if (dis[u] > dis[v]) dis[u] = dis[v]++; else if (dis[u] < dis[v]) dis[v] = dis[u]++; } for (int i = 1; i <= N; ++i) printf("%d%c", dis[i] == Inf ? -1 : dis[i], " \n"[i == N]); } int main() { int Tests; scanf("%d", &Tests); while (Tests--) Solve(); return 0; }