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 <vector>
#include <queue>
#include <climits>
#define REP(i,n) for(int i=0;i<n;++i)

//ダイグスとらで解くそうです

using namespace std;

typedef pair<int,int> DisNum;

vector<int> canto;

int Serch(vector<DisNum> canConnect[102],int from,int to,int hp)
{
	int distance[102] = {};//距離を保存
	REP(i,102) distance[i] = INT_MAX;
	priority_queue<DisNum,vector<DisNum>,greater<DisNum>> qu;//行き先のストックをキュー
	qu.push(DisNum(0,from));
	distance[from] = 0;
	while(!qu.empty())
	{
		DisNum nowfrom;
		nowfrom = qu.top();qu.pop();
		if(distance[nowfrom.second] != nowfrom.first) continue;
		if(nowfrom.second == to) return nowfrom.first;
		for(int i=0;i<canConnect[nowfrom.second].size();++i)
		{
			int v = canConnect[nowfrom.second][i].second;//今から調べる先の座標
			int n = nowfrom.second;
			if(distance[v] > distance[n] + canConnect[nowfrom.second][i].first)
			{
				distance[v] = distance[n] + canConnect[nowfrom.second][i].first;
				qu.push(DisNum(distance[v],v));
			}
		}
	}
}

int main(void)
{
	int maxn;
	canto.clear();
	vector<DisNum> canConnect[102];
	cin >> maxn;
	for(int i=0;i<maxn;++i)
	{
		int r,k;
		cin >> r >> k;
		for(int j=0;j<k;++j)
		{
			int t;
			cin >> t;
			canConnect[r].push_back(DisNum(1,t));
		}
	}
	int l;
	cin >> l;
	for(int j=0;j<l;++j)
	{
		int s,d,v;
		cin >> s >> d >> v;
		int ans = Serch(canConnect,s,d,v) + 1;
		if(ans <= v)
			cout << ans << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>

#define INF 1000000000

using namespace std;

int N;
int cost[128][128];
int d[128];
bool used[128];

void dijkstra(int s){
	//ツ渉可甘コツ可サ
	for(int i= 0; i < 128; i++){
		d[i] = INF;
		used[i] = false;
	}
	
	d[s] = 0;
	
	while(true){
		int v = -1;
		
		for(int i = 1; i <= N; i++){
			if(!used[i] && (v == -1 || d[i] < d[v]))v = i;
		}
		
		if(v == -1)break;
		
		used[v] = true;
		
		for(int i = 1; i <= N; i++){
			d[i] = min(d[i],d[v] + cost[v][i]);
		}
	}
}
	





int main(void){
	
	while(scanf("%d", &N) != EOF){
		int P;
		int r1,k1,t;
		int s1,d1,v1;
		
		for(int i = 0; i < 128; i++){
			for(int j = 0; j < 128; j++){
				cost[i][j] = INF;
			}
		}
		
		//ツδ仰ーツタツづ個禿偲ヲ
		for(int i = 0; i < N; i++){
			scanf("%d %d", &r1, &k1);
			for(int j = 0; j < k1; j++){
				scanf("%d", &t);
				cost[r1][t]=1;
			}
		}
		
		//ツパツケツッツトツづ個禿偲ヲ
		scanf("%d", &P);
		for(int i = 0; i < P; i++){
			scanf("%d%d%d", &s1, &d1, &v1);
			dijkstra(s1);
			
//			for(int j = 1; j < N; j++)printf("%d\n", d[i]);
			if(v1 - d[d1] > 0){
				printf("%d\n",d[d1]+1);
			}else {
				puts("NA");
			}
		}
	}
	return 0;
}

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

int main(){
  int a,b,c,d,e,f,g,now,v[200],m[201][201]={};
  queue<int> q;
  cin >> a;
  for(int i=1;i<=a;i++){
    cin >>b>>c;
    for(int j=0;j<c;j++){
      cin >> d;
      m[i][d]=1;
    }
  }
  cin >> d;
  for(int i=0;i<d;i++){
    cin >> e >> f >> g;
    q.push(e);
    for(int j=1;j<=a;j++)v[j]=1200;
    v[e]=1;
    while(1){
      now=q.front();q.pop();
      if(now==f){
	if(v[f]>g){
	  cout << "NA" << endl;
	  break;
	}else {
	  cout << v[now] << endl;
	  break;
	}
      }
      for(int j=1;j<=a;j++){
	if(m[now][j]==1&&v[j]==1200){
	  v[j]=v[now]+1;
	  q.push(j);
	}
      }
      if(q.empty()){
	cout<< "NA" << endl;
	break;
      }
    }
    while(!q.empty())q.pop();
  }
  return 0;
}

C++: #include<vector>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef vector<int> vi;
int main(){
  int n,m; cin>>n;

  int adj[100][100]={0};
  rep(i,n){
    int r,k,t; cin>>r>>k, r--;
    rep(j,k)cin>>t,adj[r][t-1]=1;
  }
  cin>>m;
  rep(i,m){
    int s,d,v; cin>>s>>d>>v,s--,d--;
    vi frontier; frontier.push_back(s);
    int cost[n]; rep(j,n)cost[j]=9999; cost[s]=0;
    for(int step=0;step<v-1&&cost[d]==9999;step++){
      vi next;
      rep(j,frontier.size())rep(h,n)if(adj[frontier[j]][h]&&cost[h]>step+1)
	cost[h]=step+1,next.push_back(h);
      frontier=next;
    }
    if(cost[d]!=9999)cout<<cost[d]+1<<endl;
    else cout<<"NA"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<21

using namespace std;

int main()
{	
	int n;
	cin >> n;
	
	int a[101][101];
	rep(i,101) rep(j,101) a[i][j] = INF;

	rep(i,n)
	{
		int r,k,temp;
		cin >> r >> k;
			
		rep(j,k)
		{
			cin >> temp;
			a[r-1][temp-1] = 1;
		}
	}

	rep(k,n)
	{
		rep(i,n)
		{
			rep(j,n)
			{
				a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
			}
		}
	}

	int p;
	cin >> p;
	rep(i,p)
	{
		int from,to,cost;
		cin >> from >> to >> cost;
		
		if(cost > a[from-1][to-1])
		{
			cout << a[from-1][to-1] + 1 << endl;
		}
		else
		{
			cout << "NA" << endl;
		}

	}
	return 0;
}

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

typedef vector<vector<int> > Graph;

const int INF = 1<<28;

int N;
Graph G;
  
int bfs(int src, int dst) {
  queue<int> que;
  vector<int> cost(N, INF);
  que.push(src);
  cost[src] = 0;
  while(que.size()) {
    const int v = que.front();
    que.pop();
    for(int i = 0; i < G[v].size(); ++i) {
      int nv = G[v][i];
      if(cost[nv] != INF) continue;
      cost[nv] = cost[v] + 1;
      que.push(nv);
    }
  }
  return cost[dst];
}

int main() {
  cin >> N;
  G.resize(N);
  vector<vector<int> > to(N);
  map<int,int> id;
  for(int i = 0; i < N; ++i) {
    int r, k;
    cin >> r >> k;
    id[r] = i;
    to[i].resize(k);
    for(int j = 0; j < k; ++j) {
      cin >> to[i][j];
    }
  }
  for(int i = 0; i < N; ++i) {
    for(int j = 0; j < to[i].size(); ++j) {
      G[i].push_back(id[to[i][j]]);
    }
  }

  int p;
  cin >> p;
  while(p--) {
    int s, d, v;
    cin >> s >> d >> v;
    s = id[s];
    d = id[d];
    int res = bfs(s, d);
    if(res < v) {
      cout << res+1 << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}