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 <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 101;

bool ok[N][N];
int n, res, vv;

void dfs(int, int, int);

int main(void){
    cin >> n;

    rep(i, n){
        int r, k; cin >> r >> k;
        while(k--){
            int t; cin >> t;
            ok[r][t] = true;
        }
    }

    int p; cin >> p;
    while(p--){
        int s, d; cin >> s >> d >> vv;
        res = INF;

        dfs(s, d, vv - 1);
     
        if(res == INF) cout << "NA" << endl;
        else cout << res << endl;
    }

    return 0;
}

void dfs(int cur, int z, int v){
    if(cur == z){
        res = vv - v;
        return;
    }
    if(v == 0) return;

    COUNT(i, n) if(ok[cur][i]) dfs(i, z, v - 1);
}

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

int main(){
  int a,b,c,d,e,f,g,now,v[101],m[101][101]={};
  queue<int> q;
  cin >> a;
  for(int i=1;i<=a;i++){
    cin >>b>>c;
    for(int j=0;j<c;j++){
      cin >> d;
      m[i][d]=1;
    }
  }
  cin >> d;
  for(int i=0;i<d;i++){
    cin >> e >> f >> g;
    q.push(e);
    for(int j=1;j<=a;j++)v[j]=1200;
    v[e]=1;
    while(1){
      now=q.front();q.pop();
      if(now==f){
	if(v[f]>g){
	  cout << "NA" << endl;
	  break;
	  }else {
	  cout << v[now] << endl;
	  break;
	}
      }
      for(int j=1;j<=a;j++){
	if(m[now][j]==1&&v[j]==1200){
	  v[j]=v[now]+1;
	  q.push(j);
	}
      }
      if(q.empty()){
	cout<< "NA" << endl;
	break;
      }
    }
    while(!q.empty())q.pop();
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
typedef pair<int,int> NODE;
#define mp make_pair
bool connect[100][100];

int bfs(int s,int g,int t){
	queue<NODE> Q;
	int ttl,pos;
	Q.push(mp(s,t));
	while(!Q.empty()){
		pos = Q.front().first;
		ttl = Q.front().second;
		Q.pop();
		ttl--;
		if(pos == g)return ttl; 
		if(ttl>0)
			for(int i=0;i<100;i++)
				if(connect[pos][i])
					Q.push(mp(i,ttl));
	}
	return -1;
}

int main(){
	int n,r,k,t;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> r >> k;
		r--;
		for(int j=0;j<k;j++){
			cin >> t;
			t--;
			connect[r][t]=true;
		}
	}
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> r >> k >> t;
		r--;k--;
		if(~bfs(r,k,t))cout << t-bfs(r,k,t) << endl;
		else cout << "NA" << endl;
	}
}

C++: #include<iostream>
#include<cstring>
#include<queue>
#define MAX_N 110
bool A[MAX_N][MAX_N];
int rooter_num;
using namespace std;
int bfs(int,int);
int main()
{
  int n,startNode,goalNode,TTL;
  int r,k,t;
  int a;
  memset(A,false,sizeof(A));
  cin>>rooter_num;
  for(int i=0;i<rooter_num;i++)
    {
      cin>>r>>k;
      for(int j=0;j<k;j++)
	{
	  cin>>t;
	  A[r][t]=true;
	}
    }
  cin>>n;
  for(int i=0;i<n;i++)
    {
      cin>>startNode>>goalNode>>TTL;
      a=bfs(startNode,goalNode);
      //    cout<<"TTL="<<TTL<<";"<<"a-1="<<a<<endl;
      if(a-1<TTL)cout<<a<<endl;
      else cout<<"NA"<<endl;
       
    }
  return 0;
}
int bfs(int startNode,int goalNode)
{
  queue<int>Q;
  int visited[rooter_num+1];
  memset(visited,0,sizeof(visited));
  int currentNode=startNode;
  
  Q.push(currentNode);
  visited[currentNode]=1;
  
  while(!Q.empty())
    {

      
      currentNode=Q.front();
      Q.pop();
      for(int nextNode=1;nextNode<=rooter_num;nextNode++)
	{

	  if(A[currentNode][nextNode]==0)continue;

	  if(nextNode==goalNode)return visited[currentNode]+1;
	  
	  if(visited[nextNode]==0)
	    {
	      visited[nextNode]=visited[currentNode]+1;
	      Q.push(nextNode);
	    }
	}
    }
  return MAX_N;
}

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

using namespace std;

struct Packet{
	Packet(){}

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

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

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

int main(){

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

	for(int i=1; i <= n; i++){
		for(int k=1; k <= n; k++){
			table[i][k] = 0;
			memo[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(memo[start][goal] != 0){
			if(memo[start][goal] <= t_limit){
				printf("%d\n",memo[start][goal]);
			}else{
				printf("NA\n");
			}
			continue;
		}

		for(int i = 1; i <= n; i++){
			if(i != start && table[start][i] == 1){
				if(memo[i][goal] > 0 && memo[i][goal] < t_limit){
					printf("%d\n",memo[i][goal]+1);
					memo[start][goal] = memo[i][goal]+1;
					FLG = true;
					break;
				}
				Packet p(t_limit-1,start);
				p.set(i);
				Q.push(p);
			}
		}



		while(!Q.empty() && FLG == false){
			tmp = Q.front();
			Q.pop();

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

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

				memo[start][goal] = t_limit - tmp.ttl+1;

				break;
			}

			if(tmp.ttl > 0 && tmp.went_num < n){
				for(int i=1; i <= n; i++){
					if(i != tmp.current && tmp.checkTable[i] == 0 && table[tmp.current][i] == 1){
						if(memo[i][goal] > 0 && memo[i][goal] < tmp.ttl){
							printf("%d\n",t_limit-tmp.ttl+memo[i][goal]+1);
							memo[start][goal] = t_limit-tmp.ttl+memo[i][goal]+1;
							FLG = true;
							while(!Q.empty())Q.pop();
							break;
						}

						Packet p = tmp;
						p.ttl--;
						p.set(i);
						Q.push(p);
					}
				}
			}
		}

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

	}

    return 0;
}