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<set>
#include<cstring>
#include<algorithm>

using namespace std;

int CntData[101][101];
const int INF = 1000000;

void Init() {
	for (int i = 1; i < 101; i++) {
		for (int j = 1; j < 101; j++) {
			CntData[i][j] = INF;
		}
	}
}

void solve(int n) {
	for (int k = 1; k < n; k++) {
		for (int i = 1; i < n; i++) {
			for (int j = 1; j < n; j++) {
				CntData[i][j] = min(CntData[i][j], CntData[i][k] + CntData[k][j]);
			}
		}
	}
}

int main() {
	int n, p, r, k, t;

	Init();
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> r >> k;
		for (int j = 0; j < k; j++) {
			cin >> t;
			CntData[r][t] = 1;
		}
	}
	solve(n+1);
	
	cin >> p;
	while (p--) {
		cin >> r >> k >> t;
		if (t > CntData[r][k]) {
			cout << CntData[r][k]+1 << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
#define MAX 10000000
using namespace std;

typedef pair<int,int> P;
struct edge{
  int to,cost; edge(){}
  edge(int cost,int to):to(to),cost(cost){}
};
vector< vector<edge> > info(110);
int n,m,co[110],ttl;

void init(){
  for(int i=0;i<110;i++) co[i] = MAX;
}

int Dijkstra(){
  init();
  int s,d;
  cin >> s >> d >> ttl;
  priority_queue<P,vector<P>,greater<P> > que;
  que.push(P(0,s));
  co[s] = 0;
  while(!que.empty()){
    P p = que.top(); que.pop();
    int now = p.second,c = p.first;
    if(now == d) return c + 1;
    for(int i=0;i<info[now].size();i++){
      edge e = info[now][i];
      if(e.cost + c < co[e.to]){
	que.push(P(e.cost + c,e.to));
	co[e.to] = e.cost + c;
      }
    }
  }
  return -1;
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    int r,k,tmp;
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> tmp;
      info[r].push_back(edge(1,tmp));
    }
  }
  cin >> m;
  while(m--){
    int d = Dijkstra();
    if(d > ttl || d == -1) cout << "NA" << endl;
    else cout << d << endl;
  }
}

C++: #include<iostream>
#include<stack>
#include<vector>
#include <map>
#include <queue>
#include<string.h>
using namespace std;
typedef pair<int,int> P;

int main()
{
  int n;
  int node[100][100];
  cin>>n;
  for(int i=0;i<100;i++)
    for(int j=0;j<100;j++)
      node[i][j]=0;
  
  
  for(int x=0;x<n;x++)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k][tmp]=1;
	}
    }
  int p;
  cin>>p;
  for(;p--;)
    {
      int s,d,v;
      cin>>s>>d>>v;
      queue<P> qu;
      int ans=-1;
      if(v>1)
	for(int i=0;i<=n;i++)
	  {
	    if(node[s][i]==1)
	      qu.push(P(i,2));
	  }
      bool move[n+1];
      for(int i=0;i<=n;i++)
	{
	  move[i]=true;
	}
      while(!qu.empty())
	{
	  P a = qu.front();
	  qu.pop();
	  //	  cout<<"Queue:"<<a.first<<"  "<<a.second<<endl;
	  if(a.first==d)
	    {
	      ans = a.second;
	      break;
	    }
	    for(int i=1;i<=n;i++)
	      {
		if(node[a.first][i]==1)
		  {
		    if(move[i])
		      {
			qu.push(P(i,a.second+1));
			move[i]=false;
		      }
		  }
	      }
	}
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
    }
}

C++: #include <iostream>
#include <set>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;


class Packet
{
public:
	Packet( int _now, set< int > _via ) : now( _now ), via( _via ){}
	bool isPast( int p )
	{
		if( via.find( p ) != via.end() ){
			return true;
		}
		return false;
	}

	int now;
	set< int > via;
};

int solve( int table[ SIZE ][ SIZE ], int s, int e, int initTtl, int numRooter )
{
	if( initTtl < 2 ){
		return 0;
	}
	vector< Packet > pList;
	for( int i = 1; i <= numRooter; ++i ){
		set< int > via;
		via.insert( s );
		if( table[ s ][ i ] ){
			if( i == e ){
				return 2;
			}
			set< int > via2 = via;
			via2.insert( i );
			pList.push_back( Packet( i, via2 ) );
		}
	}

	int ttl = initTtl - 1;
	while( pList.size() > 0 ){
		--ttl;
		if( ttl < 1 ){
			return 0;
		}
		vector< Packet > pList2;
		for( int i = 0; i < pList.size(); ++i ){
			Packet& p = pList[ i ];
			for( int j = 1; j <= numRooter; ++j ){
				if( table[ p.now ][ j ] && !p.isPast( j ) ){
					if( j == e ){
						return initTtl - ttl + 1;
					}
					set< int > via = p.via;
					via.insert( j );
					pList2.push_back( Packet( j, via ) );
				}
			}
		}
		pList = pList2;
	}

	return 0;
}

int main()
{
	int table[ SIZE ][ SIZE ];

	int n;
	while( cin >> n ){
		cin.ignore( 100, '\n' );
		memset( table, 0, sizeof(table) );
		string s;
		for( int i = 0; i < n; ++i ){
			getline( cin, s );
			int o = s[ 0 ] - '0';
			for( int j = 2; j < s.size(); j += 2 ){
				table[ o ][ s[ j ] - '0' ] = true;
			}
		}
		int p;
		cin >> p;
		for( int i = 0; i < p; ++i ){
			int from, to, ttl;
			cin >> from >> to >> ttl;
			int result = solve( table, from, to, ttl, n );
			if( result > 0 ){
				cout << result << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}

	return 0;
}