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<iostream>
#include<algorithm>
#define INF 1e8
using namespace std;

int graf[105][105];

void wfloyd(int V) {
	for (int k = 0;k < V;k++) {
		for (int i = 0;i < V;i++) {
			for (int j = 0;j < V;j++)graf[i][j] = min(graf[i][j], graf[i][k] + graf[k][j]);
		}
	}
}

int main() {
	int n;
	cin >> n;
	
	for (int i = 0;i <= n;i++)
		for (int j = 0;j <= n;j++)graf[i][j] = INF;

	for (int i = 0;i < n;i++) {
		int s, d;
		cin >> s >> d;

		for (int j = 0;j < d;j++) {
			int a;
			cin >> a;
			graf[s-1][a-1] = 1;
		}
	}

	wfloyd(n);
	int p;
	cin >> p;
	for (int i = 0;i < p;i++) {
		int a, b, c;
		cin >> a >> b >> c;
		int d = graf[a-1][b-1];
		if (d >= c)cout << "NA" << endl;
		else cout << d + 1 << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#define MM	1000000
using namespace std;
int main(void)
{
	int i,j,n,p,k[101],t[101][100];
	int s,d,leng[101],r,flg[101],f,v,ii,jj,x;
	scanf("%d",&n);
	for(i=1;i<=n;i++)	{
		scanf("%d",&r);
		scanf("%d",&k[r]);
		for(j=0;j<k[r];j++)	{
			scanf("%d",&t[r][j]);
		}
	}
	scanf("%d",&p);
	for(i=0;i<p;i++)	{
		scanf("%d %d %d",&s,&d,&v);
		f=0;
		for(j=1;j<=n;j++)	{
			flg[j]=0;	leng[j]=MM;
		}
		leng[s]=1;
		for(j=1;j<v;j++)	{
			for(ii=1;ii<=n;ii++)	{
				if(leng[ii]==j)	{
					for(jj=0;jj<k[ii];jj++)	{
						x=t[ii][jj];
						if(leng[x]>j+1)	{
							leng[x]=j+1;
						}
						if(x==d)	{
							f=1;
							ii=n+1;
							j=v;
							break;
						}
					}
				}
			}
		}
		if(f==1)	{
			printf("%d\n",leng[d]);
		}
		else {
			printf("NA\n");
		}
	}
	return 0;
}

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 CalcSumRouter(I4 i4Sender,I4 i4Destination,I4 i4TTL);
/* ??°?????????????????° */
I1 u1Connect[MAX_ROUTER+1][MAX_ROUTER+1];
I4 i4RouterNum;

/* main??? */
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 = CalcSumRouter(i4StartRouter,i4GoalRouter,i4TTL);
		if(i4Ans > 0){
			printf("%d\n",i4Ans);
		}
		else{
			printf("NA\n");
		}
	}
	else{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

/* ??????????????????????¨????????????°?¨??????¢??° */
I4 CalcSumRouter(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;
	I4 i4RoopMax;
	
	/* ????????? */
	for( i4RoopCnt1=1; i4RoopCnt1<=i4RouterNum; i4RoopCnt1++){
		i4MinVal[i4RoopCnt1] = 0;
		for(i4RoopCnt2=1; i4RoopCnt2<=i4RouterNum; i4RoopCnt2++){
			i1StarTable[i4RoopCnt1][i4RoopCnt2] = FALSE;
		}
	}
	
	if(i4TTL<=i4RouterNum){
		i4RoopMax = i4TTL;
	}
	else{
		i4RoopMax == i4RouterNum;
	}
	
	i1StarTable[1][i4Sender] = TRUE;
	
	for(i4RoopCnt1=1; i4RoopCnt1<i4RoopMax; i4RoopCnt1++){
		for(i4RoopCnt2=1; i4RoopCnt2<=i4RouterNum; i4RoopCnt2++){
			if(i1StarTable[i4RoopCnt1][i4RoopCnt2] == TRUE){
				for(i4RoopCnt3=1; i4RoopCnt3<=i4RouterNum; i4RoopCnt3++){
					if(u1Connect[i4RoopCnt2][i4RoopCnt3] == CONNECT){
						if(i4RoopCnt3 == i4Destination){
							return i4RoopCnt1+1;
						}
						else{
							;
						}
						i1StarTable[i4RoopCnt1+1][i4RoopCnt3] = TRUE;
					}
					
				}
			}
			else{
				;
			}
		}
	}
	return 0;
}

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

int md = 10000000;
vector <int> ro[101];
int s,d,v;
int s2,d2;
int kk[100],n;
bool flg[102] ;

void visit(int sn,int dis) {
  int x;

  //  for(int i=1;i<=n;i++)cout << flg[i];
  // cout <<" ";
  
  flg[sn+1] = 1;
  
  if(sn == d-1 && dis <=v)  md = min(md,dis);

  else if(dis <= v && dis < md) {
    for(int i=0;i<ro[sn].size();i++) {
      
      if(flg[ro[sn][i]] == 0){
	//	cout <<sn+1 << "to"<<ro[sn][i] << endl;
	visit(ro[sn][i]-1,dis+1);
	flg[ro[sn][i]] = 0;
      }
    } 
  }
}
  
int main() {

  cin >> n;
  int a;
    
  for(int i=0;i<n;i++) {
    cin >> kk[i];
    cin >> a;
    ro[i].resize(a);
    for(int j=0;j<a;j++) cin >> ro[i][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;


    for(int i=0;i<=n;i++) flg[i] = 0;
    md = 10000000;
   
    visit(s-1,1);
    if(md != 10000000) cout << md <<endl;
    else cout << "NA" <<endl;
  }
  
  return 0;
  
}