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 <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;
#define M 1000
typedef pair<pair<int, int>, int > P;

void disp_p (P p )
{
	int from = p.first.first;
	int to   = p.first.second;
	int ttl  = p.second;

	cout << "from: " << from << " to: " << to << " ttl: " << ttl << endl;
}

// ツパツケツッツトツつェツ経ツ由ツつキツづゥツδ仰ーツタツづ個催短ツ仰猟猟」ツづーツダツイツクツスツトツδ可づ仰づ淞づゥツ。
// ttl >= ツ個仰嘉環づ按づァ ツ個仰嘉環づーツ陛板つキ
// ttl < ツ個仰嘉環づ按づァ -1 ツづーツ陛板つキ
int dijkstra (int from , int to, vector <vector <int> > t )
{
//	int from = packet.first.first;
//	int to   = packet.first.second;
//	int ttl  = packet.second;
//	int res  = 0;
	int size = t.size();
	vector <int> d (size, M );
	vector <bool> v (size, false );
	int i,p;

	v[0] = true;
	d[from] = 1;

	// for debug
//	disp_p (packet );
	int mind = M;

	do{
		mind = M;
		for (i = 1; i < size; ++i){
			if (!v[i] && mind > d[i] ){
				mind = d[i];
				p = i;
			} // end if
		} // end for
		
		v[p] = true;

		for (i = 1; i < size; ++i){
			if ( d[p] + t[p][i] < d[i] ){
				d[i] = d[p] + t[p][i];
			} // end if
		} // end for
//	} while ((int)count (v.begin(), v.end(), true ) != size );
	} while (mind != M );
	// for debug
/*
	for (i = 0; i < size; ++i ){
		cout << d[i] << ' ';
	} // end for
	cout << endl;
*/	
/*
	if (d[to] <= ttl ){
		res = d[to];
	}else{
		res = -1;
	} // end if
*/
	return d[to];
}

template<class T> void disp_t (vector <T> t )
{
	int size = t.size();
	int i,j;

	for (i = 0; i < size; ++i){
		for (j = 0; j < size; ++j){
			if (t[i][j] == M ){
				cout << " 0";
			}else{
				cout << setw(2) << t[i][j];
			} // end if
		} // end for
		cout << endl;
	} // end for
}
	
int main()
{
//	cut here before submit 
//	freopen ("testcase.packet", "r", stdin );
	string str = "";
	string res = "";
	int n;

	getline (cin, str );
	stringstream ssn(str);
	ssn >> n;

	vector <vector <int> > t (n+1, vector <int> (n+1, M) );
	vector <vector <int> > memo (n+1, vector <int> (n+1, M ) );
	int i,j;
	for (i = 0; i < n; ++i){
		int from, num, to;
		getline (cin, str );
		stringstream ss(str);

		ss >> from >> num;
		for (j = 0; j < num; ++j){
			ss >> to;
			t[from][to] = 1;
		} // end for
	} // end for
	

	for (i = 1; i <= n; ++i){
		for (j = 1; j <= n; ++j){
			if (i != j ){
				memo[i][j] = dijkstra (i, j, t );
			} // end if
		}  // end for
	} // end for

	// for debug
//	disp_t (memo );

	int p;
	getline (cin, str );
	stringstream ssp (str );
	ssp >> p;
	
	for (i = 0; i < p; ++i){
		int from, to , ttl;
		getline (cin, str );
		stringstream ss (str );
		ss >> from >> to >> ttl;
	
		int res = memo[from][to];
		if (memo[from][to] <= ttl ){
			res = memo[from][to];
		}else{
			res = -1;
		} // end if		

		if (res == -1){
			cout << "NA" << endl;
		}else{
			cout << res << endl;
		} // end if
	} // end for

	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);i++)
#define rep0(i,n) for(i=0;i<(n);i++)
#define repp(i,m,n) for(int i=(m);i<(n);i++)

#define PUTLINE cout<<__LINE__<<endl;

vector<int> edges[110];
int dis[110];

int main() {
	int n;
	cin>>n;
	int r,k;
	rep(i,n){
		cin>>r>>k;
		while(k--){
			edges[i].push_back(in()-1);
		}
	}
	int p;
	cin>>p;
	rep(i,p){
		int s,d,v;
		cin>>s>>d>>v;
		s--;d--;
		rep(j,n)dis[j]=100100100;
		priority_queue<pint,vector<pint>,greater<pint> > que;
		que.push(mp(0,s));
		while(!que.empty()){
			pint v=que.top();
			que.pop();
			if(dis[v.second]<=v.first)continue;
			dis[v.second]=v.first;
			rep(j,edges[v.second].size()){
				que.push(mp(v.first+1,edges[v.second][j]));
			}
		}
		if(dis[d]>=v)cout<<"NA"<<endl;
		else cout<<dis[d]+1<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
int d[1000][1000]={};
int x[100];
int n;
int nm=0;
void bfs(int);
int main(){
  int p;
  int na[100];
  int to;
  int th;
  int st,go,ttl;
  //syoki
  for(int i=0;i<n;i++){
    x[i]=10000;
  }
  //in
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> na[i] >> to;
    if(na[i]>nm)nm=na[i];
    for(int j=0;j<to;j++){
      cin >> th;
      d[na[i]][th]=1;
    }
  
  }
  //in2
  cin >> p;
  for(int i=0;i<p;i++){
    for(int j=0;j<=nm;j++){
      x[j]=10000;
    }
    
    cin>> st >> go >> ttl;
    x[st]=1;
    bfs(st);
    if(x[go]<=ttl)cout << x[go] << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

///bfs///////////////////////////////////////////
void bfs(int s){
  queue<int>q;
  int u;
  q.push(s);
  while(!q.empty()){
    u=q.front();
    q.pop();
    for(int v=0;v<=nm;v++){
      if(d[u][v]==1&&x[v]==10000){
	x[v]=x[u]+1;
	q.push(v);
      }
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	int g[n][n];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
			g[i][j] = 100000000;
	int r,k,t;
	for(int i = 0; i < n; i++){
		cin >> r >> k;
		r--;
		for(int j = 0; j < k; j++){
			cin >> t;
			t--;
			g[r][t] = 1;
		}
	}
	int p;
	cin >> p;
	for(int i = 0; i < p; i++){
		int s,d,v;
		cin >> s >> d >> v;
		s--; d--;
		vector <int> used(n,false);
		vector <int> dist(n,100000000);
		dist[s] = false;
		while(1){
			int va = -1;
			for(int j = 0; j < n; j++){
				if(used[j] == false && (va == -1 || dist[va] > dist[j]))
					va = j;
			}
			if(va == -1)
				break;
			used[va] = true;
			for(int j = 0; j < n; j++)
				dist[j] = min(dist[j] , dist[va] + g[va][j]);	
		}

		if(v > dist[d])
			cout << dist[d] + 1 << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}