#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define dbg(x) cout << #x << " : " << x << endl
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define inf 1000000000000000000
#define maxn 100005
ll n, m, s, d[maxn] = {inf};
ll dau[maxn] = {0};
vector<ll> edges[maxn];
vector<pair<ll, ll>> res;
queue<ll> q;
void bfs(ll source)
{
d[source] = 0;
q.push(source);
dau[source] = 1;
while (!q.empty())
{
ll p = q.front();
q.pop();
for (auto ke : edges[p])
{
if (dau[ke] == 1)
continue;
d[ke] = min(d[ke], d[p] + 1);
q.push(ke);
dau[ke] = 1;
}
}
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n >> m >> s;
s--;
ll i;
for (i = 0; i < m; i++)
{
ll u, v;
cin >> u >> v;
u--, v--;
edges[u].push_back(v);
edges[v].push_back(u);
}
// for(i=0;i<n;i++){
// cout<<i+1<<"-->";
// for(auto v: edges[i]) cout<<v+1<<" ";
// cout<<'\n';
// }
for (i = 0; i < n; i++)
d[i] = inf;
bfs(s);
for (i = 0; i < n; i++)
res.push_back({d[i], i});
// cout<<res.size()<<'\n';
sort(res.begin(), res.end());
for (auto p : res)
{
if (p.first == inf)
continue;
cout << p.second + 1 << " " << p.first << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZGJnKHgpIGNvdXQgPDwgI3ggPDwgIiA6ICIgPDwgeCA8PCBlbmRsCiNkZWZpbmUgcmVwKGksIGEsIGIpIGZvciAoaW50IGkgPSAoYSk7IGkgPD0gKGIpOyBpKyspCiNkZWZpbmUgaW5mIDEwMDAwMDAwMDAwMDAwMDAwMDAKI2RlZmluZSBtYXhuIDEwMDAwNQpsbCBuLCBtLCBzLCBkW21heG5dID0ge2luZn07CmxsIGRhdVttYXhuXSA9IHswfTsKdmVjdG9yPGxsPiBlZGdlc1ttYXhuXTsKdmVjdG9yPHBhaXI8bGwsIGxsPj4gcmVzOwpxdWV1ZTxsbD4gcTsKdm9pZCBiZnMobGwgc291cmNlKQp7CiAgICBkW3NvdXJjZV0gPSAwOwogICAgcS5wdXNoKHNvdXJjZSk7CiAgICBkYXVbc291cmNlXSA9IDE7CiAgICB3aGlsZSAoIXEuZW1wdHkoKSkKICAgIHsKICAgICAgICBsbCBwID0gcS5mcm9udCgpOwogICAgICAgIHEucG9wKCk7CiAgICAgICAgZm9yIChhdXRvIGtlIDogZWRnZXNbcF0pCiAgICAgICAgewogICAgICAgICAgICBpZiAoZGF1W2tlXSA9PSAxKQogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIGRba2VdID0gbWluKGRba2VdLCBkW3BdICsgMSk7CiAgICAgICAgICAgIHEucHVzaChrZSk7CiAgICAgICAgICAgIGRhdVtrZV0gPSAxOwogICAgICAgIH0KICAgIH0KfQppbnQgbWFpbigpCnsKICAgIC8vIGZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKTsKICAgIC8vIGZyZW9wZW4oIm91dHB1dC50eHQiLCJ3IixzdGRvdXQpOwogICAgY2luID4+IG4gPj4gbSA+PiBzOwogICAgcy0tOwogICAgbGwgaTsKICAgIGZvciAoaSA9IDA7IGkgPCBtOyBpKyspCiAgICB7CiAgICAgICAgbGwgdSwgdjsKICAgICAgICBjaW4gPj4gdSA+PiB2OwogICAgICAgIHUtLSwgdi0tOwogICAgICAgIGVkZ2VzW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICBlZGdlc1t2XS5wdXNoX2JhY2sodSk7CiAgICB9CiAgICAvLyBmb3IoaT0wO2k8bjtpKyspewogICAgLy8gICAgIGNvdXQ8PGkrMTw8Ii0tPiI7CiAgICAvLyAgICAgZm9yKGF1dG8gdjogZWRnZXNbaV0pIGNvdXQ8PHYrMTw8IiAiOwogICAgLy8gICAgIGNvdXQ8PCdcbic7CiAgICAvLyB9CiAgICBm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