Go: package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n+1)
	d := make([]int, n+1)
	for i := 0; i < n; i++ {
		var u,k int
		fmt.Scan(&u)
		fmt.Scan(&k)
		g[u] = make([]int, k)
		for j := 0; j < k; j++ {
			fmt.Scan(&g[u][j])
		}

		d[i+1] = -1
	}

	q := [][]int{}
	q = append(q, []int{1, 0})
	for len(q) > 0 {
		pop := q[0]
		if len(q) > 1 {
			q = q[1:]
		} else {
			q = [][]int{}
		}
		if d[pop[0]] >= 0 {
			continue
		}
		d[pop[0]] = pop[1]

		for j := 0; j < len(g[pop[0]]); j++ {
			q = append(q, []int{g[pop[0]][j], pop[1]+1})
		}
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

// 総数を1000000007で割った余り
const long long mod = 1e9 + 7;

using ll  = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
// #define SORT(v, n) stable_sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

const int N     = 100;
const int INFTY = (1 << 21);

int n, M[N][N];
int d[N];  // 距離で訪問状態(color)を管理する。

void bfs(int s) {
    queue<int> q;  //標準ライブラリのqueueを使用。
    q.push(s);
    rep(i, n) d[i] = INFTY;
    d[s]           = 0;
    int u;
    while (!q.empty()) {
        u = q.front();
        q.pop();
        rep(v, n) {
            if (M[u][v] == 0) continue;
            if (d[v] != INFTY) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    rep(i, n) {
        cout << i + 1 << " " << ((d[i] == INFTY) ? (-1) : d[i]) << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // cout << fixed << setprecision(5);

    int u, k, v;
    cin >> n;
    rep(i, n) { rep(j, n) M[i][j] = 0; }
    rep(i, n) {
        cin >> u >> k;
        u--;
        rep(j, k) {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);

    return 0;
}

C++: #include<bits/stdc++.h>
#define INF 10000000
using namespace std;

int z[100],n,w[100];
int a[100][100];

void bfs(){
  int m;
  z[0]=0;
  queue<int> q;
  q.push(0);
  while(!q.empty()){
    m=q.front();
    q.pop();
    if(w[m]==1) continue;
    w[m]=1;
    for(int i=0;i<n;i++){
      if(a[m][i]==1){
	q.push(i);
	z[i]=min(z[i],z[m]+1);
      }
    }
  }
}


int main(){
  int p,x;
  for(int i=0;i<100;i++){
    z[i]=w[i]=INF;
    for(int j=0;j<100;j++) a[i][j]=0;
  }
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>p>>p;
    for(int j=0;j<p;j++){
      cin>>x;
      a[i][x-1]=1;
    }
  }
  bfs();
  for(int i=0;i<n;i++){
    if(z[i]!=INF) cout<<i+1<<" "<<z[i]<<endl;
    else cout<<i+1<<" "<<-1<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
  queue<int> Q;
  int n,u,k,vv,cur,dst;
  cin >> n;
  vector<int> D(n,-1);
  vector< vector<int> > v(n);
  for(int i = 0;i < n;++i){
    cin >> u >> k;
    for(int j = 0;j < k;++j){
      cin >> vv;
      v[u - 1].push_back(vv);
    }
    sort(v[u - 1].begin(),v[u - 1].end());
  }
  D[0] = 0;
  Q.push(1);
  while(!Q.empty()){
    cur = Q.front();
    Q.pop();
    for(int i = 0;i < v[cur - 1].size();++i){
      dst = v[cur - 1][i];
      if(D[dst - 1] == -1) D[dst - 1] = D[cur - 1] + 1;
      Q.push(dst);
    }
  }
  for(int i = 0;i < n;++i)
    cout << i + 1 << " " << D[i] << endl;
}

C++: #include <iostream>
#include <queue> 
using namespace std;

static const int N = 100;
static const int INFTY = (1<<21);

int n,M[N][N];
int d[N];

void bfs(int s){
	queue<int> q;
	q.push(s);
	for(int i=0;i<n;i++) d[i]=INFTY;
	d[s]=0;
	
	int u;
	while(!q.empty()){
		u=q.front();q.pop();
		for(int v=0;v<n;v++){
			if(M[u][v]==0) continue;
			if(d[v] != INFTY) continue;
			d[v]=d[u]+1;
			q.push(v);
		}	
	}
	for(int i=0;i<n;i++){
		cout<<i+1<<" "<<((d[i]==INFTY)?(-1):d[i])<<endl;
	}
} 


int main() {
	int u,k,v;
	
	cin>>n;
	
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			M[i][j]=0;
		}
	}
	
	for(int i=0;i<n;i++){
		cin>>u>>k;
		u--;
		for(int j=0;j<k;j++){
			cin>>v;
			v--;
			M[u][v]=1;
		}
	}
	
	bfs(0);
	
	return 0;
}

C++: #include <iostream>
#include <string>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 100000

static const int N =100;
static const int INFTY =(1<<21);
int n,M[N][N];
int d[N];

void bfs(int s){
  queue<int> q;
  q.push(s);
  for(int i=0;i<n;i++) d[i]=INFTY;
  d[s]=0;
  int u;
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<n;v++){
      if(M[u][v]==0)continue;
      if(d[v]!=INFTY)continue;
        d[v]=d[u]+1;
        q.push(v);
    }
  }
  for(int i=0;i<n;i++){
    cout << i+1 << " " << ((d[i]==INFTY)? (-1):d[i])<< endl;
  }
}

int main(){
  int u,k,v;
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) M[i][j]=0;
  }

  for(int i=0;i<n;i++){
    cin >>u>>k;
    u--;
    for(int j=0;j<k;j++){
      cin >> v;
      v--;
      M[u][v]=1;
    }
  }
  bfs(0);
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000009;
const long long INF = 1LL << 60;

int main() {
    int N;
    cin >> N;
    Graph G(N);
    for (int i = 0; i < N; ++i) {
        int u, k;
        cin >> u >> k;
        --u;
        for (int i = 0; i < k; ++i) {
            int v;
            cin >> v;
            --v;
            G[u].push_back(v);
        }
    }
    vector<int> dist(N, -1);
    dist[0] = 0;
    queue<int> que;
    que.push(0);
    while(!que.empty()) {
        auto q = que.front(); que.pop();
        for (auto&& nq : G[q]) {
            if (dist[nq] != -1) continue;
            dist[nq] = dist[q] + 1;
            que.push(nq);
        }
    }
    for (int i = 0; i < N; ++i)  cout << i+1 << " " << dist[i] << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int G[123][123];
int dis[123];
int n;

struct st {
  int pos;
  int v;
};

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for(int i = 1; i <= n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    for(int j = 0; j < v; j++) {
      int t;
      scanf("%d", &t);
      G[u][t] = 1;
    }
  }

  memset(dis, 0x3f, sizeof dis);

  int start = 1;
  queue<st> q;
  q.push((st) {
    1, 0
  });

  while(!q.empty()) {

    st u = q.front();
    q.pop();
    dis[u.pos] = u.v;

    for(int i = 1; i <= n; i++) {
      if(i == u.pos) continue;
      if(G[i][u.pos] == 0) continue;
      if(dis[i] <= u.v) continue;
      q.push((st){i, u.v + 1});
    }
  }

  for(int i=1;i<=n;i++) {
    printf("%d %d\n", i, dis[i] > 100000 ? -1 : dis[i]);
  }

  return 0;
}

C++: #include <queue>
#include <map>
#include <cstdio>
using namespace std;
main(){
	int t=0,T,i,j,k;
	map<int,vector<int> >a;
	for(scanf("%d",&T);t<T;t++)for(scanf("%d%d",&i,&k);k--;a[i].push_back(j))scanf("%d",&j);
	map<int,int>m;
	queue<int>q;
	for(q.push(1),m[1]=1;!q.empty();){
		k=q.front();q.pop();
		for(i=0;i<a[k].size();i++)if(!m[a[k][i]])m[a[k][i]]=m[k]+1,q.push(a[k][i]);
	}
	for(i=1;i<=T;i++)printf("%d %d\n",i,m[i]-1);
}