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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	vvi cost(100, vi(100, INF));

	int n;
	cin >> n;
	int r, k, t;
	REP(i, n) {
		cin >> r >> k;
		REP(j, k) {
			cin >> t;
			cost[r-1][t-1] = 1;
		}
	}

	REP(k, 100) {
		REP(i, 100) {
			REP(j, 100) {
				cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j]);
			}
		}
	}

	cin >> n;
	REP(i, n) {
		cin >> r >> k >> t;
		if(cost[r-1][k-1]+1 <= t) {
			cout << cost[r-1][k-1]+1 << endl;
		} else {
			cout << "NA" << endl;
		}
	}

    return 0;
}

C++: #include <bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
int dx[] = { -1, 0, 0, 1 }; int dy[] = { 0, -1, 1, 0 };
bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h&&y < w); }
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int main()
{
	int n;
	int d[101][101];
	REP(i, 101)REP(j, 101) d[i][j] = (i == j ? 0 : INF);
	cin >> n;
	REP(i, n)
	{
		int a, b;
		cin >> a >> b;
		REP(j, b)
		{
			int k;
			cin >> k;
			k--;
			d[i][k] = 1;
		}
	}
	REP(k, n)REP(i, n)REP(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	int q;
	cin >> q;
	while (q--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		if (d[a][b] < c) cout << d[a][b]+1 << endl;
		else puts("NA");
	}
	return 0;
}

C++: #include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;
const int INF = 0x7fffff;

class Node {
public:
	int pos, ttl;
	Node(int p, int t) : pos(p), ttl(t) {}
};

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
int visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
	queue<Node> que;
	fill(visited, visited + n + 1, INF);
	que.push(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (current.ttl >= 0 && visited[current.pos] <= current.ttl) continue;
		visited[current.pos] = current.ttl;
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next] && visited[next] > current.ttl) {
				que.push(Node(next, current.ttl - 1));
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k;
		cin >> r >> k;
		fill(adj[r], adj[r] + n + 1, false);
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			adj[r][t] = true;
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		bfs(s, d, v);
	}
	return 0;
}

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

const int INF = 100000;
int d[100][100];
void init()
{
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++){
			if(i == j){
				d[i][j] = 0;
			}else{
				d[i][j] = INF;
			}
		}
	}
}

int main()
{
	int n;
	cin >> n;
	init();
	for(int i=0;i<n;i++){
		int a,m;
		cin >> a >> m;
		for(int j=0;j<m;j++){
			int b;
			cin >> b;
			d[a-1][b-1] = 1;
		}
	}
	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				int dis = d[i][k]+d[k][j];
				if(dis < d[i][j]){
					d[i][j] = dis;
				}	
			}
		}
	}
	int p;
	cin >> p;
	for(int j=0;j<p;j++){
		int a,b,pket;
		cin >> a >> b >> pket;
		if(d[a-1][b-1]+1 <= pket && d[a-1][b-1] != INF)
			cout << d[a-1][b-1]+1  << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}