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++: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF= 1 << 28;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)

double cross(const xy_t &a, const xy_t &b){
	return imag(conj(a) * b);
}

double dot(const xy_t &a, const xy_t &b){
	return real(conj(a) * b);
}

xy_t projection(const line &l, const xy_t &p){
	double t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
	return l.first + t * (l.first - l.second);
}

int ccw(xy_t a, xy_t b, xy_t c){
	b -= a;
	c -= a;
	if(cross(b, c) > 0) return 1;
	if(cross(b, c) < 0) return -1;
	if(dot(b, c) < 0) return 2;
	if(norm(b) < norm(c)) return -2;
	return 0;
}

bool intersectSP(const line &l, const xy_t &p){
	return abs(l.first- p) + abs(l.second - p) - abs(l.first - l.second) < EPS;
}

bool intersectSS(const line &s, const line &t){
	return ccw(s.first, s.second, t.first) * ccw(s.first, s.second, t.second) <= 0 &&
		ccw(t.first, t.second, s.first) * ccw(t.first, t.second, s.second) <= 0;
}

double distanceSP(line s, xy_t p){
	const xy_t r = projection(s, p);
	if(intersectSP(s, r)) return abs(r - p);
	return min(abs(s.first - p), abs(s.second - p));
}

int g[101][101];

int main(){
	int n, p;
	int s, d, v;
	while(cin >> n && n){
		fill(&g[0][0], &g[100][100] + 1, INF);
		rep(i, 101) g[i][i] = 0;
		rep(i, n){
			int a, b, k;
			cin >> a >> k;
			rep(j, k){
				cin >> b;
				g[a][b] = 1;
			}
		}
		rep(k, 101){
			rep(i, 101){
				rep(j, 101){
					g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
				}
			}
		}
		cin >> p;
		rep(i, p){
			cin >> s >> d >> v;
			int cost = g[s][d];
			if(cost < v){
				cout << cost + 1 << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++)
#define MAX 100
#define INF (1<<21)
int n, L[MAX][MAX], S[MAX];

int bfs( int s, int t ){
    queue<int> q; q.push(s);
    int u, v, d[MAX];
    rep(i, n) d[i] = INF;
    d[s] = 1;
    while( !q.empty() ){
	u = q.front(); q.pop();
	rep(i, S[u]){
	    v = L[u][i];
	    if ( d[v] == INF ){
		d[v] = d[u] + 1;
		q.push(v);
	    }
	}
    }
    return d[t];
}

main(){
    int s, t, tl, q, c;
    cin >> n;
    rep(i, n){
	cin >> s; s--; cin >> S[s];
	rep(j, S[s]){
	    cin >> L[s][j]; L[s][j]--;
	}
    }
    cin >> q;
    rep(i, q){
	cin >> s >> t >> tl;
	c = bfs(s-1, t-1);
	if ( c <= tl ) cout << c << endl;
	else cout << "NA" << endl;
    }
}

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

#define INF 10000

int n,m,k,s;
int t[100][100];

int main()
{
	cin >> n;

	for(int i=0; i<100; i++)
	for(int j=0; j<100; j++)
		t[i][j] = INF;

	for(int i=0; i<100; i++)
		t[i][i] = 0;

	for(int i=0; i<n; i++)
	{
		cin >> k >> m;
		for(int j=0; j<m; j++)
		{
			cin >> s;
			t[k-1][s-1] = 1;
		}
	}

	for(int k=0; k<n; k++)
	for(int i=0; i<n; i++)
	for(int j=0; j<n; j++)
	{
		if(t[i][j]>t[i][k]+t[k][j]) t[i][j] = t[i][k]+t[k][j];
	}

	cin >> n;
	while(n--)
	{
		cin >> k >> s >> m;
		if(t[k-1][s-1]+1<=m)
		{
			cout << t[k-1][s-1]+1 << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int main(){
    int n; cin >>n;
    vector< vector<int> > v(n, vector<int>());
    REP(i, n){
        int s, nn; cin >>s >>nn; s--;
        REP(j, nn){
            int e; cin >>e; e--;
            v[s].push_back(e);
        }
    }

    int c; cin >>c;
    REP(i, c){
        int s, e, t; cin >>s >>e >>t; s--; e--;
        queue<P> open;
        open.push( P(s, t) );
        vector<int> closed(n, -1);

        while(!open.empty()){
            P now = open.front(); open.pop();
            int place = now.first, ttl = now.second;
            if(ttl <= 0 || ttl <= closed[place]) continue;
            closed[place] = ttl;
            int s = v[place].size();
            REP(i, s) open.push( P(v[place][i], ttl - 1) );
        }
        if(closed[e] == -1) cout <<"NA" <<endl;
        else cout <<t - closed[e] + 1 <<endl;
    }
    return 0;
}