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 <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


const int INF = 1 << 20;

int main()
{
    int n, p;
	cin >> n;
	
	vector<vint> G(n, vint(n, INF));
	
	rep(i, n)
	{
		int r1, k1;
		
		cin >> r1 >> k1;
		
		rep(j, k1)
		{
			int buf;
			cin >> buf;
	
			G[r1 - 1][buf - 1] = 1;
		}
	}
	
	
	rep(k, n) rep(i, n) rep(j, n)
		chmin(G[i][j], G[i][k] + G[k][j]);
	
	
	cin >> p;
	
	rep(i, p)
	{
		int s1, d1, v1;
		cin >> s1 >> d1 >> v1;
		
		int cost = G[s1 - 1][d1 - 1];
		
		if(cost == INF || v1 <= cost)
		{
			cout << "NA" << endl;
		}
		else
		{
			cout << cost + 1 << endl;
		}
	}
}

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

int main() {
    int n;
    cin >> n;
    vector<vector<int>> dist(n+1, vector<int>(n+1, 1000));
    for (int i=0; i<n+1; ++i) {
        dist[i][i] = 0;
    }
    int r, k;
    for (int i=0; i<n; ++i) {
        cin >> r >> k;
        int t;
        for (int j=0; j<k; ++j) {
            cin >> t;
            dist[r][t] = 1;
        }
    }
    for (int k=0; k<n+1; ++k) {
        for (int i=0; i<n+1; ++i) {
            for (int j=0; j<n+1; ++j) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
    int p;
    cin >> p;
    int s, d, v;
    for (int i=0; i<p; ++i) {
        cin >> s >> d >> v;
        if (dist[s][d]+1 <= v) {
            cout << dist[s][d]+1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<utility>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef pair<int,int>P;
vector<int>R[105];

int f(int s,int t,int v)
{
	int flg[105][105],r=-1;
	memset(flg,0x7f,sizeof(flg));
	priority_queue<P>Q;
	Q.push(P(1,s));
	flg[s][s]=0;
	for(;!Q.empty();)
	{
		P now=Q.top();Q.pop();
		if(now.second==t)
		{
			if(r==-1)r=now.first;
			else r=min(r,now.first);
			continue;
		}
		now.first++;
		if(now.first>v)continue;
		int n=now.second;
		for(int i=0;i<R[n].size();i++)
		{
			if(flg[s][R[n][i]]>now.first)
			{
				flg[s][R[n][i]]=now.first;
				Q.push(P(now.first,R[n][i]));
			}
		}
	}
	return r;
}

int main()
{
	int N,M,s,t,v,r;
	scanf("%d",&N);
	for(;N--;)
	{
		scanf("%d%d",&s,&t);
		for(;t--;)
		{
			scanf("%d",&v);
			R[s].push_back(v);
		}
	}
	scanf("%d",&M);
	for(;M--;)
	{
		scanf("%d%d%d",&s,&t,&v);
		r=f(s,t,v);
		if(r!=-1)printf("%d\n",r);
		else puts("NA");
	}
}

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,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(t_limit <= 1){
			printf("NA\n");
			FLG = true;
		}

		if(!FLG){
			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.ttl > 0 && 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+2+memo[i][goal]);
							memo[start][goal] = t_limit-tmp.ttl+2+memo[i][goal];
							FLG = true;
							while(!Q.empty())Q.pop();
							break;
						}

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

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

	}

    return 0;
}