Go: package main

import "fmt"

type container struct {
	data []int
}

func (c *container) queue(v int) {
	c.data = append(c.data, v)
}
func (c *container) dequeue(p *int) bool {
	if p == nil || len(c.data) == 0 {
		return false
	}
	*p = c.data[0]
	c.data = c.data[1:len(c.data)]
	return true
}

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

	var q container
	d[1] = 0
	q.queue(1)

for len(q.data) > 0  { // 幅優先探索
		var v int
		q.dequeue(&v)
		for i := 0; i < len(g[v]); i++ {
			nv := g[v][i]
		if d[nv] == -1 { // 未訪問
				q.queue(nv)
				d[nv] = d[v] + 1
			}
		}
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d %d\n", i, d[i])
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main() {
    ll n;
    cin >> n;
    vector<vector<ll>> G(n);
    rep(i, n) {
        ll u, k;
        cin >> u >> k;
        u--;
        rep(j, k) {
            ll v;
            cin >> v;
            v--;
            G[u].emplace_back(v);
        }
    }
    vector<ll> dist(n, -1);
    dist[0] = 0;

    queue<ll> que;
    que.push(0);

    while(!que.empty()) {
        auto v = que.front();
        que.pop();

        for(auto nv : G[v]) {
            if (dist[nv] != -1) continue;
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
    rep(i, n) cout << i+1 << " " << dist[i] << endl;
}

C++: #include <bits/stdc++.h>
#include<math.h>
#define rep(i,n) for (int i = 0; i < (n) ; ++i)
using namespace std;
using ll = long long ;
using P = pair<int , int> ;
#define PI 3.14159265358979323846264338327950
#define INF 1e18 

int main() {
    int n ;
    cin >> n ;
    vector<int> list [n] ;
    rep(i, n){
        int v, k;
        cin >> v >> k ;
        v-- ;
        rep(j, k){
            int g ;
            cin >> g  ;
            g-- ;
            list[v].push_back(g) ;
        }
    }
    queue<P> que ;
    vector<int> d (n, -1) ;
    que.push(P(0, 0)) ;
    while(!que.empty()) {
        P st = que.front() ;
        que.pop() ;
        if(d[st.first] > -1){
            continue ;
        }
        else {
            int f = st.first ;
            int e = st.second ;
            d[f] = e ;
            for(int u : list[f]){
                que.push(P(u, e + 1)) ;
            }
        }
 
    }
    rep(i, n){
        cout << i + 1 << " " << d[i] << endl ;
    }
}

C++: #include <iostream>
#include <iomanip>
#include <utility>
#include <cmath>
#include <random>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;

#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
typedef long long ll;
typedef pair<int,int> ip;
typedef pair<ll,ll> lp;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;

int main(){
	int n;
	cin>>n;
	vector<int> ans(110,-1);
	vector<vector<int>> input (110);
	REP(i,n){
		int el_siz;
		cin>>el_siz>>el_siz;
		vector<int> el(el_siz);
		rep(j,el_siz)cin>>el[j];
		input[i] = el;
	}
	queue<int> Q;
	int cnt = 0;
	ans[1] = 0;
	Q.push(1);
	while(!Q.empty()){
		int node = Q.front();
		Q.pop();
		rep(i,input[node].size()){
			if(ans[input[node][i]] == -1){
				ans[input[node][i]] = ans[node]+1;
				Q.push(input[node][i]);
			}
		}
	}
	REP(i,n)cout<<i<<' '<<ans[i]<<endl;
	return 0;
}

C++: #include <iostream>
#include <queue>

int n;

int V[100][100];
int D[100];
int d = 0;

std::queue<int> Q;

void bfs(int r) {
    Q.push(r);
    D[r] = 0;
    while (Q.size()) {
        r = Q.front();
        Q.pop();
        for (int i = 0; i < n; i++) {
            if ((D[i] == -1) && (V[r][i] == 1)) {
                Q.push(i);
                D[i] = D[r] + 1;
            }
        }
    }
}


int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        int id, e_num;
        std::cin >> id >> e_num;
        for (int j = 0; j < e_num; j++) {
            int t;
            std::cin >> t;
            V[id - 1][t - 1] = 1;
        }
    }

    for (int i = 0; i < n; i++) {
        D[i] = -1;
    }

    bfs(0);
    for (int i = 0; i < n; i++) {
        std::cout << i + 1 << " " << D[i] << std::endl;
    }
}