// Skyqwq #include #include #include #define pb push_back #define fi first #define se second #define mp make_pair using namespace std; typedef long long LL; template void chkMax(T &x, T y) { if (y > x) x = y; } template void chkMin(T &x, T y) { if (y < x) x = y; } template void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N = 1e5 + 5, INF = 0x3f3f3f3f; int n, m, k, f[N]; int main() { int T; read(T); while (T--) { read(n), read(m), read(k); memset(f, 0x3f, sizeof f); f[k] = 0; while (m--) { int u, v; read(u), read(v); int x = f[u], y = f[v]; swap(f[u], f[v]); chkMin(f[u], x + 1); chkMin(f[v], y + 1); } for (int i = 1; i <= n; i++) { if (f[i] == INF) f[i] = -1; printf("%d", f[i]); if (i < n) printf(" "); } puts(""); } return 0; }