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<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[r-1].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<cstdio>
#include<algorithm>
#define INF 9999999
using namespace std;
int n, p;
int r, k;
int t;
int s, d, v, cost[100][100];
void warshall_floyd();
int main(){
  cin >> n;
  for(int i = 0; i <= n; i++){
    for(int j = 0; j <= n; j++){
      if(i == j) cost[i][j] = 0;
      else{
	cost[i][j] = INF;
      }
    }
  }
  for(int i = 1; i <= n; i++){
    cin >> r >> k;
    for(int j = 0; j < k; j++){
      cin >> t;
      cost[i][t] = 1;
    }
  }
  warshall_floyd();
  cin >> p;
  for(int i = 0; i < p;i++){
    cin >> s >> d >> v;
    if(cost[s][d] + 1 <= v) cout << cost[s][d] + 1 << endl;
    else cout << "NA" << endl;
  }
}

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

C++: #include <bits/stdc++.h>
using namespace std;

int router[200][200];
int b[200][200];

int main(void){
  int N;
  cin >> N;
  for(int n=0; n < N; n++){
    int r, k;
    cin >> r >> k;
    for(int i=0; i < k; i++){
      int tmp; cin >> tmp;
      b[r][tmp] = 1;
    }
  }


  int pac_n;
  cin >> pac_n;
  for(int p=0; p < pac_n; p++){
    int start, des, ttl; cin >> start >> des >> ttl;
    
    queue<pair<int, int> > que;   //<id, now_ttl>
    que.push(make_pair(start, ttl));
    bool flg = false;
    for(int x = 0; x < N+1; x++){
      for(int y = 0; y < N+1; y++){
        router[x][y] = b[x][y];
      }
    }
    while(!que.empty()){
      pair<int, int> q = que.front(); que.pop();
      if(q.first == des){
        cout << ttl - q.second + 1<< endl;
        flg = true;
        break;
      }
      if(q.second != 1){
        for(int i=0; i <= N; i++){
          if(router[q.first][i] == 1){
            router[q.first][i] = 0;
            que.push(make_pair(i, q.second - 1));
          }
        }
      }
    }
    if(!flg)
      cout << "NA" << endl;
  }
  return 0;
}

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;
      bool move[n+1];
      if(v>1)
	{
	  for(int i=0;i<=n;i++)
	    {
	      if(node[s][i]==1)
		qu.push(P(i,2));
	    }
	  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] && a.second+1<=v)
		    {
		      qu.push(P(i,a.second+1));
		      move[i]=false;
		    }
		}
	    }
	}
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<endl;
    }
}