Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 1<<31

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			arr[i][j] = INF
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		x, k := input[0]-1, input[1]
		for j := 0; j < k; j++ {
			y := input[j+2]-1
			arr[x][y] = 1
		}
	}
	for k := 0; k < n; k++ {
		arr[k][k] = 0
		for i := 0; i < n; i++ {
			if arr[i][k] >= INF {
				continue
			}
			for j := 0; j < n; j++ {
				if arr[k][j] >= INF {
					continue
				}
				arr[i][j] = min(arr[i][j], arr[i][k]+arr[k][j])
			}
		}
	}
	scanner.Scan()
	p, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < p; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		s, d, v := input[0]-1, input[1]-1, input[2]
		if arr[s][d] < v {
			fmt.Println(arr[s][d]+1)
		} else {
			fmt.Println("NA")
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <deque>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;

class Node {
public:
	int pos, ttl;
	Node(int p, int t) : pos(p), ttl(t) {}
};

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
bool visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
	deque<Node> que;
	fill(visited, visited + n, false);
	que.push_back(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop_front();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (visited[current.pos]) continue;
		visited[current.pos] = true;
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next] && !visited[current.pos]) {
				que.push_back(Node(next, current.ttl - 1));
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k;
		cin >> r >> k;
		fill(adj[r], adj[r] + n, false);
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			adj[r][t] = true;
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		bfs(s, d, v);
	}
	return 0;
}

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

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back 

int bfs(int s, int t, int c);

vector<vector<int> > g(101);

int main()
{
    int n;
    cin >> n;
    loop(i, n) {
        int r, k, t;
        cin >> r >> k;
        loop(j, k) {
            cin >> t;
            g[r].pb(t);
        }
    }
    int p;
    cin >> p;
    loop (i, p) {
        int s, d, v;
        cin >> s >> d >> v;
        int ans = bfs(s, d, v);
        if (ans != -1) {
            cout << v-ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

int bfs(int s, int t, int c)
{
    vector<bool> check(101, false);
    queue<int> fq, nq, sq;
    fq.push(s);
    while (1) {
        c--;
        while (!fq.empty()) {
            int pos = fq.front(); fq.pop();
            if (check[pos]) {
                continue;
            } else {
                check[pos] = true;
            }
            //cout << pos << endl;
            if (pos == t) {
                return c;
            } else {
                for (int i: g[pos]) {
                    nq.push(i);
                }
            }
        }
        if (c == 0 || nq.empty()) {
            return -1;
        }
        fq = nq;
        nq = sq;
    }
}

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

int main() {
  static int const MaxElement = 100;
  std::array<std::array<bool, MaxElement>, MaxElement> map = {};

  int n = 0;
  std::cin >> n;
  for( int i=0; i<n; ++i ) {
	int r = 0, k = 0;
	std::cin >> r >> k;
	for( int j=0; j<k; ++j ) {
	  int idx = 0;
	  std::cin >> idx;
	  map[r-1][idx-1] = true;
	}
  }

  int m = 0;
  std::cin >> m;
  for( int i=0; i<m; ++i ) {
	int src = 0, dst = 0, ttl = 0;
	std::cin >> src >> dst >> ttl;

	std::array<int, MaxElement> distance;
	distance.fill( -1 );

	std::queue<int> q;
	q.push( src - 1 );
	distance[src-1] = 0;
	while( !q.empty() ) {
	  int const u = q.front();
	  q.pop();
	  for( int v=0; v<n; ++v ) {
		if ( map[u][v] && distance[v] == -1 ) {
		  distance[v] = distance[u] + 1;
		  q.push( v );
		}
	  }
	}

	if ( distance[dst-1] == -1 || ttl - distance[dst-1] <= 0 ) {
	  std::cout << "NA" << std::endl;
	} else {
	  std::cout <<  distance[dst-1] + 1 << std::endl;
	}
  }
}

C++: #include <iostream>
#include <set>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;

int solve( int table[ SIZE ][ SIZE ], int s, int e, int initTtl, int numRooter )
{
	if( initTtl < 2 ){
		return 0;
	}
	bool bPast[ SIZE ];
	memset( bPast, 0, sizeof( bPast ) );
	bPast[ s ] = true;
	vector< int > pList;
	for( int i = 1; i <= numRooter; ++i ){
		if( table[ s ][ i ] ){
			if( i == e ){
				return 2;
			}
			bPast[ i ] = true;
			pList.push_back( i );
		}
	}

	int ttl = initTtl;
	while( pList.size() > 0 ){
		--ttl;
		if( ttl < 1 ){
			return 0;
		}
		vector< int > pList2;
		for( int i = 0; i < pList.size(); ++i ){
			int p = pList[ i ];
			if( p == e ){
				return initTtl - ttl + 1;
			}
			for( int j = 1; j <= numRooter; ++j ){
				if( table[ p ][ j ] && !bPast[ j ] ){
					bPast[ j ] = true;
					pList2.push_back( j );
				}
			}
		}
		pList = pList2;
	}

	return 0;
}

int main()
{
	int table[ SIZE ][ SIZE ];

	int n;
	while( cin >> n ){
		cin.ignore( 100, '\n' );
		memset( table, 0, sizeof(table) );
		string s;
		for( int i = 0; i < n; ++i ){
			getline( cin, s );
			int o = s[ 0 ] - '0';
			for( int j = 4; j < s.size(); j += 2 ){
				table[ o ][ s[ j ] - '0' ] = true;
			}
		}
		int p;
		cin >> p;
		for( int i = 0; i < p; ++i ){
			int from, to, ttl;
			cin >> from >> to >> ttl;
			int result = solve( table, from, to, ttl, n );
			if( result > 0 ){
				cout << result << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}

	return 0;
}