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<cstdio>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

int main(){
	int N,d[100][100]; fill_n((int*)d,100*100,INT_MAX/2);
	cin>>N;
	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--;
			d[R][T] = 1;
		}
	}
	
	for(int k=0;k<N;k++)for(int i=0;i<N;i++)for(int j=0;j<N;j++)
		d[i][j] = min(d[i][j],d[i][k]+d[k][j]);

	int P; cin>>P;
	for(int i=0;i<P;i++){
		int S,D,V;
		cin>>S>>D>>V; S--; D--;
		if(V<d[S][D]+1) puts("NA");
		else printf("%d\n",d[S][D]+1);
	}
	return 0;
}

C++: #include <iostream>
const int INF=500;
using namespace std;
int main(){
  int d[100][100],n,p;
  cin>>n;
  for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=INF;
  for(int i=0,r,k,t;i<n&&cin>>r>>k;i++) for(int j=0;j<k&&cin>>t;j++) d[r-1][t-1]=1;
  for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
  for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]++;
  cin>>p;
  for(int i=0,s,t,v;i<p;i++){
    cin>>s>>t>>v;
    if(d[s-1][t-1]>v) cout<<"NA"<<endl;
    else cout<<d[s-1][t-1]<<endl;
  }
  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 INPUT_SUCCESS 0
#define INPUT_ERORR -1

/* ????????????????????£?¨? */
VD setInit(VD);
I1 inputConnectInfo(VD);
I1 inputTransmitPacket(VD);
I4 CalcRouterSum(I4 i4Sender,I4 i4Destination,I4 i4TTL,I4 i4RouterSum);

/* ??°?????????????????° */
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();
		}
	}
	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)
	{
		//printf("start:%d,goal:%d,TTL:%d\n",i4StartRouter,i4GoalRouter,i4TTL);
		i4Ans = CalcRouterSum(i4StartRouter,i4GoalRouter,i4TTL,1);
		if(i4Ans >= i4TTL+1){
			printf("NA\n");
		}
		else
		{
			printf("%d\n",i4Ans);
		}
		//printf("--------------\n");
	}
	else
	{
		i1Return = INPUT_ERORR;
	}
	return i1Return;
}

I4 CalcRouterSum(I4 i4Sender,I4 i4Destination,I4 i4TTL,I4 i4RouterSum)
{
	//printf("%d -> %d,TTL=%d\n",i4Sender,i4Destination,i4TTL);
	I4 i4RoopCnt1;
	I4 i4MinRouterNum;
	I4 i4TmpRouterSum;
	I4 i4Return;
	
	
	i4Return = i4TTL+i4RouterSum;
	
	if(i4Sender == i4Destination)
	{
		//printf("RouterSum=%d\n",i4RouterSum);
		return i4RouterSum;
	}
	else
	{
		if(i4TTL <= 0)
		{
			printf("TTL is 0\n");
			return 0;
		}
		else
		{
			i4RoopCnt1 = 1;
			while(u1Connect[i4Sender][i4RoopCnt1] != SENTINEL){
				if(u1Connect[i4Sender][i4RoopCnt1] == CONNECT)
				{
					i4TmpRouterSum = CalcRouterSum(i4RoopCnt1,i4Destination,i4TTL-1,i4RouterSum+1);
					if((i4TmpRouterSum > 0 ) && (i4TmpRouterSum < i4Return))
					{
						//printf("CalcRouterSum()=%d\n",i4TmpRouterSum);
						i4Return = i4TmpRouterSum;
					}
						
				}
				i4RoopCnt1++;
			}
			return i4Return;
		}
	}
	return i4Return;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 1<<30

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

bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void shortestPath(const Graph &g, int s, vector<Weight> &dist) {
  int n = g.size();
  dist.assign(n, INF); dist[s] = 0;
  priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push(Edge(-2, s, 0)); !Q.empty(); ) {
    Edge e = Q.top(); Q.pop();
    if (dist[e.dst] < e.weight) continue;
    FOR(f,g[e.dst]) {
      if (dist[f->dst] > e.weight+f->weight) {
        dist[f->dst] = e.weight+f->weight;
        Q.push(Edge(f->src, f->dst, e.weight+f->weight));
      }
    }
  }
}

int main() {
  int n;
  cin >> n;
  Graph g(n);
  while(n--) {
    int a;
    cin >> a;
    int s;
    cin >> s;
    while(s--) {
      int d;
      cin >> d;
      g[a-1].push_back(Edge(a-1,d-1,1));
    }
  }
  int m;
  cin >> m;
  while(m--) {
    int s,d,t;
    cin >>s>>d>>t;
    vector<int> v;
    shortestPath(g,s-1,v);
    if (v[d-1]<t)
      cout << v[d-1]+1 << endl;
    else
      cout << "NA"<< endl;
  }
}