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>
using namespace std;

int main(){
	int n,i,j,k,a,b,c,cost[101][101];
	char cc;
	cin>>n;
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++) cost[i][j] = 1<<15;
		cin>>a>>b;
		for(j=0;j<b;j++){
			cin >> c;
			cost[a][c]=1;
		}
	}
	for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)
		cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a>>b>>c;
		if(cost[a][b]<c && cost[a][b]<1<<15) cout << cost[a][b]+1 << endl;
		else cout << "NA" << endl;
	}
}

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]) {
				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 <queue>
using namespace std;

#define INFTY 10000000

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[d] > TTL ){
    cout << "NA" << endl;
    return;
  }else{
    cout << D[d] << 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++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e9
#define rep(i,n) for(int i=0;i<n;i++)
struct edge{int to,cost;};
typedef pair<int,int> P;

int V,n,r,k,t,p,s,d,v,D[1001];
vector<edge> g[1001];

int main(){
	while(cin>>n&&n){
		rep(i,n)g[i].clear();
		rep(i,n){
			cin>>r>>k;
			rep(j,k){
				edge e;
				cin>>t;
				e.to=t;
				e.cost=1;
				g[r].push_back(e);
			}
		}
		cin>>p;
		rep(h,p){
			cin>>s>>d>>v;
			priority_queue<P,vector<P>,greater<P> > q;
			fill(D,D+n+1,INF);
			D[s]=0;
			q.push(P(0,s));
			while(!q.empty()){
				P pp=q.top();q.pop();
				int vv=pp.second;
				if(D[vv]<pp.first)continue;
				rep(j,g[vv].size()){
					edge e=g[vv][j];
					if(D[e.to]>D[vv]+e.cost){
						D[e.to]=D[vv]+e.cost;
						q.push(P(D[e.to],e.to));
					}
				}
			}
			if(D[d]<v)cout<<D[d]+1<<endl;
			else cout<<"NA"<<endl;
		}
	}
}

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

using namespace std;


static const int NO = -2;
static const int N = 100;


int graph[N + 1][N + 1] = {{}};
int dis[N + 1] = {};
int n;

void bfs(int v){
  queue<int> Q;

  Q.push(v);

  for(int i = 1; i <= N; i++){
    dis[i] = NO;
  }

  dis[v] = 1;

  int u;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();

    for(int i = 1;i <= n; i++){
      if(graph[u][i]==0) continue;
      if(dis[i]!=NO)continue;
      dis[i] = dis[u] + 1;
      Q.push(i);

    }
  }
}

int main(){

  int r[N+1] = {};
  int d[N+1] = {};
  int k[N+1] = {};
  int f[N+1] = {};
  int p,s[N+1] = {};
  int v[N+1] = {};


  //in
  cin >> n;

  for(int i = 1;i <= n; i++){

    cin >> r[i] >> k[i];

    for(int j = 1;j <= k[i];j++){

      cin >> f[j];
      graph[i][f[j]] = 1;
    }
  }

  cin >> p;
  for(int i = 1;i <= p; i++){
    cin >> s[i] >> d[i] >> v[i];
    bfs(s[i]);

    if(v[i] >= dis[d[i]]) cout << dis[d[i]] << endl;
    else cout << "NA" << endl;
  }
}

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

const int INF = 10000;
const int VMAX = 108;

int main() {
  int n;
  cin >> n;
  int G[VMAX][VMAX];
  for (int i = 0; i < VMAX; i++) {
    for (int j = 0; j < VMAX; j++) {
      G[i][j] = (i == j ? 0 : INF);
    }
  }

  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    while (k > 0) {
      int t;
      cin >> t;
      G[r][t] = 1;
      k--;
    }
  }

  for (int k = 0; k < VMAX; k++) {
    for (int i = 0; i < VMAX; i++) {
      for (int j = 0; j < VMAX; j++) {
        G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
      }
    }
  }

  int p;
  cin >> p;
  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    if (G[s][d] + 1 > v) {
      cout << "NA" << endl;
    } else {
      cout << G[s][d] + 1 << endl;
    }
  }
 
  return 0;
}