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>

/* ?????°???????????? */
typedef	void			VD;
typedef	signed char		I1;
typedef unsigned char 	U1;
typedef	signed short	I2;
typedef unsigned short 	U2;
typedef	signed int		I4;
typedef unsigned int 	U4;

/* ?????°?????? */
#define MAX_ROUTER 100
#define MAX_TTL 10000

#define CONNECT 1
#define DISCONNECT 0
#define SENTINEL -1

#define TRUE 1
#define FALSE 0

#define INPUT_SUCCESS 0
#define INPUT_ERORR -1

/* ????????????????????£?¨? */
VD setInit(VD);
I1 inputConnectInfo(VD);
I1 inputTransmitPacket(VD);
I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL);
/* ??°?????????????????° */
I1 u1Connect[MAX_ROUTER+1][MAX_ROUTER+1];
I4 i4RouterNum;

I4 main(VD)
{
	I1 i1ErrorCheck;
	I4 i4CalcNum;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	setInit();
	i1ErrorCheck = inputConnectInfo();
	if(i1ErrorCheck == INPUT_SUCCESS){
		scanf("%d",&i4CalcNum);
		for( i4RoopCnt1=0; i4RoopCnt1<i4CalcNum; i4RoopCnt1++)
		{
			inputTransmitPacket();
		}
	}
	else{
		;
	}
	return 0;
}

VD setInit(VD)
{
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++)
	{
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++)
		{
			u1Connect[i4RoopCnt1][i4RoopCnt2] = DISCONNECT;
		}
	}
	return;
}

I1 inputConnectInfo(VD)
{
	I1 i1Return;
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RouterCnt;
	I4 i4ConnectNum;
	I4 i4AccessPoint;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d",&i4RouterNum);
	if(i4RouterNum <= (I4)MAX_ROUTER){
		for( i4RoopCnt1=1; i4RoopCnt1<=i4RouterNum; i4RoopCnt1++)		{
			scanf("%d%d",&i4RouterCnt,&i4ConnectNum);
			for( i4RoopCnt2=0; i4RoopCnt2<i4ConnectNum; i4RoopCnt2++)			{
				scanf("%d",&i4AccessPoint);
				u1Connect[i4RouterCnt][i4AccessPoint] = CONNECT;
			}
			u1Connect[i4RouterCnt][i4RouterNum+1] = SENTINEL;
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I1 inputTransmitPacket(VD)
{
	I1 i1Return;
	I4 i4StartRouter;
	I4 i4GoalRouter;
	I4 i4TTL;
	I4 i4Ans;
	
	i1Return = INPUT_SUCCESS;
	
	scanf("%d%d%d",&i4StartRouter,&i4GoalRouter,&i4TTL);
	if(i4TTL <= MAX_TTL){
		i4Ans = CalcDijikstra(i4StartRouter,i4GoalRouter,i4TTL,1);
		if(i4Ans > 0){
			printf("%d\n",i4Ans);
		}
		else{
			printf("NA\n");
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I4 CalcDijikstra(I4 i4Sender,I4 i4Destination,I4 i4TTL)
{
	I1 i1StarTable[MAX_ROUTER+1][MAX_ROUTER+1];
	I4 i4MinVal[MAX_ROUTER+1];
	I4 i4RoopCnt1;
	I4 i4RoopCnt2;
	I4 i4RoopCnt3;
	
	for( i4RoopCnt1=1; i4RoopCnt1<=MAX_ROUTER; i4RoopCnt1++){
		i4MinVal[i4RoopCnt1] = 0;
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++){
			i1StarTable[i4RoopCnt1][i4RoopCnt2] = FALSE;
		}
	}
	
	i1StarTable[1][i4Sender] = TRUE;
	
	for(i4RoopCnt1=1; i4RoopCnt1<MAX_ROUTER; i4RoopCnt1++){
		if(i4TTL <= i4RoopCnt1){
			return 0;
		}
		else{
			;
		}
		for(i4RoopCnt2=1; i4RoopCnt2<=MAX_ROUTER; i4RoopCnt2++){
			if(i1StarTable[i4RoopCnt1][i4RoopCnt2] == TRUE){
				for(i4RoopCnt3=1; i4RoopCnt3<=MAX_ROUTER; i4RoopCnt3++){
					if(u1Connect[i4RoopCnt2][i4RoopCnt3] == CONNECT){
						if(i4RoopCnt3 == i4Destination){
							return i4RoopCnt1+1;
						}
						else{
							;
						}
						i1StarTable[i4RoopCnt1+1][i4RoopCnt3] = TRUE;
					}
					
				}
			}
			else{
				;
			}
		}
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

typedef int Weight;
struct Edge {
	int src, dst;
	Weight weight;
	Edge(int src_, int dst_, Weight weight_) :
		src(src_), dst(dst_), weight(weight_) { }
	Edge(int src_, int dst_) :
		src(src_), dst(dst_) {

	}
};
pair<bool, vector<Weight>> spfa(const int v_num, const vector<vector<Edge>>&es, const int start) {
	vector<Weight>diss(v_num, INT_MAX);
	queue<int>que;
	vector<bool>use(v_num);
	vector<int>count(v_num);
	que.emplace(start);
	diss[start] = 0;
	while (!que.empty()) {
		int src(que.front());
		que.pop();
		use[src] = false;
		for (auto e : es[src]) {
			const int d = e.dst;
			if (diss[src] + e.weight < diss[d]) {
				diss[d] = diss[src] + e.weight;
				if (!use[d]) {
					use[d] = true;
					count[d]++;
					if (count[d] >= v_num)return make_pair(false, vector<Weight>());
					que.emplace(d);
				}
			}
		}
	}
	return make_pair(true, diss);
}
struct Compress {
	map<int, int>mp;
	map<int, int>revmp;

	Compress(vector<int>vs) {
		setmp(vs);
	}
	Compress(const set<int>&vs) {
		setmp(vs);
	}
	Compress() :mp(), revmp() {

	}
	void setmp(vector<int>vs) {
		sort(vs.begin(), vs.end());
		vs.erase(unique(vs.begin(), vs.end()), vs.end());
		for (int i = 0; i < static_cast<int>(vs.size()); ++i) {
			mp[vs[i]] = i;
			revmp[i] = vs[i];
		}
	}
	void setmp(const set<int>&vs) {
		int num = 0;
		for (auto v : vs) {
			mp[v] = num;
			revmp[num] = v;
			num++;
		}
	}
};

int main() {
	int N; cin >> N;
	vector<vector<Edge>>es(N);
	map<int, int>mp;
	map<int, int>revmp;
	for (int i = 0; i < N; ++i) {
		int r; cin >> r;
		mp[r] = i;
		revmp[i] = r;
		int k; cin >> k;
		for (int j = 0; j < k; ++j) {
			int t; cin >> t;
			es[i].push_back(Edge(i, t));
		}
	}
	vector<vector<int>>companion(N,vector<int>(N,1e8));
	for (auto& aes : es) {
		for (auto&e : aes) {
			e.dst = mp[e.dst];
			companion[e.src][e.dst] = 1;
		}
	}
	for (int i = 0; i < N; ++i) {
		companion[i][i] = 0;
	}
	for (int k = 0; k < N; ++k) {
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < N; ++j) {
				companion[i][j] = min(companion[i][k] + companion[k][j], companion[i][j]);
			}
		}
	}
	int P; cin >> P;
	while (P--) {
		int s, d, v; cin >> s >> d >> v;
		int ans = companion[mp[s]][mp[d]];
		if (ans >= v)cout << "NA" << endl;
		else {

			cout << ans + 1 << endl;
		}
	}
	

	return 0;
}

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

const int INF = 1 << 28;

int main()
{
  int n;
  while (cin >> n) {
    vector<vector<int> > dp(n, vector<int>(n, INF));
    for (int i = 0; i < n; ++i) {
      int r, k;
      cin >> r >> k;
      --r;
      for (int j = 0; j < k; ++j) {
	int t;
	cin >> t;
	--t;
	dp[r][t] = 1;
      }
    }

    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
	for (int j = 0; j < n; ++j)
	  dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
      }
    }

    int p;
    cin >> p;
    while (p--) {
      int s, d, v;
      cin >> s >> d >> v;
      --s;
      --d;
      if (dp[s][d] < v)
	cout << dp[s][d]+1 << endl;
      else
	cout << "NA" << endl;
    }
  }
  return 0;
}