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

#define INF 10000000

int n,r,k,t,p,s,d,v;
vector<pair<int,vector<int> > > vec;

int find(int num){
	for(int i = 0; i < n; i++){
		if(vec[i].first == num){
			return i;
		}
	}
	return -1;
}

int sch(int st, int ed){
	int from = find(st);
	int to = find(ed);
	vector<int> vv = vector<int>(n,INF);
	vv[from] = 0;
	for(int i = 1; i <= n; i++){
		for(int vi = 0; vi < n; vi++){
			if(vv[vi] == i - 1){
				for(int j = 0; j < vec[vi].second.size(); j++){
					int tmp = find(vec[vi].second[j]);
					if(vv[tmp] == INF){
						vv[tmp] = i;
					}
				}
			}
		}
	}
	return vv[to];
}

int main(){
	scanf("%d",&n);
	vec = vector<pair<int,vector<int> > >(n);
	for(int i = 0; i < n; i++){
		scanf("%d%d",&r,&k);
		vec[i].first = r;
		vec[i].second = vector<int>(k);
		for(int j = 0; j < k; j++){
			scanf("%d",&t);
			vec[i].second[j] = t;
		}
	}
	scanf("%d",&p);
	for(int i = 0; i < p; i++){
		scanf("%d%d%d",&s,&d,&v);
		int l = sch(s,d);
		l += 1;
		if(l <= v){
			printf("%d\n",l);
		}
		else{
			printf("NA\n");
		}
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back

#define INF 1000010
#define MAX_V 110

using namespace std;

int cost[MAX_V][MAX_V];

int main(){
    int n;

    for(int i=0; i < MAX_V; i++) for(int j=0; j < MAX_V; j++) cost[i][j] = INF;
    for(int i=0; i < MAX_V; i++) cost[i][i] = 0;

    cin >> n;
    for(int i=0; i < n; i++){
        int e1;
        int m;
        cin >> e1 >> m;
        for(int j=0; j < m; j++){
            int e2;
            cin >> e2;
            cost[e1-1][e2-1] = 1;
        }
    }

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

    cin >> n;
    for(int i=0; i < n; i++){
        int e1, e2, t;
        cin >> e1 >> e2 >> t;
        if(cost[e1-1][e2-1] != INF && cost[e1-1][e2-1]+1 <= t) cout << cost[e1-1][e2-1]+1 << endl;
        else cout << "NA" << endl;
    }
}

C++: #include <iostream>
#include <sstream>
#include <algorithm>

#define INF 1000000000

using namespace std;

int main(void){
	int n;
	int s, t, num;
	
	cin >> n;
	int d[n][n];
	fill((int *)d, (int *)d+n*n, INF);
	//for(int i=0;i<n;i++) d[i][i] = 0;
	for(int i=0;i<n;i++){
		cin >> s;
		cin >> num;
		for(int j=0;j<num;j++){
			cin >> t;
			d[s-1][t-1] = 1;
		}
	}
	
	//WF
	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]);
			}
		}
	}
	
	cin >> n;
	
	for(int i=0;i<n;i++){
		cin >> s >> t >> num;
		d[s-1][t-1]+1 <= num?cout << d[s-1][t-1]+1 << endl:cout << "NA" << endl;
	}
}

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;
    }
}

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

#define MAX_N 110

#define INF 999999999

int x[MAX_N][MAX_N];
int d[MAX_N];

queue<int>Q;

int bfs(int s, int t){
	for(int i=0;i<MAX_N;i++){
		d[i]=INF;
	}
	d[s]=0;
	Q.push(s);
	while(!Q.empty()){
		int z=Q.front();
		for(int i=0;i<MAX_N;i++){
			if(x[z][i]<INF){
				if(d[i]==INF){
					d[i]=d[z]+1;
				}
			}
		}
		Q.pop();
	}
	return d[t];
}

int main(){
	int n,m,a,b,c;
	for(int i=0;i<MAX_N;i++){
		for(int j=0;j<MAX_N;j++){
			x[i][j]=INF;
		}
	}
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		for(int j=0;j<b;j++){
			cin>>c;
			x[a][c]=1;
		}
	}
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b>>c;
		int y=bfs(a,b);
		if(y>=c){cout<<"NA"<<endl;}
		else{cout<<y+1<<endl;}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef pair<int,int> NODE;

map<int,vector<int> > list;

int bfs(int s,int g){
	map<int,bool> done;
	queue< NODE > Q;
	Q.push( make_pair(s,1) );
	while(!Q.empty()){
		NODE q = Q.front();
		Q.pop();
		done[q.first] = true;
		
		if(q.first == g){
			return q.second;
		}
		for(int i=0;i<list[q.first].size();i++){
			if( !done[ list[q.first][i] ] )Q.push( make_pair(list[q.first][i],q.second+1) );
		}
	}
	cout << endl;
	return (1<<21);
}
int main(){
	int n,r,k,t;
	int p,s,d,v;
	cin >> n;
	list.clear();
	for(int i=0;i<n;i++){
		cin >> r >> k;
		for(int j=0;j<k;j++){
			cin >> t;
			list[r].push_back(t);
		}
	}
	cin >> p;
	for(int i=0;i<n;i++){
		cin >> s >> d >> v;
		int ret = bfs(s,d);
		if( ret <= v){
			cout << ret << endl; 
		}else{
			cout << "NA" << endl;
		}
	}
}