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
#define IND first
#define Z second.first
#define V second.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];
bool visited[N];
int n, res, vv;

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;
        COUNT(i, N) visited[i] = false;

        queue< pair<int, pii> > q;
        q.push(mp(s, mp(d, vv)));

        while(!q.empty()){
            pair<int, pii> cur = q.front();
            q.pop();

            if(cur.IND == cur.Z){
                res = vv - cur.V + 1;
                break;
            }
            cur.V--;
            if(!cur.V || visited[cur.IND]) continue;
            visited[cur.IND] = true;

            COUNT(i, N)
                if(ok[cur.IND][i]){
                    pair<int, pii> next = mp(i, mp(cur.Z, cur.V));
                    q.push(next);
                }
        }
     
        if(res == INF) cout << "NA" << endl;
        else cout << res << endl;
    }

    return 0;
}

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();

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

				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>
using namespace std;
const int maxv=100000;
int main()
{
	int root[101][101];
	for(int i=0;i<10;i++){
		for(int i2=0;i2<=100;i2++){
			root[i][i2]=maxv;
		}
	}
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
	int r,k,ch;
	cin>>r>>k;
		for(int i2=0;i2<k;i2++){
			cin>>ch;
			root[r][ch]=1;
		}
	}

	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int i2=01;i2<=n;i2++){
				root[i][i2]=min(root[i][i2],root[i][k]+root[k][i2]);
			}
		}
	}

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

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int g[100][100];
int main(){
	int a;
	scanf("%d",&a);
	for(int i=0;i<100;i++)
		for(int j=0;j<100;j++)
			g[i][j]=9999;
	for(int i=0;i<a;i++)
		g[i][i]=0;
	for(int i=0;i<a;i++){
		int b,c;
		scanf("%d%d",&b,&c);
		for(int j=0;j<c;j++){
			int d;
			scanf("%d",&d);
			g[b-1][d-1]=1;
		}
	}
	for(int k=0;k<a;k++)
		for(int i=0;i<a;i++)
			for(int j=0;j<a;j++)
				g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
	scanf("%d",&a);
	while(a--){
		int p,q,r;scanf("%d%d%d",&p,&q,&r);
		if(r<=g[p-1][q-1])printf("NA\n");
		else printf("%d\n",g[p-1][q-1]+1);
	}
}