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>
using namespace std;

int main(){
	int n,i,j,k,a,b,c,cost[101][101];
	cin>>n;
	for(i=1;i<=n;i++){
		cin>>a>>b;
		for(j=1;j<=n;j++) cost[a][j] = 1<<15;
		for(j=0;j<b;j++){
			cin >> c;
			cost[a][c]=1;
		}
	}
	for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)
		cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a>>b>>c;
		if(cost[a][b]<c && cost[a][b]<(1<<15)) cout << cost[a][b]+1 << endl;
		else cout << "NA" << endl;
	}
}

C++: // 0144
#include <iostream>
#include <vector>
#include <queue>
#define  N    1000
using namespace std;

void p_transport(int s, int d, int v, vector<vector<int> > R){
	vector<int> dis;

	for(int i=0;i<R.size();i++)
		if(i == s)
			dis.push_back(1);
		else
			dis.push_back(N);

	queue<int> ver;
	ver.push(s);

	while(!ver.empty()){
		int x = ver.front();
		for(int i=0;i<R[x].size();i++)
			if(dis[R[x][i]] > dis[x] + 1){
				dis[R[x][i]] = dis[x] + 1;
				ver.push(R[x][i]);
			}
		ver.pop();
	}

	if(dis[d] <= v)
		cout<<dis[d]<<endl;
	else
		cout<<"NA"<<endl;
}

int main(){
	int n, r, k, t;
	vector<vector<int> > router;

	cin>>n;
	router.resize(n+1);

	for(int i=0;i<n;i++){
		cin>>r>>k;

		for(int j=0;j<k;j++){
			cin>>t;
			router[r].push_back(t);
		}
	}

	int p, s, d, v;
	cin>>p;

	for(int i=0;i<p;i++){
		cin>>s>>d>>v;

		p_transport(s, d, v, router);
	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#include <climits>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main() {
	int n, p, r, k, t, s, d, v;
	cin >> n;
	int m[100][100] = {};
	rep(i,n) {
		cin >> r >> k;
		rep(j,k) {
			cin >> t;
			m[r-1][t-1] = 1;
		}
	}
	int ret[100][100] = {};
	rep(i,100) {
		ret[i][i] = 1;
	}
	rep(i,n) {
		queue<int> q;
		q.push(i);
		while(!q.empty()) {
			int a = q.front(); q.pop();
			rep(j,n) {
				if(m[a][j] && ret[i][j]==0) {
					q.push(j);
					ret[i][j] = ret[i][a]+1;
				}
			}
		}
	}
	rep(i,n) {
		rep(j,n) {
			cout << ret[i][j] << " ";
		}
		cout <<endl;
	}
	cin >> p;
	rep(i,p) {
		cin >> s >> d >> v;
		--s,--d;
		if(ret[s][d] > v || ret[s][d] == 0) cout<< "NA" << endl;
		else cout << ret[s][d] << endl;
	}
	return 0;
}

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

int main(){
  int n,p;
  unsigned int dis[101][101];
  int r,k,t;
  int s,d,v;

  cin >> n;

  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      dis[i][j] = (1<<30);
      if(i==j)dis[i][j] = 0;
    }
  }

  for(int i=0;i<n;i++){
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> t;
      dis[r][t] = 1;
    }
  }

  for(int x=1;x<=n;x++){
    for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++)dis[i][j] = min(dis[i][j],dis[i][x] + dis[x][j]);
    }
  }

  cin >> p;
  for(int i=0;i<p;i++){
    cin >> s >> d >> v;
    if(dis[s][d]+1>v)cout << "NA" << endl;
    else cout << dis[s][d]+1 << endl;
  }
}

C++: #include <iostream>
#include <climits>
const int INF=500;
using namespace std;
int main(void){
    int d[100][100],n,p;
    cin>>n;
    for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=INF;
    for(int i=0,r,k,t;i<n&&cin>>r>>k;i++) for(int j=0;j<k&&cin>>t;j++) cin>>d[r-1][t-1];
    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>>p;
    for(int i=0,s,t,v;i<p;i++){
        cin>>s>>t>>v;
        if(d[s-1][t-1]>v) cout<<"NA"<<endl;
        else cout<<d[s-1][t-1]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<map>
#include<queue>
#include<set>

using namespace std;
typedef pair<int,int> PII;
typedef long long ll;

int main(){
  int n;
	cin>>n;
	int graph[n][n];
	for(int j=0;j<n;j++)
		for(int i=0;i<n;i++){
				graph[j][i] = 10000;
		}
	for(int i=0;i<n;i++){
		int k,m,l;
		cin>>k>>m;
		for(int j=0;j<m;j++){
			cin>>l;
			graph[k-1][l-1] = 1;
		}
	}
	
	for(int k=0;k<n;k++)
		for(int j = 0;j<n;j++)
			for(int i = 0;i<n;i++)
				if(graph[i][j]>graph[i][k]+graph[k][j])
					graph[i][j] = graph[i][k]+graph[k][j];
	int p;
	cin>>p;
	for(int i=0;i<p;i++){
		int s,d,v;
		cin>>s>>d>>v;
		if(graph[s-1][d-1]<v)
			cout<<graph[s-1][d-1]+1<<endl;
		else
			cout<<"NA"<<endl;
	}
  return 0;
}

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

int md = 10000000;
vector <int> ro[100];
int s,d,v;
int s2,d2;
int kk[100],n;
bool flg[101] ;

void visit(int sn,int dis) {
  int x;
  
  flg[sn] = 1;
  
  if(sn == d2 && dis <=v){
    md = min(md,dis);
    // cout << md <<" "<<sn <<" "<<d2 << endl;
  }
  
  else if(dis <= v) {
    for(int i=0;i<ro[sn].size();i++) {
      for(int j=0;j<n;j++)
	if(ro[sn][i] == kk[j]) {
	  x = j;
	  break;
	}
      if(dis == 1){
	for(int i=0;i<=n;i++) flg[i] = 0;
	//	cout <<"reset"<<endl;
	flg[s2] = 1;
      }
      //       cout <<sn+1 << "to"<<x+1 << endl;
      if(flg[x] == 0) visit(x,dis+1);
    } 
  }
}
  
int main() {

  cin >> n;
  int a;
    
  for(int i=0;i<n;i++) {
    cin >> kk[i];
    cin >> a;
    ro[i].resize(a);
    for(int j=0;j<a;j++) cin >> ro[i][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;

   
    for(int i=0;i<n;i++){
      if(kk[i] == s) s2 = i;
      if(kk[i] == d) d2 = i;
    }

    for(int i=0;i<=n;i++) flg[i] = 0;
    
    for(int i=0;i<n;i++) 
    md = 10000000;
    visit(s2,1);
    if(md != 10000000) cout << md <<endl;
    else cout << "NA" <<endl;
  }
  
  return 0;
  
}