#include using namespace std; int ans[100005]; int main() { int T; scanf("%d", &T); while (T--) { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) ans[i] = 1e9; ans[k] = 0; for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); int a = ans[u], b = ans[v]; ans[v] = min(b + 1, a); ans[u] = min(a + 1, b); } for (int i = 1; i <= n; ++i) { if (ans[i] > m) printf("-1"); else printf("%d", ans[i]); putchar(i == n ? '\n' : ' '); } } }