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 <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define INF (1<<29)

int dist[100][100];

int main() {
	int n;
	cin >> n;
	
	fill(dist[0], dist[100], INF);
	REP(i, n) dist[i][i] = 0;
	REP(i, n) {
		int r, k;
		scanf("%d %d", &r, &k);
		r--;
		REP(j, k) {
			int t;
			scanf("%d", &t);
			t--;
			dist[r][t] = 1;
		}
	}
	
	REP(k, n) REP(i, n) REP(j, n)
		dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
	
	int p;
	cin >> p;
	REP(i, p) {
		int s, d, v;
		scanf("%d %d %d", &s, &d, &v);
		s--; d--;
		if (dist[s][d] == INF || dist[s][d] >= v) puts("NA");
		else printf("%d\n", dist[s][d] + 1);
	}
	return 0;
}

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

#define MAX_N 110

#define INF 999999999

int x[MAX_N][MAX_N];
int d[MAX_N];

queue<int>Q;

int bfs(int s, int t){
	for(int i=0;i<MAX_N;i++){
		d[i]=INF;
	}
	d[s]=0;
	Q.push(s);
	while(!Q.empty()){
		int z=Q.front();
		for(int i=0;i<MAX_N;i++){
			if(x[z][i]<INF){
				if(d[i]==INF){
					d[i]=d[z]+1;
					Q.push(i);
				}
			}
		}
		Q.pop();
	}
	return d[t];
}

int main(){
	int n,m,a,b,c;
	for(int i=0;i<MAX_N;i++){
		for(int j=0;j<MAX_N;j++){
			x[i][j]=INF;
		}
	}
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		for(int j=0;j<b;j++){
			cin>>c;
			x[a][c]=1;
		}
	}
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b>>c;
		int y=bfs(a,b);
		if(y>=c){cout<<"NA"<<endl;}
		else{cout<<y+1<<endl;}
	}
	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 <list>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
// ------ Class ------ //
class Graph1 {
public:
	// ------ Variables ------ //
	static const int INF = 1000000000;
	int V; vector<vector<int> > G;

	// ------ Constructors ------ //
	Graph1() : V(0), G(vector<vector<int> >()) {}
	Graph1(int v) : V(v), G(vector<vector<int> >(v)) {}
	Graph1(vector<vector<int> > g) : V(g.size()), G(g) {}

	// ------ Basic Functions ------ //
	void add1(int v1, int v2) { G[v1].push_back(v2); }
	void add2(int v1, int v2) { add1(v1, v2); add1(v2, v1); }

	// ------ Algorithms ------ //
	vector<int> dist(int s) {
		queue<int> que; que.push(s);
		vector<int> d(V, INF); d[s] = 0;
		while (!que.empty()) {
			int u = que.front(); que.pop();
			for (int i = 0; i < G[u].size(); i++) {
				if (d[G[u][i]] == INF) {
					d[G[u][i]] = d[u] + 1;
					que.push(G[u][i]);
				}
			}
		}
		return d;
	}
};
int N, Q, c, u, v; Graph1 G;
int main() {
	cin >> N; G = Graph1(N);
	for (int i = 0; i < N; i++) {
		cin >> u >> c;
		for (int j = 0; j < c; j++) {
			cin >> v; G.add1(u - 1, v - 1);
		}
	}
	cin >> Q;
	for (int i = 0; i < Q; i++) {
		cin >> u >> v >> c;
		int d = G.dist(u - 1)[v - 1] + 1;
		if (d > c) cout << "NA" << endl;
		else cout << d << endl;
	}
	return 0;
}

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]=99999999;
	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);
		r=min(r,10000);
		
		if(r<=g[p-1][q-1])printf("NA\n");
		else printf("%d\n",g[p-1][q-1]+1);
	}
}

C++: #include <iostream>
#include <map>
#define INF 1e+9
using namespace std;

int main(){
	int n,p;
	map<int,int> num;
	int cost[101][101];
	cin >> n;
	for(int i = 0;i <= n;i++){
		for(int j = 0;j <= n;j++) cost[i][j] = INF;
		cost[i][i] = 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;
			cost[r][t] = 1;
		}
	}
	for(int k = 0;k <= n;k++){
		for(int i = 0;i <= n;i++){
			for(int j = 0;j <= n;j++) cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
		}
	}
	cin >> p;
	for(int i = 0;i < p;i++){
		int s,d,v;
		cin >> s >> d >> v;
		if(cost[s][d] >= v) cout << "NA" << endl;
		else cout << cost[s][d] + 1 << endl;
	}
	return 0;
}