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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

const int INF = 1e6;
int main(void){
  int n;
  int cost[100][100];
  REP(i,100)REP(j,100)cost[i][j] = (i!=j)?INF:0;
  cin>>n;
  REP(i,n){
    int r,k;
    cin>>r>>k;
    r--;
    while(k--){
      int t;
      cin>>t;
      t--;
      cost[r][t] = 1;
    }
  }
  int p;
  cin>>p;
  REP(k,n)REP(i,n)REP(j,n)cost[i][j] = min(cost[i][j],cost[i][k]+cost[k][j]);
  while(p--){
    int s,d,v;
    cin>>s>>d>>v;
    int c = cost[s-1][d-1];
    c++;
    if(c<=v)cout<<c<<endl;
    else cout<<"NA"<<endl;
  }
  return 0;
}

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;
		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,memo[101][101];
	Packet tmp;

	for(int i=1; i < 101; i++){
		for(int k=1; k < 101; 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");
			}
			FLG = true;
		}

		if(!FLG){
			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()){
			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 > 1){
				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]+1) < tmp.ttl){
							printf("%d\n",t_limit-tmp.ttl+memo[i][goal]+2);
							memo[start][goal] = t_limit-tmp.ttl+memo[i][goal]+2;
							FLG = true;
							while(!Q.empty())Q.pop();
							break;
						}

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

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

	}

    return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <climits>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef pair<int,int> NODE;
#define pos first
#define distance second

int n;
bool info[100][100];

int bfs(int s,int d){
	queue<NODE> Q;
	bool done[100]={false};
	
	Q.push( make_pair(s,0) );
	while( !Q.empty() ){
		NODE now = Q.front();
		Q.pop();
		done[now.pos] = true;

		if(now.pos == d){
			return now.distance;
		}

		for(int next=0;next<n;next++){
			if(info[now.pos][next] && !done[next]){
				Q.push( make_pair(next,now.distance+1) );
			}
		}
	}
	return INT_MAX;
}

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

C++: #include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int n, r, k, t;
int net[100][100];

void warshall_floyd() {
  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      for(int k=0; k<n; k++) {
	net[i][j] = min(net[i][j], net[i][k] + net[k][j]);
      }
    }
  }
}

int main() {

  cin>> n;
  for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {
      net[i][j] = INF;
    }
  }

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

  warshall_floyd();

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

}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N, P;
int d[100][100];

int main(){
    std::cin >> N;

    REP(i, 100){
        REP(j, 100){
            if(i == j){d[i][j] = 0;}
            else{d[i][j] = INF;}
        }
    }

    REP(i, N){
        int from, k;
        std::cin >> from >> k;
        --from;
        
        REP(j, k){
            int to;
            std::cin >> to;
            --to;
            
            d[from][to] = 1;
        }
    }

    REP(k, N){
        REP(i, N){
            REP(j, N){
                d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    std::cin >> P;
    REP(i, P){
        int s, t, ttl;
        std::cin >> s >> t >> ttl;
        --s; --t;

        if(d[s][t] == INF || d[s][t] >= ttl){
            puts("NA");
        }else{
            printf("%d\n", d[s][t] + 1);
        }
    }
}