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 <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef long long ll;

const int MAX_SIZE = 10000;

struct edge{
	int to;
	int cost;
};

typedef pair<int,int> P;
int V;

const int MAX_V = 5000;

vector<edge> G[MAX_V];
int d[MAX_V];
const int INF = 100000000;

int prevv[MAX_V];

class Situation{
public:
	int ttl;
	int node;
	int dist;

	Situation();
	Situation(int ttl_,int node_,int dist_){
		ttl=ttl_;
		node=node_;
		dist=dist_;
	}

	bool operator<(const Situation &sit)const{
		return sit.dist > this->dist;
	}
};

void dijkstra(int s,int ttl){
	priority_queue<Situation > que;
	fill(d,d+MAX_V,INF);
	d[s] = 0;
	que.push(Situation(ttl,s,0));
	while(!que.empty()){
		Situation p = que.top();
		que.pop();
		int v = p.node;
		if(d[v] < p.dist)
			continue;
		if(p.ttl<=1)
			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(Situation(ttl-1,e.to,d[e.to]));
			}
		}
	}
}


int main(){

	int n;
	cin>>n;
	for(int i = 0; i < n; i++){
		int from;
		cin>>from;
		int m;
		cin>>m;
		for(int j = 0; j < m; j++){
			int to;
			cin>>to;
			edge e;
			e.cost=1;
			e.to=to;
			G[from].push_back(e);
		}
	}
	int l;
	cin>>l;
	for(int j = 0; j < l; j++){
		int s,dd,v;
		cin>>s>>dd>>v;
		dijkstra(s,v);
		if(d[dd]==INF)
			cout<<"NA"<<endl;
		else
			cout<<d[dd]+1<<endl;
	}


	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
 
const int INF = 100000000;
int a[101][101];

void solve(int n) {
  
  for(int i = 0 ; i < n ; i++)
    for(int j = 0 ; j < n ; j++)
      a[i][j] = INF;
  
  for(int i = 0 ; i < n ; i++) {
    int r, k;
    cin >> r >> k;
    for(int j = 0 ; j < k ; j++) {
      int t;
      cin >> t;
      a[r-1][t-1] = 1;
    }
  }
  
  for(int k = 0 ; k < n ; k++)
    for(int i = 0 ; i < n ; i++)
      for(int j = 0 ; j < n ; j++)
	a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
  
  int m;
  cin >> m;
  for(int i = 0 ; i < m ; i++) {
    int s, d, v;
    cin >> s >> d >> v;
    if(a[s-1][d-1]+1 > v) cout << "NA" << endl;
    else cout << a[s-1][d-1]+1 << endl;
  }
}

int main() {
  int n;
  while(cin >> n) {
    solve(n);
  }
  return 0;
}

C++: #include "stdio.h"
#include <bits/stdc++.h>
using namespace std;
int eg[101][101];
int d[101][101];
int const MAX=10000;
int main(){
    int n;
    cin >> n;
    for (int i=0;i<n;i++){
        int nt;
        cin >> nt;
        string str;
        getline(cin,str);
        stringstream ss;
        ss << str;
        while (!ss.eof()){
            int to;
            ss >> to;
            eg[nt][to]=1;
        }
    }
    for(int f=1;f<=n;f++)for(int t=1;t<=n;t++){
        d[f][t]=f==t?0:(eg[f][t]==1?1:MAX);
    }
    for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){
        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    }
    int rn;
    cin >> rn;
    for (int i=0;i<rn;i++){
        int from,to,ttl;
        cin >> from >> to >> ttl;
        if (d[from][to]+1>ttl)cout << "NA";
        else cout << d[from][to]+1;
        cout << endl;
    }
    
}

C++: #include <iostream>
const int INF=1e9;
using namespace std;
int main(){
  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++) d[r-1][t-1]=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]);
  for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][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<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(vector<int> t[],int k[],int b[],int c,int s,int d,int v){
  int i;
  if(v==0);
  else if(s==d){
    if(b[s]==0||b[s]>c)
      b[s]=c;
  }else if(b[s]==0||b[s]>c){
    b[s]=c;
    for(i=0;i<k[s];i++)
      f(t,k,b,c+1,t[s][i],d,v-1);
  }
  return;
}
int main(){
  int i,j;
  int k[101],b[101],n,p,s,d,v;
  vector<int> t[101],u;
  cin>>n;
  for(i=0;i<n;i++){
    int q,r;
    cin>>r>>q;
    k[r]=q;
    t[r].reserve(k[r]);
    for(j=0;j<k[r];j++)
      cin>>t[r][j];
  }
  cin>>p;
  u.reserve(p);
  for(i=0;i<p;i++){
    cin>>s>>d>>v;
    memset(b,0,sizeof(b));
    f(t,k,b,1,s,d,v);
    u[i]=b[d];
  }
  for(i=0;i<p;i++){
    if(u[i])
      cout<<u[i]<<endl;
    else
      cout<<"NA"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#define N 100
#define INFINITY 2000000000
using namespace std;
int bfs();
int n,r,k,t,p,s,g,v,u,M[N][N],d[N],result;
string color[N];
int main(){
  int i,j;
  cin >> n;
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      M[i][j]=0;
    }
  }
  for(i=0;i<n;i++){
    cin >> r >> k;
    for(j=0;j<k;j++){
      cin >> t;
      M[r-1][t-1]=1;
    }
  }
  cin >> p;
  for(i=0;i<p;i++){
    cin >> s >> g >> v;
    s--,g--;
    result=bfs();
    if(result!=0) cout << result << endl;
    else cout << "NA" << endl;
  }
  return 0;
}
int bfs(){
  int i,j;
  queue<int> q;
  for(i=0;i<n;i++) color[i]="WHITE",d[i]=INFINITY;
  color[s]="GLAY";
  d[s]=0;
  q.push(s);
  while(q.empty()!=1){
    u=q.front();
    q.pop();
    for(i=0;i<n;i++){
      if(M[u][i]&&color[i]=="WHITE"){
	color[i]="GLAY";
	d[i]=d[u]+1;
	q.push(i);
      }
    }
    color[u]="BLACK";
  }
  v-=d[g];
  if(v>0) return d[g]+1;
  return 0;
}