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 <stdio.h>
#include <memory.h>

int G[100][100] = {0}; // [from][to] = 0, 1
int k[100];            // edgenum
int n;
int qhead = 0, qtail = 0;
int queue[100];
int cost[100], dat[100];

void bfs(int);
void PushQueue(int);
int PopQueue();
int isQueueEmpty();

int main (void) {
  int i, j;
  int from_edge, to_edge;

  scanf("%d", &n);
  
  for(i = 0; i < n; i++) {
    scanf("%d", &from_edge);
    scanf("%d", &k[from_edge-1]);
    for(j = 0; j < k[from_edge-1]; j++) {
      scanf("%d", &to_edge);
      G[from_edge-1][to_edge-1] = 1;
    }
  }

  //for (i = 0; i < n; i++) {
  //  for (j = 0; j < n; j++) {
  //    printf("%d ", G[i][j]);
  //  }
  //  printf("\n");
  //}

  int n_packet;
  int from_node, to_node, ttl;

  scanf("%d", &n_packet);
  
  for (i = 0; i < n_packet; i++) {
    scanf("%d", &from_node);
    scanf("%d", &to_node);
    scanf("%d", &ttl);

    cost[from_node-1] = 0;
    qhead = 0; qtail = 0;
    bfs(from_node);

    //for (j = 0; j < n; j++) { printf("cost[%d] = %d\n", j, cost[j]); }

    if (cost[to_node-1] < ttl) {
      printf("%d\n", cost[to_node-1] + 1);  // senderの通過も含める
    } else {
      printf("NA\n");
    }
  }

  return 0;
}

void bfs(int st_node) {
  int i;
  int u, v = st_node - 1;
  int visited[n];

  for (i = 0; i < n; i++) {
    visited[i] = 0;
  }
  
  PushQueue(v);
  visited[v] = 1;

  while(!isQueueEmpty()) {
    u = PopQueue();
    //printf("u:%d\n", u);
    for(v = 0; v < n; v++) {
      if(G[u][v] && !visited[v]) {
	PushQueue(v);
	cost[v] = cost[u] + 1;
	visited[v] = 1;
      }
    }

  }
}

void PushQueue(int num) {
  queue[qtail] = num;
  qtail++;
}
int PopQueue() {
  int ret;

  ret = queue[qhead];
  qhead++;
  return ret;
}

int isQueueEmpty() {
  if (qtail == qhead) return 1; // Queue is empty.

  return 0; // Queue exists.
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }


const int INF = 1 << 20;
struct P
{
	int r1, k1;
	vint ok;
};


int main()
{
    int n, p;
	cin >> n;
	cin.ignore();
	
	vector<P> v(n);
	rep(i, n)
	{
		cin >> v[i].r1;
		
		int k1;
		cin >> k1;
		
		rep(j, k1)
		{
			int buf;
			cin >> buf;
			v[i].ok.push_back(buf);
		}
	}
	
	cin >> p;
	
	vector<vint> G(n, vint(n, INF));
	
	rep(i, n)
	{
		rep(j, v[i].ok.size())
		{
			G[v[i].r1 - 1][v[i].ok[j] - 1] = 1;
		}
	}
	
	rep(k, n) rep(i, n) rep(j, n)
		chmin(G[i][j], G[i][k] + G[k][j]);
		
	rep(i, p)
	{
		int s1, d1, v1;
		cin >> s1 >> d1 >> v1;
		
		int cost = G[s1 - 1][d1 - 1];
		if(cost == INF || v1 <= cost)
		{
			cout << "NA" << endl;
		}
		else
		{
			cout << cost + 1 << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  int r,k,t;
  int p;
  int s,d,b;
  int ans=0;
  int box[111][111];
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      box[i][j]=INT_MAX;
    }
  }
  for(int i=0;i<n;i++){
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> t;
      box[r-1][t-1]=1;
    }
  }
  for(int z=0;z<n;z++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	box[i][j]=min(box[i][j],box[i][z]+box[z][j]);
      }
    }
  }
  cin >> p;
  for(int i=0;i<p;i++){
    cin >> s >> d >> b;
    ans=box[s-1][d-1]+1;
    if(ans<=b){
      cout << box[s-1][d-1]+1 << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

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

class Node{
public:
	vector< int > e_to;
	vector< int > e_cost;
	
	bool def;
	int min_cost;
	
	Node(){
		def = false;
		min_cost = -1;
	}
};
bool operator==(const Node& ot1, const Node& ot2){
	return ot1.min_cost == ot2.min_cost;
}
bool operator!=(const Node& ot1, const Node& ot2){
	return !(ot1 == ot2);
}
bool operator<(const Node& ot1, const Node& ot2){
	return ot1.min_cost < ot2.min_cost;
}
bool operator>(const Node& ot1, const Node& ot2){
	return ot1.min_cost > ot2.min_cost;
}
bool operator<=(const Node& ot1, const Node& ot2){
	return !(ot1 > ot2);
}
bool operator>=(const Node& ot1, const Node& ot2){
	return !(ot1 < ot2);
}

class Dijkstra{
	priority_queue< Node, vector< Node >, greater< Node > > que;
	vector< Node > nodes;
	
public:
	void Init(int node_num){
		nodes.resize(node_num);
		for(int i = 0; i < node_num; ++i){
			nodes[i] = Node();
		}
	}
	
	void Put(int id, int to, int cost){
		nodes[id].e_to.push_back(to);
		nodes[id].e_cost.push_back(cost);
	}
	
	int Run(int s, int t){
		
		for(vector< Node >::iterator it = nodes.begin(); it != nodes.end(); ++it){
			it->min_cost = -1;
			it->def = false;
		}
		
		nodes[s].min_cost = 0;
		que.push(nodes[s]);
		
		while(!que.empty()){
			Node def_node = que.top();
			que.pop();
		
			if(def_node.def)continue;
		
			def_node.def = true;
		
			for(int i = 0; i < def_node.e_to.size(); ++i){
				int to = def_node.e_to[i];
				int cost = def_node.min_cost + def_node.e_cost[i];
				
				if(nodes[to].min_cost == -1 || cost < nodes[to].min_cost){
					nodes[to].min_cost = cost;
					que.push(nodes[to]);
				}
			}
		}
		return nodes[t].min_cost;
	}
};


int main(){
	int n, r, k, t, p, s, d, v;
	Dijkstra dijk;
	
	
	cin >> n;
	
	dijk.Init(n);

	for(int i = 0; i < n; ++i){
		cin >> r >> k;
		
		for(int j = 0; j < k; ++j){
			cin >> t;
			dijk.Put(r - 1, t - 1, 1);
		}
	}
	
	cin >> p;
	
	for(int i = 0; i < p; ++i){
		cin >> s >> d >> v;
		
		int res = dijk.Run(s - 1, d - 1);
		
		if(res >= v){
			cout << "NA" << endl;
		}else{
			cout << res + 1 << endl;
		}
	}
	return 0;
}