#include<bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, m, Distance[N];
vector<int> dsk[N];
bool dd[N];
void bfs(int u) {
queue<int> Q;
Q.push(u);
Distance[u] = 0;
dd[u] = true;
while (Q.size()) {
int u = Q.front();
Q.pop();
/// keo u di
/// dan toi bfs() toi duoc
/// cung bi keo di
for (int v : dsk[u])
if (dd[v] == false) {
dd[v] = true;
Distance[v] = Distance[u] + 1;
Q.push(v);
}
}
}
int main() {
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
int s;
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
dsk[u].push_back(v);
dsk[v].push_back(u);
}
bfs(s);
/// trong so, gia tri dinh
/// a[i].first = a[j].first
/// so sanh a[i].second, a[j].second
vector<pair<int, int>> V;
for (int i = 1; i <= n; i++)
if (dd[i])
V.push_back({Distance[i], i});
sort(V.begin(), V.end());
for (pair<int, int> v : V)
cout << v.second << " " << v.first << endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDEwMDAwMDU7CgppbnQgbiwgbSwgRGlzdGFuY2VbTl07CnZlY3RvcjxpbnQ+IGRza1tOXTsKYm9vbCBkZFtOXTsKCnZvaWQgYmZzKGludCB1KSB7CiAgICBxdWV1ZTxpbnQ+IFE7CiAgICBRLnB1c2godSk7CiAgICBEaXN0YW5jZVt1XSA9IDA7CiAgICBkZFt1XSA9IHRydWU7CgogICAgd2hpbGUgKFEuc2l6ZSgpKSB7CiAgICAgICAgaW50IHUgPSBRLmZyb250KCk7CiAgICAgICAgUS5wb3AoKTsKCiAgICAgICAgLy8vIGtlbyB1IGRpCiAgICAgICAgLy8vIGRhbiB0b2kgYmZzKCkgdG9pIGR1b2MKICAgICAgICAvLy8gY3VuZyBiaSBrZW8gZGkKICAgICAgICBmb3IgKGludCB2IDogZHNrW3VdKQogICAgICAgICAgICBpZiAoZGRbdl0gPT0gZmFsc2UpIHsKICAgICAgICAgICAgICAgIGRkW3ZdID0gdHJ1ZTsKICAgICAgICAgICAgICAgIERpc3RhbmNlW3ZdID0gRGlzdGFuY2VbdV0gKyAxOwogICAgICAgICAgICAgICAgUS5wdXNoKHYpOwogICAgICAgICAgICB9CiAgICB9Cn0KCmludCBtYWluKCkgewovLyAgICBmcmVvcGVuKCJ0ZXN0LmlucCIsICJyIiwgc3RkaW4pOwovLyAgICBmcmVvcGVuKCJ0ZXN0Lm91dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIGludCBzOwogICAgY2luID4+IG4gPj4gbSA+PiBzOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbTsgaSsrKSB7CiAgICAgICAgaW50IHUsIHY7CiAgICAgICAgY2luID4+IHUgPj4gdjsKICAgICAgICBkc2tbdV0ucHVzaF9iYWNrKHYpOwogICAgICAgIGRza1t2XS5wdXNoX2JhY2sodSk7CiAgICB9CgogICAgYmZzKHMpOwogICAgLy8vICAgdHJvbmcgc28sIGdpYSB0cmkgZGluaCAKICAgIAogICAgLy8vIGFbaV0uZmlyc3QgPSBhW2pdLmZpcnN0IAogICAgCiAgICAvLy8gc28gc2FuaCBhW2ldLnNlY29uZCwgYVtqXS5zZWNvbmQgCiAgICAKICAgIHZlY3RvcjxwYWlyPGludCwgaW50Pj4gVjsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICAgICAgaWYgKGRkW2ldKQogICAgICAgICAgICBWLnB1c2hfYmFjayh7RGlzdGFuY2VbaV0sIGl9KTsKCiAgICBzb3J0KFYuYmVnaW4oKSwgVi5lbmQoKSk7CgogICAgZm9yIChwYWlyPGludCwgaW50PiB2IDogVikKICAgICAgICBjb3V0IDw8IHYuc2Vjb25kIDw8ICIgIiA8PCB2LmZpcnN0IDw8IGVuZGw7Cn0KCgo=