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

#define N 101

int M[N][N];//&#65533;&#1488;&#1677;s&#65533;
int d[N];//&#65533;m&#65533;[&#65533;h&#65533;&#784;&#65533;
int pi[N];//&#65533;&#450;&#65533;&#65533;&#65533;&#65533;&#30152;&#65533;&#65533;&#65533;&#65533;

void initi();
int BFS(int start,int goal);


main(){
  initi();
  int n;
  cin >> n;
  for(int i = 0; i< n ; i++){
    int Bangou,setuzokusu;
    cin >> Bangou;
    cin >> setuzokusu;
    for(int j=0;j < setuzokusu ; j++){
      int to;
      cin >> to;
      M[Bangou][to]=1;
    }
  }//&#65533;f&#65533;[&#65533;^&#65533;&#787;&#65533;&#65533;&#834;&#65533;&#65533;&#65533;&#65533;&#1794;
  
  
  cin >> n;
  for(int i = 0; i < n;i++){
    int start,goal,TTL,result;
    cin >> start >> goal >> TTL;
    if ( (result =BFS(start,goal)+1) > TTL)cout << "NA" << endl;
    else cout << result << endl;
  }
  
  
  
  
  return 0;     
}


void initi(){
        for(int i=0;i<N;i++){
                for(int j=0;j<N;j++)M[i][j]=0;
        }
}

int BFS(int start,int goal){
        
  queue<int> Q;//&#65533;L&#65533;&#65533;&#65533;[&#65533;&#65533;&#65533;p&#65533;
  for(int i=0;i<N;i++){
    d[i] =1 << 21;
    pi[i]=0;
  }
  
  d[start]=0;
  Q.push(start);
  
  while(!Q.empty()){//&#65533;L&#65533;&#65533;&#65533;[&#65533;&#65533;&#65533;&#65533;&#65533;&#578;&#514;&#65533;&#65533;&#1794;&#322;&#65533;&#65533;
    int u=Q.front();
    Q.pop();
    //  cout << "Now u is" << u << endl;
    for(int v = 0 ; v <N ; v++){
      if (M[u][v] && d[v] == 1 << 21){//u&#65533;&#65533;&#65533;&#65533;v&#65533;&#589;s&#65533;&#65533;&#65533;&#269;X&#65533;&#65533;v&#65533;&#771;X&#65533;^&#65533;[&#65533;g&#65533;n&#65533;_&#65533;&#65533;&#65533;&#65533;&#65533;&#779;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#337;&#65533;&#65533;&#782;&#65533;
        d[v]=d[u]+1;
        pi[v]=u;//&#65533;&#450;&#65533;&#65533;&#65533;&#65533;&#30152;&#65533;&#65533;&#65533;&#65533;
        Q.push(v);
        //cout <<"from "  <<u << "to" <<v << endl;
      }
    }
  }

  //for(int i=1;i<8;i++)cout << d[i]<< " ";
  //cout << endl;
  
  
  return d[goal];
        
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

struct Packet{
	Packet(){
		ttl = current = 0;
	}

	void copy(Packet t){
		ttl = t.ttl;
		current = t.current;
		for(int i=0; i < 101; i++)checkTable[i] = t.checkTable[i];
	}

	Packet(int arg_ttl,int loc){
		ttl = arg_ttl;
		for(int i=0; i < 101; i++)checkTable[i] = 0;
		checkTable[loc] = 1;
		current = loc;
	}

	void set(int loc){
		checkTable[loc] = 1;
		current = loc;
	}

	int ttl,checkTable[101],current;
};

int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit;
	Packet tmp;

	for(int i=1; i < 101; i++){
		for(int k=1; k < 101; k++){
			table[i][k] = 0;
		}
	}

	scanf("%d",&n);

	for(int i = 0; i < n; i++){
		scanf("%d %d",&r,&k);
		for(int p=0;p < k; p++){
			scanf("%d",&to);
			table[r][to] = 1;
		}
	}

	scanf("%d",&num_of_packet);

	queue<Packet> Q;
	bool FLG;

	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		FLG = false;

		if(t_limit <= 1){
			printf("NA\n");
			FLG = true;
		}

		if(!FLG){
			for(int d = 1; d <= n; d++){
				if(d != start && table[start][d] == 1){
					Packet p(t_limit-1,start);
					p.set(d);
					Q.push(p);
				}
			}
		}

		while(!Q.empty()){
			tmp.copy(Q.front());
			Q.pop();

			if(tmp.ttl > 0 && tmp.current == goal){
				printf("%d\n",t_limit - tmp.ttl+1);
				FLG = true;

				while(!Q.empty())Q.pop();
				
				break;
			}

			if(tmp.ttl > 1 && t_limit-tmp.ttl + 1 <= n){
				for(int m=1; m <= n; m++){
					if(m != tmp.current && tmp.checkTable[m] == 0 && table[tmp.current][m] == 1){
						Packet q;
						q.copy(tmp);
						q.ttl--;
						q.set(m);
						Q.push(q);
					}
				}
			}
			if(FLG)break;
		}

		if(!FLG){
			printf("NA\n");
		}

	}

    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main() {
 int d[100][100],rn,pn,i,j,to,fr,s1,d1,v1,a,b,c,n;
 for (i=0;i<100;i++) for (j=0;j<100;j++) d[i][j]=999;
 cin >> n;
 for (i=0;i<n;i++) {
   cin >> to >> rn; 
   for (j=0;j<rn;j++) { cin >> fr; d[to-1][fr-1]=1;}
 }
 for (a=0;a<100;a++) for (b=0;b<100;b++) for (c=0;c<100;c++) d[b][c]=min(d[b][c],d[b][a]+d[a][c]);
 cin >> pn;
 for (i=0;i<pn;i++) {
   cin >> s1 >> d1 >> v1;
   if (d[s1-1][d1-1]>v1-1) cout << "NA"; else cout << d[s1-1][d1-1]+1;
   cout << endl;
 }
return 0;
 }

C++: #include <bits/stdc++.h>
 
using namespace std;
 
#define MAX 100
#define INF 100000
 
int main()
{
    int n;
    cin >> n;
    int d[MAX][MAX];
    for (int i = 0; i < n; i++) {
	for (int j = 0; j < n; j++) {
	    d[i][j] = INF;
	}
    }
    for (int i = 0; i < n; i++) {
	int r, k, t;
	cin >> r >> k; r--;
	for (int j = 0; j < k; j++) {
	    cin >> t; t--;
	    d[r][t] = 1;
	}
    }
    for (int k = 0; k < n; k++) {
	for (int i = 0; i < n; i++) {
	    for (int j = 0; j < n; j++) {
		d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
	    }
	}
    }
    
    int p, s, t, v;
    cin >> p;
    for (int i = 0; i < p; i++) {
	cin >> s >> t >> v; s--; t--;
	if (d[s][t]+1 <= v) {
	    cout << d[s][t]+1 << endl;
	} else {
	    cout << "NA" << endl;
	}
    }
    return 0;
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include <map>
#include <queue>
#include<string.h>
using namespace std;
typedef pair<int,int> P;
int node[200][200];
int main()
{
  int n;
  cin>>n;
  memset(node,-1,sizeof(node));
  for(;n--;)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k][tmp]=1;
	}
    }
  int p;
  cin>>p;
  for(;p--;)
    {
      int s,d,v;
      cin>>s>>d>>v;
      int ans=-1;

      queue<P> qu;
      int c[200];
      memset(c,-1,sizeof(c));
      for(int i=0;i<200;i++)
	{
	  if(v-1>0)
	    if(node[s][i]==1)
	      qu.push(P(i,v-1));
	}
      while(!qu.empty())
	{
	  P a = qu.front();
	  qu.pop();
	  //      cout<<a.first<<" "<<a.second<<endl;
	  if(a.first==d)
	    {
	      ans=v-a.second+1;
	      break;
	    }
	  for(int i=0;i<200;i++)
	    {
	      if(node[a.first][i]==1)
		{
		  if(a.second-1>0)
		    {
		      if(c[i]==-1)
			{
			  c[i]=a.second-1;
			  qu.push(P(i,a.second-1));	    
			}
		    }
		}
	    } 
	}
      
      
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
      
    }
  
}