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 <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef long long ll;

const int MAX_SIZE = 10000;

struct edge{
	int to;
	int cost;
};

typedef pair<int,int> P;
int V;

const int MAX_V = 5000;

vector<edge> G[MAX_V];
int d[MAX_V];
const int INF = 100000000;

int prevv[MAX_V];

class Situation{
public:
	int ttl;
	int node;
	int dist;

	Situation();
	Situation(int ttl_,int node_,int dist_){
		ttl=ttl_;
		node=node_;
		dist=dist_;
	}

	bool operator<(const Situation &sit)const{
		return sit.dist > this->dist;
	}
};

void dijkstra(int s,int ttl){
	priority_queue<Situation > que;
	fill(d,d+MAX_V,INF);
	d[s] = 0;
	que.push(Situation(ttl,s,0));
	while(!que.empty()){
		Situation p = que.top();
		que.pop();
		int v = p.node;
		if(d[v] < p.dist)
			continue;
		if(ttl==1)
			continue;
		for(int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				que.push(Situation(ttl-1,e.to,d[e.to]));
			}
		}
	}
}


int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		int from;
		cin>>from;
		int m;
		cin>>m;
		for(int j = 0; j < m; j++){
			int to;
			cin>>to;
			edge e;
			e.cost=1;
			e.to=to;
			G[from].push_back(e);
		}
	}
	int l;
	cin>>l;
	for(int j = 0; j < l; j++){
		int s,dd,v;
		cin>>s>>dd>>v;
		dijkstra(s,v);
		if(d[dd]==INF)
			cout<<"NA"<<endl;
		else
			cout<<d[dd]+1<<endl;
	}


	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define it vector<int>::iterator
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
using namespace std;
int main(){
	int n;
	cin>>n;
	vvi cost(n+1,vi(n+1,inf));
	rep(i,n){
		int s,m;
		cin>>s>>m;
		rep(j,m){
			int t;
			cin>>t;
			cost[s][t]=1;
		}
	}
	rep(k,n+1)rep(i,n+1)rep(j,n+1)
		cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
	int m;
	cin>>m;
	rep(i,m){
		int a,b,c;
		cin>>a>>b>>c;
		int t=cost[a][b]+1;
		if(t>c)cout<<"NA"<<endl;
		else cout<<t<<endl;
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

struct edge{ 
	int to, cost;
	edge(int to, int cost) :to(to), cost(cost){};
};
vector<edge> G[110];

int n, r, k, t;
int p, s, d, v;

int dis[110];

void dijkstra(int start){
	dis[start] = 0;

	priority_queue<P> que;
	que.push(P(0, start));

	while (!que.empty()){
		P p = que.top(); que.pop();
		int a = p.second;

		if (dis[a] < p.first)continue;

		for (int i = 0; i < G[a].size(); i++){
			edge e = G[a][i];
			if (dis[e.to] > dis[a] + e.cost){
				dis[e.to] = dis[a] + e.cost;
				que.push(P(dis[e.to], e.to));
			}

		}
	}
}

int main(){
	cin >> n;
	REP(i, n){
		cin >> r >> k;
		REP(j, k){
			cin >> t;
			G[r].push_back(edge(t, 1));
		}
	}
	cin >> p;
	REP(i, p){
		REP(j, 110)dis[j] = INF;
		cin >> s >> d >> v;
		dijkstra(s);
		if (dis[d] + 1 <= v)cout << dis[d] + 1 << endl;
		else cout << "NA" << endl;
	}
}

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

int n;
int dst[110][110];
int M = 1001001001; // dekai kazu

int main(){
	cin >> n;
	for(int i=0; i<=n; i++){
		for(int j=0; j<=n; j++){
			dst[i][j] = M;
			if(i==j) dst[i][j] = 0;
		}
	}
	for(int i=0; i<n; i++){
		int r, k;
		cin >> r >> k;
		for(int j=0; j<k; j++){
			int t;
			cin >> t;
			dst[r][t] = 1;
		}
	}
	for(int k=1; k<=n; k++){
		for(int i=1; i<=n; i++){
			for(int j=1; j<=n; j++){
				if(dst[i][k]+dst[k][j] < dst[i][j]){
					dst[i][j] = dst[i][k]+dst[k][j];
				}
			}
		}
	}

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

	return 0;
}

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

struct Node {
    vector<int> to;
    bool isDone;
    int cost;
};

const int MAX_N = 100;
Node nodes[MAX_N + 1];
int n;

int dijkstra(int start, int end) {
    for (int i = 1; i <= n; i++) {
        nodes[i].isDone = false;
        nodes[i].cost = -1;
    }
    nodes[start].cost = 0;
    while (true) {
        queue<int> qu;
        for (int i = 1; i <= n; i++) {
            if (nodes[i].isDone == true || nodes[i].cost < 0) {
                continue;
            }
            qu.push(i);
        }
        if (qu.empty() == true) {
            break;
        }
        while (qu.empty() == false) {
            int idx = qu.front();
            qu.pop();
            nodes[idx].isDone = true;
            for (vector<int>::iterator it = nodes[idx].to.begin(); it != nodes[idx].to.end(); it++) {
                if (nodes[*it].cost < 0) {
                    nodes[*it].cost = nodes[idx].cost + 1;
                }
            }
        }
    }
    return nodes[end].cost;
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        int r, k;
        cin >> r >> k;
        for (int j = 0; j < k; j++) {
            int t;
            cin >> t;
            nodes[r].to.push_back(t);
        }
    }
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
        int s, d, v;
        cin >> s >> d >> v;
        int cost = 1 + dijkstra(s, d);
        if (v < cost) {
            cout << "NA" << endl;
        } else {
            cout << cost << endl;
        }
    }
}