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 <cmath>
#include <algorithm>
#include <queue>

using namespace std;

struct Packet{
	Packet(){}

	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 <= n; i++){
		for(int k=1; k <= n; 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(memo[start][goal] != 0){
			if(memo[start][goal] <= t_limit){
				printf("%d\n",memo[start][goal]);
			}else{
				printf("NA\n");
			}
			continue;
		}

		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() && FLG == false){
			tmp = Q.front();
			Q.pop();

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

						Packet p = tmp;
						p.ttl--;
						p.set(i);
						Q.push(p);
					}
				}
			}
		}

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

	}

    return 0;
}

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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
void mainmain(){
	int n;
	cin>>n;
	vint ch(n+100);
	vvint vv;
	initvv(vv,n,n,INF);
	vvint tt(n);
	rep(i,n){
		rep(j,2){
			int t;
			cin>>t;
			tt[i].PB(t);
		}
		rep(j,tt[i][1]){
			int t;
			cin>>t;
			tt[i].PB(t);
		}
		ch[tt[i][0]]=i;
	}
	rep(i,n){
		// int r,k;
		// cin>>r>>k;
		// ch[r]=i;
		rep(j,tt[i][1]){
			// int a;
			vv[i][ch[tt[i][j+2]]]=1;
		}
	}
	rep(k,n){
		rep(i,n){
			rep(j,n){
				vv[i][j]=min(vv[i][j],vv[i][k]+vv[k][j]);
			}
		}
	}
	int p;
	cin>>p;
	rep(i,p){
		int a,b,c;
		cin>>a>>b>>c;
		int t=vv[ch[a]][ch[b]];
		if(t>=INF||t+1>c) cout<<"NA\n";
		else cout<<t+1<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}