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 <iostream>
#include <algorithm> // max, min

using namespace std;

const int AllaySize = 1000;
const int Limit = 999999;

int N, P;
int ConnectR[AllaySize][AllaySize];
int InitTTL;
//int Ans[AllaySize][AllaySize];

void war(void) {
  for(int k=1; k<N+1; k++) {
    for(int i=1; i<N+1; i++) {
      for(int j=1; j<N+1; j++) {
	ConnectR[i][j]=min(ConnectR[i][j], ConnectR[i][k]+ConnectR[k][j]);
      }
    }
  } 
}

int main(void) {
  for(int i=0; i<AllaySize; i++) {
    for(int j=0; j<AllaySize; j++) {
      ConnectR[i][j]=Limit;
      // Ans[i][j]=Limit;
    }
  }

  cin>>N;
  for(int i=0; i<N; i++) { // ルータの接続情報
    int k;
    int from, to;
    cin>>from;
    cin>>k;
    for(int j=0; j<k; j++) {
      cin>>to;
      ConnectR[from][to] = 1; // 通過可能
    }
  }

  war();
  cin>>P;
  for(int i=0; i<P; i++) {
    int from, to;
    cin>>from;
    cin>>to;
    cin>>InitTTL;
    if(ConnectR[from][to] < InitTTL) {
      cout<<ConnectR[from][to]+1<<endl; // ?????
    }
    else {
      cout<<"NA"<<endl;
    }
  }
  /*
  for(int i=0; i<N; i++)
    for(int j=0; j<N; j++)
      cout<<ConnectR[i][j]<<" ";
  */
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

int main(){
	int n; cin>>n;
	vector<vector<int> > e;
	vector<int> id(n);
	FOR(i,n){
		cin>>id[i];
		int x; cin>>x;
		e.push_back(vector<int>(x));
		FOR(j,x) cin>>e[i][j];
	}

	map<int,int> mp;
	FOR(i,n) mp[id[i]] = i;
	FOR(i,n) FOR(j,sz(e[i])) e[i][j] = mp[e[i][j]];

	vector<vector<int> > dp(n,vector<int>(n,INF));
	FOR(i,n) FOR(j,sz(e[i])) dp[i][e[i][j]] = 1;

	FOR(k,n) FOR(i,n) FOR(j,n) dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);

	int m; cin>>m;
	while(m--){
		int s,t,tll; cin>>s>>t>>tll;
		s = mp[s],t = mp[t];
		int ans = dp[s][t] + 1;
		if(ans > tll) printf("NA");
		else printf("%d",ans);
		
		if(m)puts("");
	}

	return 0;
}

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

#define INFTY 2000000000

int n,u,k;
int s,d,v,TTL;
int M[101][101],D[101];

void bfs( int s ){
  
  queue <int> Q;
  for( int i = 1; i <= n; i++ ) D[i] = INFTY;

  D[s] = 1;
  Q.push(s);
     
  while( !Q.empty() ){
    u = Q.front(); Q.pop();
    for( int l = 1; l <= n; l++ ){
      if( M[u][l] == 0 ) continue;
      if( D[l] != INFTY ) continue;
      D[l] = D[u] + 1;
      // cout << "l" << " " << l << " " <<  "D[l]" << " " << D[l] << " " <<
      // 	"D[u]" << " " << D[u] << endl;
      Q.push(l);

      if ( D[l] > TTL ){
	cout << "NA" << endl;
	return;
      }else if( l == d ){
	cout << D[l] << endl;
	//	cout << "TTL" << " " << TTL << endl;
	return;
      }

    }

  }
 
}

int main(){
  cin >> n;
  for( int i = 1; i <= n; i++ ){
    cin >> u;
    cin >> k;
    for( int j = 0; j < k; j++ ){
      int v;
      cin >> v;
      M[u][v] = 1;
    }
  }
    
  int p; cin >> p; 
  for( int i = 0; i < p; i++ ){
  cin >> s >> d >> TTL;
  bfs(s);
  }

  return 0;
}

C++: //Name: Packet Transportation
//Level: 2
//Category: グラフ,Graph,全点対間最短距離
//Note:

/**
 * 全点対間の最短距離を求めておけば良い。
 * オーダーは O(N^3)。
 */
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
struct Maybe {/*{{{*/
    T val;
    bool valid;

    Maybe() : valid(false) {}
    Maybe(T &t) : val(t), valid(true) {}

    T& operator =(const T &rv) {
        val = rv;
        valid = true;
        return val;
    }

    operator T() const {
        return valid ? val : T();
    }

    template<typename Cond>
    bool update(const T &v, Cond cond) {
        if(!valid || cond(v, val)) {
            val = v;
            valid = true;
            return true;
        }
        return false;
    }

    bool update(const T &v) {
        return update(v, less<T>());
    }
};/*}}}*/

template<typename T>
struct WarshallFloyd {
    vector<vector<Maybe<T>>> graph;
    int N;

    WarshallFloyd(int size) : graph(size), N(size) {
        for(auto &v : graph) {
            v.resize(size);
        }
    }

    void add_edge(int from, int to, const T &dist) {
        graph[from][to] = dist;
    }

    void freeze() {
        for(int k = 0; k < N; ++k) {
            for(int i = 0; i < N; ++i) {
                for(int j = 0; j < N; ++j) {
                    if(graph[i][k].valid && graph[k][j].valid) {
                        graph[i][j].update(graph[i][k] + graph[k][j]);
                    }
                }
            }
        }
    }

    const Maybe<T>& query(int from, int to) {
        return graph[from][to];
    }
};

bool solve(bool first) {
    int N;
    if(!(cin >> N)) return false;
    if(!N) return false;

    WarshallFloyd<int> wf(N);
    for(int i = 0; i < N; ++i) {
        int from, k;
        cin >> from >> k;
        --from;
        while(k--) {
            int v;
            cin >> v;
            wf.add_edge(from, v-1, 1);
        }
    }
    wf.freeze();
    int P;
    cin >> P;
    while(P--) {
        int s, d, t;
        cin >> s >> d >> t;
        const auto &dist = wf.query(s-1, d-1);
        if(dist.valid && dist+1 <= t) {
            cout << dist+1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout.setf(ios::fixed);
    cout.precision(2);

    bool first = true;
    while(solve(first)) {
        first = false;
    }
    return 0;
}