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 <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); i++ )
#define UREP( i, m, n ) for ( unsigned int i = (unsigned int)( m ); i < (unsigned int)( n ); i++ )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define RALL( c ) (c).rbegin(), (c).rend()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

// 隣接リスト -> min{s, t}
int dijkstra( vector< vector<PII> > graph, const int start, const int goal )
{
	vector<int> distance( graph.size() );
	fill( ALL( distance ), INT_MAX );

	priority_queue< PII, vector< PII >, greater< PII > > que; // 累計コスト, ノード番号
	que.push( MP( 0, start ) );

	while ( !que.empty() )
	{
		int total = que.top().fst;
		int node = que.top().snd;
		que.pop();

		if ( total < distance[ node ] )
		{
			distance[ node ] = total;

			REP( i, 0, graph[ node ].size() )
			{
				int to = graph[ node ][i].fst;
				int cost = graph[ node ][i].snd;

				que.push( MP( total + cost, to ) );
			}
		}
	}

	return distance[ goal ];
}

int main()
{
	int n;
	cin >> n;

	vector< vector< PII > > graph( n );
	for ( auto &row : graph )
	{
		row = vector<PII>();
	}

	REP( dn, 0, n )
	{
		int r, k;
		cin >> r >> k;
		r--;

		REP( dk, 0, k )
		{
			int t;
			cin >> t;
			t--;

			graph[r].PB( MP( t, 1 ) );
		}
	}

	int p;
	cin >> p;

	REP( dp, 0, p )
	{
		int s, d, v;
		cin >> s >> d >> v;
		s--;
		d--;

		int res = dijkstra( graph, s, d ) + 1;

		if ( res <= v )
		{
			cout << res << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}		

	return 0;
}

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

int main() {
	int n;
	int table[128][128];
	for(int i = 0; i < 128; i++) {
		for(int j = 0; j < 128; j++)
			table[i][j] = 10000;
		table[i][i] = 0;
	}

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

		cin >> s;
		cin >> k;
		for(int j = 0; j < k; j++) {
			int d;
			cin >> d;
			table[s][d] = 1;
		}
	}

	for(int i = 0; i < 128; i++)
		for(int j = 0; j < 128; j++)
			for(int k = 0; k < 128; k++)
				table[j][k] = min(table[j][k], table[j][i] + table[i][k]);

	int p;
	cin >> p;

	for(int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		int tmp = table[s][d] + 1;
		if(v < tmp)
			cout << "NA" << endl;
		else
			cout << tmp << endl;
	}
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int pass[200][200];
int n;

int dijk(int s,int g){
    int d[200];
    bool used[200];
    rep(i,200)d[i]=999999999;
    rep(i,200)used[i]=false;
    d[s]=0;
    
    while(1){
        int v=-1;
        rep(u,n){
            if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
        }
        if(v==-1)break;
        
        used[v]=true;
        
        rep(u,n){
            d[u] = min(d[u],d[v]+pass[v][u]);
        }
    }
    return d[g];
}


int main(){
    while(cin>>n){
        rep(i,200)rep(j,200)pass[i][j]=999999999;
        rep(i,200)pass[i][i]=0;
        rep(i,n){
            int r,k;
            cin>>r>>k;
            r--;
            rep(j,k){
                int x;
                cin>>x;
                x--;
                pass[r][x]=1;
            }
        }
        int p;
        cin>>p;
        rep(i,p){
            int s,g,v;
            cin>>s>>g>>v;
            s--,g--;
            int res = dijk(s,g)+1;
            if(res<=v)cout<<res<<endl;
            else cout<<"NA"<<endl;
        }
    }
}

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

#define INF 1000000000

int d[100][100];

int main()
{
  fill((int*)d, (int*)d + 10000, INF);
  
  int N;
  cin >> N;
  for(int i = 0; i < N; i++)
  {
    int r, k, t;
    cin >> r >> k;
    for(int j = 0; j < k; j++)
    {
      cin >> t;
      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]);

  int S, D, V, P;
  cin >> P;
  for(int i = 0; i < P; i++)
  {
    cin >> S >> D >> V;
    if(d[S - 1][D - 1] < V)
      cout << d[S - 1][D - 1] + 1 << endl;
    else
      cout << "NA" << endl;
  }
}

C++: #include <iostream>
const int INF=1e9;
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;
}