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 <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; x++){
      for(int y = 0; y <= N; 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 <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 || res == -1){
			cout << "NA" << endl;
		}else{
			cout << res + 1 << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long  ll;

const int INF = 1e9;

void WarshallFloyd(vector<vector<int>> &d) {
    int n = d.size();

    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 main()
{
    int n, k, t, s, d, v;

    cin >> n;
    vector<vector<int>> g(n, vector<int>(n, INF));

    for (int i = 0; i < n; ++i)
        g[i][i] = 0;

    for (int i = 0; i < n; ++i) {
        cin >> s >> k;
        --s;
        while (k--) {
            cin >> t;
            g[s][t - 1] = 1;
        }
    }

    WarshallFloyd(g);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> s >> d >> v;
        --s, --d;
        if (g[s][d] + 1 > v)
            cout << "NA\n";
        else
            cout << g[s][d] + 1 << '\n';
    }

    return 0;
}

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

int main() {
  int n, m, t, u, v;
  int a[100][100];
  cin >> n;
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      a[i][j] = -1;
    }
    a[i][i] = 0;
  }
  for (int i=0; i<n; i++) {
    cin >> t >> u;
    --t;
    for (int j=0; j<u; j++) {
      cin >> v;
      --v;
      a[t][v] = 1;
    }
  }
  for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++) {
      for (int k=0; k<n; k++) {
        if (a[j][i] == -1 || a[i][k] == -1) continue;
        if (a[j][k] == -1) {
          a[j][k] = a[j][i] + a[i][k];
        } else if (a[j][k] > a[j][i] + a[i][k]) {
          a[j][k] = a[j][i] + a[i][k];
        }
      }
    }
  }
  cin >> m;
  for (int i=0; i<m; i++) {
    cin >> t >> u >> v;
    --t;
    --u;
    if (a[t][u] != -1 && a[t][u] < v) {
      cout << a[t][u]+1 << endl;
    } else {
      cout << "NA" << endl;
    }
  }

  return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A)		(A).begin(),(A).end()
#define DUMP(A)    cout<<#A<<"="<<(A)<< endl
#define SIZE(A)    (int)((A).size())
using namespace std;
typedef long long ll;

int main(){
    int n;
    scanf("%d",&n);
    int dist[100][100];
    const int INF=1000000;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            dist[i][j]=INF;
        }
    }
    for(int i=0;i<n;i++){
        int r,k,t;
        scanf("%d%d",&r,&k);
        r--;
        for(int j=0;j<k;j++){
            scanf("%d",&t);
            t--;
            dist[r][t]=1;
        }
    }
    for(int k=0;k<n;k++){
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }
    int p;
    scanf("%d",&p);
    for(int i=0;i<p;i++){
        int s,d,v;
        scanf("%d%d%d",&s,&d,&v);
        s--;d--;
        if(dist[s][d]>=v) printf("NA\n");
        else printf("%d\n",dist[s][d]+1);
    }
}