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++: //Memory Limit ExceededÉÈé
//[ðñ70kByteögpµÄ¢éçµ¢
//TvCvbg©Í¤Ü­¢ÁÄ¢éÁÛ¢
//



#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define SUB 103

int net[SUB][SUB],n,maxx=0;

typedef pair<int,int> BVB;


int ans(int,int,int);

void debug();

int main(){

 

  int i,j,p,num,n1,th;

  cin >> n;



  for(i=0;i<n;i++){ //net's info
    
    cin >> num >> n1;
    if(num >= maxx)maxx = num;


    //  for(int k=0;k<maxx;k++)net[num][k]=0;    //siyoukanou

    for(j=0;j<n1;j++){
      cin >> th;
      net[num][th]=1;
    }

  }//net's info
  // cout << "maxx =" << maxx << endl;
  for(int m=0;m<maxx;m++){
    for(int n=0;n<maxx;n++){
      if(net[m][n]!=1)net[m][n]=0;
    }
  }

  

  int pkn;

  cin >> pkn;

  int st,ed,c,answer;

  //debug();



  for(i=0;i<pkn;i++){//pack's info
    cin >> st >> ed >> c;


    answer = ans(st,ed,c);

    if(answer != -2)cout << answer << endl;
    else cout << "NA" << endl;

  }//pack's info



}



//L[(ÊÌzñÅà¢¢¯Ç)ÉÇñÇñüêÄ¢«TTLªO¢ÉÈéOÉÚInÉBÅ«é©Ç¤©ð²×é
//[^ÍÚInÉBµ½n_ÌTTL©çvZµÄ¾·
int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int i,j,k,l,cal=0,mem=0,yes=10001;
  BVB next,fr;

  fr.first=st;
  fr.second=c;
  Nari.push(fr);
  
  //cout << "FIRST next.second = " << c << endl;  
  //cout << "FIRST         cal = " << cal << endl;
  //cout << "             maxx = " << maxx  << endl;
  //cout << "               ed = " << ed << endl;

  if(st == ed && c != 0)return 1;


  while(1){
    next=Nari.front();
    //  cout << "pop i = " << next.first << endl;
    Nari.pop();
    next.second=next.second-1;

    if(fr.second == (next.second+1) && next.second == 0 && next.first != ed)return -2;

    if(next.second < 0)break; //I¹ð
    //   cout << "1:next.first =  " << next.first << endl;
    //   cout << "1:next.second = " << next.second << endl; 
    
    for(i=1;i<=maxx;i++){//¢¯éêðL[Éüêé
      if(net[next.first][i] == 1)mem++;

      // cout << "net[" << next.first << "][" << i <<"] = " << net[next.first][i] << endl;
      
      if(net[next.first][i] == 1 && i == ed ){ //ÚInB

	/*		
	cout << "GOOOOOOOOOL!!! i = " << i <<endl;
	cout << "2:   next.second = " << next.second << endl; 
	*/	

	if(yes >  (c-next.second+1)){
	  yes = (c - next.second)+1;
	  //	  cout << "change yes = " << yes << endl;
	}
	
      }
            

      if(net[next.first][i] == 1){
	Nari.push(BVB(i,next.second));
	//	cout << "push i = " << i << endl;
      }
      
  
    }
      
    
  }

  //  cout << "return yes = " << yes << endl; 

  if(yes != 10001){return yes;}
  else return -2;
  
  
  
}










void debug(){
  for(int i=1;i<=maxx;i++){
    if(i==1){
      cout << "   ";
      for(int k=1;k<=maxx;k++){
	printf("%3d",k);
      }
      cout << endl;
      for(int k=1;k<=maxx;k++)printf("___");
      cout << endl;
    }

    for(int j=1;j<=maxx;j++){
      if(j == 1)cout << i << " |";
      printf("%3d",net[i][j]);
    }
    cout << endl;

  }



}

C++: #include <iostream>
#include <queue>

using namespace std;


static const int NO = -2;
static const int N = 300;


int graph[N + 1][N + 1] = {{}};
int dis[N + 1] = {};
int n;

void bfs(int v){
  queue<int> Q;

  Q.push(v);

  for(int i = 1; i <= N; i++){
    dis[i] = NO;
  }

  dis[v] = 1;

  int u;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();

    for(int i = 1;i <= n; i++){
      if(graph[u][i]==0) continue;
      if(dis[i]!=NO)continue;
      dis[i] = dis[u] + 1;
      Q.push(i);

    }
  }
}

int main(){

  int r[N+1] = {};
  int d[N+1] = {};
  int k[N+1] = {};
  int f[N+1] = {};
  int p,s[N+1] = {};
  int v[N+1] = {};


  //in
  cin >> n;

  for(int i = 1;i <= n; i++){

    cin >> r[i] >> k[i];

    for(int j = 1;j <= k[i];j++){

      cin >> f[j];
      graph[r[i]][f[j]] = 1;
    }
  }

  cin >> p;
  for(int i = 1;i <= p; i++){
    cin >> s[i] >> d[i] >> v[i];
    bfs(s[i]);

    if(v[i] >= dis[d[i]]) cout << dis[d[i]] << endl;
    else cout << "NA" << endl;
  }
}