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 <cstdio>
#include <vector>
#include <queue>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back 

int bfs(int s, int t, int c);

vector<vector<int> > g(100001);

int main()
{
    int n;
    cin >> n;
    loop(i, n) {
        int r, k, t;
        cin >> r >> k;
        loop(j, k) {
            cin >> t;
            g[r].pb(t);
        }
    }
    int p;
    cin >> p;
    loop (i, p) {
        int s, d, v;
        cin >> s >> d >> v;
        int ans = bfs(s, d, v);
        if (ans != -1) {
            cout << v-ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

int bfs(int s, int t, int c)
{
    vector<bool> check(100001, false);
    queue<int> fq, nq, sq;
    int cnt = 0;
    fq.push(s);
    while (1) {
        c--;
        while (!fq.empty()) {
            int pos = fq.front(); fq.pop();
            if (check[pos]) {
                continue;
            } else {
                check[pos] = true;
            }
            //cout << pos << endl;
            if (pos == t) {
                return c;
            }
            for (int i: g[pos]) {
                nq.push(i);
            }
        }
        if (c == 0 || nq.empty()) {
            return -1;
        }
        fq = nq;
        nq = sq;
        //cout << c << "====" << endl;
    }
}

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

class Dijkstra {
#define MAX_V 102
  typedef pair<int, int> P;

private:
  const int INF = 1e9 + 10;
  struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) { }
  };

  int V;
  vector<Edge> G[MAX_V];

public:

  Dijkstra(int V): V(V) { 
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to, int cost) {
    G[from].push_back(Edge(to, cost));
  }

  vector<int> Solve(int s) {
    priority_queue<P, vector<P>, greater<P>> que;
    vector<int> d(V, INF);
    d[s] = 0;
    que.push(P(0, s));

    while (!que.empty()) {
      P p = que.top(); que.pop();
      int v = p.second;
      if (d[v] < p.first) {
        continue;
      }
      for (int i = 0; i < G[v].size(); i++) {
        Edge e = G[v][i];
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          que.push(P(d[e.to], e.to));
        }
      }
    }

    return d;
  }
};


int main() {
  int n;
  cin >> n;
  Dijkstra dijkstra(101);
  
  for (int i = 0; i < n; i++) {
    int r, k;
    cin >> r >> k;
    for (int j = 0; j < k; j++) {
      int t;
      cin >> t;
      dijkstra.AddEdge(r, t, 1);
    }
  }

  int p;
  cin >> p;

  for (int i = 0; i < p; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    vector<int> dists = dijkstra.Solve(s);
    int dist = dists[d];
    if (dist >= v) {
      cout << "NA" << endl;
    } else {
      cout << dist + 1 << endl;
    }
  }
  
  
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 999999
using namespace std;
int n, p;
int r, k;
int t;
int s, d, v, cost[110][110];
void warshall_floyd();
int main(){
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; 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[r][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 <iostream>
#include <set>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;


class Packet
{
public:
	Packet( int _now, set< int > _via ) : now( _now ), via( _via ){}
	bool isPast( int p )
	{
		if( via.find( p ) != via.end() ){
			return true;
		}
		return false;
	}

	int now;
	set< int > via;
};

int solve( int table[ SIZE ][ SIZE ], int s, int e, int initTtl, int numRooter )
{
	if( initTtl < 2 ){
		return 0;
	}
	vector< Packet > pList;
	for( int i = 1; i <= numRooter; ++i ){
		set< int > via;
		via.insert( s );
		if( table[ s ][ i ] ){
			if( i == e ){
				return 2;
			}
			set< int > via2 = via;
			via2.insert( i );
			pList.push_back( Packet( i, via2 ) );
		}
	}

	int ttl = initTtl;
	while( pList.size() > 0 ){
		--ttl;
		if( ttl < 1 ){
			return 0;
		}
		vector< Packet > pList2;
		for( int i = 0; i < pList.size(); ++i ){
			Packet& p = pList[ i ];
			if( p.now == e ){
				return p.via.size();
			}
			for( int j = 1; j <= numRooter; ++j ){
				if( table[ p.now ][ j ] && !p.isPast( j ) ){
					set< int > via = p.via;
					via.insert( j );
					pList2.push_back( Packet( j, via ) );
				}
			}
		}
		pList = pList2;
	}

	return 0;
}

int main()
{
	int table[ SIZE ][ SIZE ];

	int n;
	while( cin >> n ){
		cin.ignore( 100, '\n' );
		memset( table, 0, sizeof(table) );
		string s;
		for( int i = 0; i < n; ++i ){
			getline( cin, s );
			int o = s[ 0 ] - '0';
			for( int j = 4; j < s.size(); j += 2 ){
				table[ o ][ s[ j ] - '0' ] = true;
			}
		}
		int p;
		cin >> p;
		for( int i = 0; i < p; ++i ){
			int from, to, ttl;
			cin >> from >> to >> ttl;
			int result = solve( table, from, to, ttl, n );
			if( result > 0 ){
				cout << result << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<bits//stdc++.h>
#include<functional>
using namespace std;
struct P{
	int cost,pos;
};
bool operator<(P a,P b) {return a.cost<b.cost;}
bool operator>(P a,P b) {return a.cost>b.cost;}
long long a[101];
const long long inf=800000000;
vector<long long>e[101];
int main() {
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int r,k,c;
		cin>>r>>k;
		for(int i=0;i<k;i++){
			cin>>c;
			e[r].push_back(c);
		}
	}
	int p;
	cin>>p;
	for(int z=0;z<p;z++){
		int s,d,v;
		cin>>s>>d>>v;
		fill(a,a+n+1,inf);
		priority_queue<P>p;
		p.push(P{1,s});
		a[s]=1;
		while(!p.empty()){
			P t=p.top();p.pop();
			for(int i=0;i<(int)e[t.pos].size();i++){
				if(a[t.pos]+1<a[e[t.pos][i]]){
					a[e[t.pos][i]]=a[t.pos]+1;
					p.push(P{a[t.pos]+1,e[t.pos][i]});
				}
			}
		}
		if(a[d]<=v) cout<<a[d]<<endl;
		else cout<<"NA"<<endl;
	}
}