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<cstdio>
#include<cstdlib>
#include<queue>
using namespace std;


int net[110][110],n;

typedef pair<int,int> BVB;


int ans(int,int,int);

void debug();

int main(){

  for(int k=0;k<110;k++)for(int l=0;l<110;l++)net[k][l]=-1; //Syokika

  int i,j,p,num,n1,th;

  cin >> n;

  for(i=0;i<n;i++){ //net's info
    
    cin >> num >> n1;



    for(int k=0;k<110;k++)net[num][k]=0;    //siyoukanou

    for(j=0;j<n1;j++){
      cin >> th;
      net[num][th]=1;
    }

  }//net's info

  int pkn;

  cin >> pkn;

  int st,ed,c,answer;

  // debug();



  for(i=0;i<pkn;i++){//pack's info
    cin >> st >> ed >> c;


    answer = ans(st,ed,c);

    if(answer != -2)cout << answer << endl;
    else cout << "NA" << endl;

  }//pack's info



}



//L[(ÊÌzñÅà¢¢¯Ç)ÉÇñÇñüêÄ¢«TTLªO¢ÉÈéOÉÚInÉBÅ«é©Ç¤©ð²×é
//[^ÍÚInÉBµ½n_ÌTTL©çvZµÄ¾·
int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int i,j,k,l,cal=0,mem=0;
  BVB next,fr;

  fr.first=st;
  fr.second=c;
  Nari.push(fr);
  /*
  cout << "FIRST next.second = " << c << endl;  
  cout << "FIRST cal = " << cal << endl;
  */  

  if(st == ed && c != 0)return 1;


  while(1){
    next=Nari.front();
    Nari.pop();
    next.second=next.second-1;

    if(fr.second == (next.second+1) && next.second == 0 && next.first != ed)return -2;

    if(next.second < 0)break; //I¹ð
    //cout << "next.second = " << next.second << endl; 
    
    for(i=1;i<=110;i++){//¢¯éêðL[Éüêé
      if(net[next.first][i] == 1)mem++;  

    
      
      if(net[next.first][i] == 1 && i == ed ){ //ÚInB
	/*	
	cout << "GOOOOOOOOOL!!! i = " << i <<endl;
	cout << "             cal = " << cal << endl;
	cout << "     next.second = " << next.second << endl; 
	*/

	return (fr.second - next.second)+1;
	
      }
      
      if(net[next.first][i] == 1)Nari.push(BVB(i,next.second));
      
      if(mem == n)break;
    }
      
    
  }

  return -2;
  
  
  
}










void debug(){
  for(int i=1;i<=10;i++){
    if(i==1){
      cout << "   ";
      for(int k=1;k<=10;k++){
	printf("%3d",k);
      }
      cout << endl;
      for(int k=1;k<=10;k++)printf("___");
      cout << endl;
    }

    for(int j=1;j<=10;j++){
      if(j == 1)cout << i << " |";
      printf("%3d",net[i][j]);
    }
    cout << endl;

  }



}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n; cin>>n;
    vector<vector<pii>>edge(n,vector<pii>());
    rep(i,n){
        int r; cin>>r;
        --r;
        int k; cin>>k;
        rep(j,k){
            int to; cin>>to;
            edge[r].push_back(pii(to-1,1));
        }
    }
    int p; cin>>p;
    rep(_,p){
        priority_queue<pii,vector<pii>,greater<pii>>que;
        int from,to,cost; cin>>from>>to>>cost;
        --to,--from;
        vector<int>d(n,INF);
        d[from]=0;
        que.push(pii(0,from));
        while(!que.empty()){
            pii now=que.top(); que.pop();
            if(now.second==to)break;
            if(d[now.second]<now.first)continue;
            for(int i=0; i<edge[now.second].size();i++){
                pii next=edge[now.second][i];
                if(d[next.first]<now.first+next.second)continue;
                d[next.first]=now.first+next.second;
                que.push(pii(now.first+next.second,next.first));
            }
        }
        if(d[to]<cost)cout<<d[to]+1<<endl;
        else cout<<"NA"<<endl;
    }
}

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[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++: #include<iostream>
#include<queue>
using namespace std;

#define MAX_N 110

#define INF 999999999

int x[MAX_N][MAX_N];
int d[MAX_N];

queue<int>Q;

int bfs(int s, int t){
	for(int i=0;i<MAX_N;i++){
		d[i]=INF;
	}
	d[s]=0;
	Q.push(s);
	while(!Q.empty()){
		int z=Q.front();
		for(int i=0;i<MAX_N;i++){
			if(x[z][i]<INF){
				if(d[i]==INF){
					d[i]=d[z]+1;
				}
			}
		}
		Q.pop();
	}
	return d[t];
}

int main(){
	int n,m,a,b,c;
	for(int i=0;i<MAX_N;i++){
		for(int j=0;j<MAX_N;j++){
			x[i][j]=INF;
		}
	}
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		for(int j=0;j<b;j++){
			cin>>c;
			x[a][c]=1;
		}
	}
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b>>c;
		int y=bfs(a,b);
		if(y>=c){cout<<"NA"<<endl;}
		else{cout<<c-y<<endl;}
	}
	return 0;
}